Thea
Codec.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2009
12 //
13 //============================================================================
14 
15 #ifndef __Thea_Codec_hpp__
16 #define __Thea_Codec_hpp__
17 
18 #include "Common.hpp"
19 #include "NamedObject.hpp"
20 #include <algorithm>
21 #include <array>
22 
23 namespace Thea {
24 
25 // Forward declarations
26 class BinaryInputStream;
27 class BinaryOutputStream;
28 
33 class THEA_API Codec : public virtual INamedObject
34 {
35  public:
36  THEA_DECL_SMART_POINTERS(Codec)
37 
38 
39  static intx const MAGIC_LENGTH = 8;
40 
42  typedef std::array<int8, (size_t)MAGIC_LENGTH> MagicString;
43 
48  struct BlockHeader
49  {
50  public:
52  static intx const SERIALIZED_LENGTH = MAGIC_LENGTH
53  + /* sizeof(data_size) = */ 8
54  + /* sizeof(custom) = */ 8
55  + /* reserved */ 8;
56 
57  MagicString magic;
58  uint64 data_size;
59  uint64 custom;
60 
62  BlockHeader(MagicString const & magic_ = zeroMagic(), uint64 data_size_ = 0)
63  : magic(magic_), data_size(data_size_), custom(0), header_pos(0) {}
64 
66  BlockHeader(std::string const & magic_, uint64 data_size_ = 0)
67  : magic(toMagic(magic_)), data_size(data_size_), custom(0), header_pos(0) {}
68 
70  BlockHeader(BinaryInputStream & in);
71 
73  void read(BinaryInputStream & in);
74 
76  void write(BinaryOutputStream & out) const;
77 
93  int64 markAndSkip(BinaryOutputStream & out);
94 
114  void calcAndWrite(BinaryOutputStream & out);
115 
116  private:
117  int64 header_pos;
118 
119  }; // struct BlockHeader
120 
122  virtual ~Codec() = 0;
123 
124  int8 THEA_ICALL setName(char const * s) { return false; /* codec name is read-only by default */ }
125 
127  virtual MagicString const & getMagic() const { static MagicString const ZERO = zeroMagic(); return ZERO; }
128 
130  bool operator==(Codec const & other) const { return typeid(*this) == typeid(other); }
131 
136  operator intx() const { return reinterpret_cast<intx>(&typeid(*this)); }
137 
139  static MagicString toMagic(std::string const & s)
140  {
141  MagicString m;
142  m.fill(0); // padding, if needed
143  std::copy_n(s.data(), std::min((size_t)MAGIC_LENGTH, s.length()), m.data());
144  return m;
145  }
146 
147  private:
149  static MagicString zeroMagic() { MagicString z; z.fill(0); return z; }
150 
151 }; // class Codec
152 
153 // Pure virtual destructor should have a body
154 inline Codec::~Codec() {}
155 
157 inline std::ostream &
158 operator<<(std::ostream & os, Codec const & codec)
159 {
160  return os << codec.getName() << " codec";
161 }
162 
164 class THEA_API CodecAuto : public Codec
165 {
166  public:
167  char const * THEA_ICALL getName() const { static char const * my_name = "Auto"; return my_name; }
168 };
169 
171 class THEA_API Codec_UNKNOWN : public Codec
172 {
173  public:
174  char const * THEA_ICALL getName() const { static char const * my_name = "Unknown"; return my_name; }
175 };
176 
177 } // namespace Thea
178 
179 #endif
static MagicString toMagic(std::string const &s)
Convenience function to convert a string literal to a magic string.
Definition: Codec.hpp:139
virtual ~Codec()=0
Destructor.
Definition: Codec.hpp:154
int8 setName(char const *s)
Set the name of the object.
Definition: Codec.hpp:124
std::ptrdiff_t intx
A signed integer suitable for indexing a structure held in memory.
Definition: Platform.hpp:161
char const * getName() const
Get the name of the object.
Definition: Codec.hpp:167
BlockHeader(std::string const &magic_, uint64 data_size_=0)
Construct with a magic string specified as a std::string, and a data block size.
Definition: Codec.hpp:66
BlockHeader(MagicString const &magic_=zeroMagic(), uint64 data_size_=0)
Construct with a magic string and a data block size.
Definition: Codec.hpp:62
Indicates that the appropriate codec should be autodetected.
Definition: Codec.hpp:164
Root namespace for the Thea library.
virtual MagicString const & getMagic() const
Get the magic string for the codec, if it has one (else a string of all zeros).
Definition: Codec.hpp:127
uint64 data_size
The size of the data block in bytes.
Definition: Codec.hpp:58
A header preceding a data block serialized using a codec.
Definition: Codec.hpp:48
uint64 custom
Additional custom data that can be optionally stored in the header.
Definition: Codec.hpp:59
STL namespace.
MagicString magic
A magic string identifying the codec used to serialize the data.
Definition: Codec.hpp:57
Interface for an object that has a name string.
Definition: NamedObject.hpp:23
virtual char const * getName() const =0
Get the name of the object.
A serialization codec.
Definition: Codec.hpp:33
Sequential or random access input from binary files/memory.
char const * getName() const
Get the name of the object.
Definition: Codec.hpp:174
Indicates that the codec is unknown.
Definition: Codec.hpp:171
std::ostream & operator<<(std::ostream &os, AffineTransformN< N, T > const &tr)
Pipe a textual representation of an affine transform to a std::ostream.
Sequential or random access output to binary files/memory.
std::array< int8,(size_t) MAGIC_LENGTH > MagicString
The type of the codec&#39;s magic string (an array of MAGIC_LENGTH bytes).
Definition: Codec.hpp:42
#define THEA_ICALL
Calling convention for member functions of abstract interface classes (for talking to shared librarie...
Definition: Platform.hpp:115
bool operator==(Codec const &other) const
Check if two codecs are equal.
Definition: Codec.hpp:130