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-2018 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 
26 
27 #include <vector>
28 
29 namespace ndn {
30 namespace security {
31 namespace transform {
32 
48 class Error : public std::runtime_error
49 {
50 public:
51  Error(size_t index, const std::string& what);
52 
53  size_t
54  getIndex() const
55  {
56  return m_index;
57  }
58 
59 private:
60  size_t m_index;
61 };
62 
69 {
70 public:
71  virtual
72  ~Downstream() = default;
73 
90  size_t
91  write(const uint8_t* buf, size_t size);
92 
101  void
102  end();
103 
107  bool
108  isEnd() const
109  {
110  return m_isEnd;
111  }
112 
116  void
117  setIndex(size_t index)
118  {
119  m_index = index;
120  }
121 
125  size_t
126  getIndex() const
127  {
128  return m_index;
129  }
130 
131 protected:
132  Downstream();
133 
134 private:
138  virtual size_t
139  doWrite(const uint8_t* buf, size_t size) = 0;
140 
144  virtual void
145  doEnd() = 0;
146 
147 private:
148  bool m_isEnd;
149  size_t m_index;
150 };
151 
157 class Upstream
158 {
159 public:
160  virtual
161  ~Upstream() = default;
162 
163 protected:
164  Upstream();
165 
166 protected:
170  void
171  appendChain(unique_ptr<Downstream> tail);
172 
173  Downstream*
175  {
176  return m_next.get();
177  }
178 
179 protected:
180  unique_ptr<Downstream> m_next;
181 };
182 
186 class Transform : public Upstream,
187  public Downstream,
188  noncopyable
189 {
190 protected:
191  typedef std::vector<uint8_t> OBuffer;
192 
193  Transform();
194 
200  void
201  flushOutputBuffer();
202 
207  void
208  flushAllOutput();
209 
213  void
214  setOutputBuffer(unique_ptr<OBuffer> buffer);
215 
219  bool
220  isOutputBufferEmpty() const;
221 
222 private:
226  size_t
227  doWrite(const uint8_t* data, size_t dataLen) final;
228 
234  void
235  doEnd() final;
236 
248  virtual void
249  preTransform();
250 
256  virtual size_t
257  convert(const uint8_t* data, size_t dataLen) = 0;
258 
266  virtual void
267  finalize();
268 
269 private:
270  unique_ptr<OBuffer> m_oBuffer;
271  size_t m_outputOffset;
272 };
273 
279 class Sink : public Downstream,
280  noncopyable
281 {
282 };
283 
289 class Source : public Upstream,
290  noncopyable
291 {
292 public:
296  Source&
297  operator>>(unique_ptr<Transform> transform);
298 
304  void
305  operator>>(unique_ptr<Sink> sink);
306 
307 protected:
308  Source();
309 
313  void
314  pump();
315 
319  size_t
320  getIndex() const
321  {
322  return 0;
323  }
324 
325 private:
329  virtual void
330  doPump() = 0;
331 
332 private:
333  size_t m_nModules; // count of modules in the chain starting from this Source
334 };
335 
336 } // namespace transform
337 } // namespace security
338 } // namespace ndn
339 
340 #endif // NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
Error(size_t index, const std::string &what)
Definition: data.cpp:26
Abstraction of the transformation sink module.
size_t getIndex() const
Get the module index.
void setIndex(size_t index)
Set the module index.
The upstream interface of a transformation module.
Common includes and macros used throughout the library.
unique_ptr< Downstream > m_next
size_t getIndex() const
Get the source module index (should always be 0).
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:370
Base class of transformation error.
bool isEnd() const
Check if the input interface of a module is closed.
Abstraction of an intermediate transformation module.
The downstream interface of a transformation module.
Abstraction of the transformation source module.