transform-base.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
23 #define NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
24 
25 #include "../../common.hpp"
26 #include <vector>
27 
28 namespace ndn {
29 namespace security {
30 namespace transform {
31 
47 class Error : public std::runtime_error
48 {
49 public:
50  Error(size_t index, const std::string& what);
51 
52  size_t
53  getIndex() const
54  {
55  return m_index;
56  }
57 
58 private:
59  size_t m_index;
60 };
61 
68 {
69 public:
70  virtual
71  ~Downstream() = default;
72 
89  size_t
90  write(const uint8_t* buf, size_t size);
91 
100  void
101  end();
102 
106  bool
107  isEnd() const
108  {
109  return m_isEnd;
110  }
111 
115  void
116  setIndex(size_t index)
117  {
118  m_index = index;
119  }
120 
124  size_t
125  getIndex() const
126  {
127  return m_index;
128  }
129 
130 protected:
131  Downstream();
132 
133 private:
137  virtual size_t
138  doWrite(const uint8_t* buf, size_t size) = 0;
139 
143  virtual void
144  doEnd() = 0;
145 
146 private:
147  bool m_isEnd;
148  size_t m_index;
149 };
150 
156 class Upstream
157 {
158 public:
159  virtual
160  ~Upstream() = default;
161 
162 protected:
163  Upstream();
164 
165 protected:
169  void
170  appendChain(unique_ptr<Downstream> tail);
171 
172  Downstream*
174  {
175  return m_next.get();
176  }
177 
178 protected:
179  unique_ptr<Downstream> m_next;
180 };
181 
185 class Transform : public Upstream,
186  public Downstream,
187  noncopyable
188 {
189 protected:
190  typedef std::vector<uint8_t> OBuffer;
191 
192  Transform();
193 
199  void
200  flushOutputBuffer();
201 
206  void
207  flushAllOutput();
208 
212  void
213  setOutputBuffer(unique_ptr<OBuffer> buffer);
214 
218  bool
219  isOutputBufferEmpty() const;
220 
221 private:
225  size_t
226  doWrite(const uint8_t* data, size_t dataLen) final;
227 
233  void
234  doEnd() final;
235 
247  virtual void
248  preTransform();
249 
255  virtual size_t
256  convert(const uint8_t* data, size_t dataLen) = 0;
257 
265  virtual void
266  finalize();
267 
268 private:
269  unique_ptr<OBuffer> m_oBuffer;
270  size_t m_outputOffset;
271 };
272 
278 class Sink : public Downstream,
279  noncopyable
280 {
281 };
282 
288 class Source : public Upstream,
289  noncopyable
290 {
291 public:
295  Source&
296  operator>>(unique_ptr<Transform> transform);
297 
303  void
304  operator>>(unique_ptr<Sink> sink);
305 
306 protected:
307  Source();
308 
312  void
313  pump();
314 
318  size_t
319  getIndex() const
320  {
321  return 0;
322  }
323 
324 private:
328  virtual void
329  doPump() = 0;
330 
331 private:
332  size_t m_nModules; // count of modules in the chain starting from this Source
333 };
334 
335 } // namespace transform
336 } // namespace security
337 } // namespace ndn
338 
339 #endif // NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
Error(size_t index, const std::string &what)
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
size_t getIndex() const
Get the source module index (should always be 0).
Abstraction of the transformation sink module.
void setIndex(size_t index)
Set the module index.
The upstream interface of a transformation module.
unique_ptr< Downstream > m_next
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:324
Base class of transformation error.
Abstraction of an intermediate transformation module.
The downstream interface of a transformation module.
bool isEnd() const
Check if the input interface of a module is closed.
Abstraction of the transformation source module.
size_t getIndex() const
Get the module index.