libcommonc++  0.7
DataEncoder.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_DataEncoder_hxx
24 #define __ccxx_DataEncoder_hxx
25 
27 
28 #include <deque>
29 
30 namespace ccxx {
31 
32 class DataEncoder; // fwd decl
33 
75 {
76  public:
77 
85  {
86  public:
87 
93  : _encoder(encoder)
94  { _encoder.pushContext(); }
95 
98  { _encoder.popContext(); }
99 
100  private:
101 
102  DataEncoder& _encoder;
103 
105  };
106 
116  {
117  public:
118 
120  virtual ~Manipulator() { }
121 
128  virtual void apply(DataEncoder& encoder) const = 0;
129  };
130 
138  {
139  public:
140 
146  SetEndianness(Endianness endianness);
147 
149  ~SetEndianness();
150 
151  protected:
152 
153  void apply(DataEncoder& encoder) const;
154 
155  private:
156 
157  Endianness _endianness;
158  };
159 
167  {
168  public:
169 
175  SetLength(size_t length);
176 
178  ~SetLength();
179 
180  protected:
181 
182  void apply(DataEncoder& encoder) const;
183 
184  private:
185 
186  size_t _length;
187  };
188 
197  {
198  public:
199 
205  Skip(size_t count);
206 
208  ~Skip();
209 
210  protected:
211 
212  void apply(DataEncoder& encoder) const;
213 
214  private:
215 
216  size_t _count;
217  };
218 
228  {
229  public:
230 
237  Align(size_t size);
238 
240  ~Align();
241 
242  protected:
243 
244  void apply(DataEncoder& encoder) const;
245 
246  private:
247 
248  size_t _size;
249  };
250 
259  {
260  public:
261 
267  SetLimit(int64_t limit);
268 
270  ~SetLimit();
271 
272  protected:
273 
274  void apply(DataEncoder& encoder) const;
275 
276  private:
277 
278  int64_t _limit;
279  };
280 
287  virtual void reset();
288 
290  void pushContext();
291 
297  void popContext();
298 
300  bool isTopContext() const;
301 
303  uint_t getContextDepth() const;
304 
309  void setEndianness(Endianness endianness);
310 
312  Endianness getEndianness() const;
313 
318  void setLength(size_t length);
319 
321  size_t getLength() const;
322 
330  void setLimit(int64_t limit);
331 
333  int64_t getLimit() const;
334 
336  int64_t getOffset() const;
337 
343  virtual void setOffset(int64_t offset) = 0;
344 
349  int64_t getCumulativeOffset() const;
350 
356  int64_t getRemaining() const;
357 
367  void align(size_t size);
368 
377  virtual void skip(size_t count) = 0;
378 
380  virtual ~DataEncoder();
381 
382  protected:
383 
385  DataEncoder();
386 
391  bool isSameEndianness() const;
392 
399  struct Context
400  {
402  Context();
403 
404  void reset();
405 
406  void bumpOffset(size_t delta);
407 
411  size_t length;
413  int64_t limit;
415  int64_t parentLimit;
417  int64_t offset;
419  int64_t maxOffset;
420  };
421 
423  Context& currentContext();
424 
426  const Context& currentContext() const;
427 
436  void checkRemaining(const Context& ctx, size_t count) const;
437 
439  class ContextStack; // fwd decl
440 
441  ContextStack* _contexts;
442  int64_t _maxOffset;
445  private:
446 
448 };
449 
450 } // namespace ccxx
451 
452 #endif // __ccxx_DataEncoder_hxx
Endianness endianness
The current byte endianness.
Definition: DataEncoder.h++:409
A manipulator that skips past bytes in the stream.
Definition: DataEncoder.h++:196
A manipulator that specifies the maximum number of bytes that can be read/written.
Definition: DataEncoder.h++:258
virtual ~Manipulator()
Destructor.
Definition: DataEncoder.h++:120
size_t length
The current value length.
Definition: DataEncoder.h++:411
A manipulator that skips past enough bytes to re-align the data stream at a specified boundary...
Definition: DataEncoder.h++:227
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
A lexically scoped DataEncoder context.
Definition: DataEncoder.h++:84
A manipulator that specifies the length of String, Blob, and array values to be subsequently encoded/...
Definition: DataEncoder.h++:166
A base class for DataEncoder manipulators.
Definition: DataEncoder.h++:115
int64_t offset
The current offset.
Definition: DataEncoder.h++:417
int64_t parentLimit
The parent (enclosing) limit.
Definition: DataEncoder.h++:415
int64_t limit
The current limit.
Definition: DataEncoder.h++:413
#define COMMONCPP_API
Definition: Common.h++:126
~ScopedContext()
Destructor.
Definition: DataEncoder.h++:97
int64_t maxOffset
The maximum offset.
Definition: DataEncoder.h++:419
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
ScopedContext(DataEncoder &encoder)
Construct a new ScopedContext for the given DataEncoder.
Definition: DataEncoder.h++:92
Endianness
Byte endianness.
Definition: Common.h++:321
A manipulator that specifies the endianness of integer values to be subsequently encoded/decoded.
Definition: DataEncoder.h++:137
Definition: AllocationMap.c++:25
A DataEncoder context.
Definition: DataEncoder.h++:399
An abstract class with basic functionality that is common to all data decoders (readers) and data enc...
Definition: DataEncoder.h++:74