Athena IO Library
DNA.hpp
1 #ifndef DNA_HPP
2 #define DNA_HPP
3 
4 /* BIG FAT WARNING!!!
5  *
6  * The type-structure of this file is expected to remain consistent for 'atdna'
7  * Any changes to the types or namespacing must be reflected in 'atdna/main.cpp'
8  */
9 
10 #include "Global.hpp"
11 #include "IStreamReader.hpp"
12 #include "IStreamWriter.hpp"
13 #include <vector>
14 #include <memory>
15 
16 namespace athena
17 {
18 namespace io
19 {
20 
21 /* forward-declaration dance for recursively-derived types */
22 
23 template <size_t sizeVar, Endian VE>
24 struct Buffer;
25 
26 template <atInt32 sizeVar, Endian VE>
27 struct String;
28 
29 template <atInt32 sizeVar, Endian VE>
30 struct WString;
31 
32 template <atInt32 sizeVar, Endian VE>
34 
44 template <Endian DNAE>
45 struct DNA
46 {
47  virtual ~DNA() {}
48 
52  virtual void read(IStreamReader&)=0;
56  virtual void write(IStreamWriter&) const=0;
62  virtual size_t binarySize(size_t __isz) const=0;
63 
69  template <typename T, Endian VE = DNAE>
70  using Value = T;
71 
78  template <typename T, size_t cntVar, Endian VE = DNAE>
79  using Vector = std::vector<T>;
80 
86  template <size_t sizeVar>
87  using Buffer = struct athena::io::Buffer<sizeVar, DNAE>;
88 
94  template <atInt32 sizeVar = -1>
95  using String = struct athena::io::String<sizeVar, DNAE>;
96 
102  template <atInt32 sizeVar = -1, Endian VE = DNAE>
103  using WString = struct athena::io::WString<sizeVar, VE>;
104 
110  template <atInt32 sizeVar = -1>
111  using WStringAsString = struct athena::io::WStringAsString<sizeVar, DNAE>;
112 
118  template <off_t offset, SeekOrigin direction>
119  struct Seek {};
120 
125  template <size_t align>
126  struct Align {};
127 
131  struct Delete {};
132 
139  template <typename T>
140  static size_t __EnumerateSize(size_t __isz, const T& v)
141  {
142  for (const auto& val : v)
143  __isz = val.binarySize(__isz);
144  return __isz;
145  }
146 };
147 
151 template <size_t sizeVar, Endian VE>
152 struct Buffer : public DNA<VE>, public std::unique_ptr<atUint8[]>
153 {
154  typename DNA<VE>::Delete expl;
155  void read(IStreamReader& reader)
156  {
157  reset(new atUint8[sizeVar]);
158  reader.readUBytesToBuf(get(), sizeVar);
159  }
160  void write(IStreamWriter& writer) const
161  {
162  writer.writeUBytes(get(), sizeVar);
163  }
164  size_t binarySize(size_t __isz) const
165  {
166  return __isz + sizeVar;
167  }
168 };
169 
173 template <atInt32 sizeVar, Endian VE>
174 struct String : public DNA<VE>, public std::string
175 {
176  typename DNA<VE>::Delete expl;
177  void read(IStreamReader& reader)
178  {this->assign(std::move(reader.readString(sizeVar)));}
179  void write(IStreamWriter& writer) const
180  {writer.writeString(*this, sizeVar);}
181  size_t binarySize(size_t __isz) const
182  {return __isz + ((sizeVar<0)?(this->size()+1):sizeVar);}
183  std::string& operator=(const std::string& __str)
184  {return this->assign(__str);}
185  std::string& operator=(std::string&& __str)
186  {this->swap(__str); return *this;}
187 };
188 
192 template <atInt32 sizeVar, Endian VE>
193 struct WString : public DNA<VE>, public std::wstring
194 {
195  typename DNA<VE>::Delete expl;
196  void read(IStreamReader& reader)
197  {
198  reader.setEndian(VE);
199  this->assign(std::move(reader.readWString(sizeVar)));
200  }
201  void write(IStreamWriter& writer) const
202  {
203  writer.setEndian(VE);
204  writer.writeWString(*this, sizeVar);
205  }
206  size_t binarySize(size_t __isz) const
207  {return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
208  std::wstring& operator=(const std::wstring& __str)
209  {return this->assign(__str);}
210  std::wstring& operator=(std::wstring&& __str)
211  {this->swap(__str); return *this;}
212 };
213 
217 template <atInt32 sizeVar, Endian VE>
218 struct WStringAsString : public DNA<VE>, public std::string
219 {
220  typename DNA<VE>::Delete expl;
221  void read(IStreamReader& reader)
222  {*this = reader.readWStringAsString(sizeVar);}
223  void write(IStreamWriter& writer) const
224  {writer.writeStringAsWString(*this, sizeVar);}
225  size_t binarySize(size_t __isz) const
226  {return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
227  std::string& operator=(const std::string& __str)
228  {return this->assign(__str);}
229  std::string& operator=(std::string&& __str)
230  {this->swap(__str); return *this;}
231 };
232 
234 #define DECL_DNA \
235  void read(athena::io::IStreamReader&); \
236  void write(athena::io::IStreamWriter&) const; \
237  size_t binarySize(size_t __isz) const;
238 
240 #define DECL_EXPLICIT_DNA \
241  void read(athena::io::IStreamReader&); \
242  void write(athena::io::IStreamWriter&) const; \
243  size_t binarySize(size_t __isz) const; \
244  Delete __dna_delete;
245 
247 #ifdef __clang__
248 #define DNA_COUNT(cnt) sizeof(cnt)
249 #else
250 #define DNA_COUNT(cnt) 0
251 #endif
252 
253 }
254 }
255 
256 #endif // DNA_HPP
257 
virtual size_t binarySize(size_t __isz) const =0
Common virtual binary size computation for all DNA types.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
Definition: DNA.hpp:201
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
Definition: DNA.hpp:206
Meta Template preventing atdna from emitting read/write implementations.
Definition: DNA.hpp:131
std::string readWStringAsString(atInt32 fixedLen=-1)
Reads a wide-char string (using endianness from setEndian), converts to UTF8 and advances the positio...
std::wstring readWString(atInt32 fixedLen=-1)
Reads a wstring and advances the position in the file.
virtual void write(IStreamWriter &) const =0
Common virtual write function for all DNA types.
void writeWString(const std::wstring &str, atInt32 fixedLen=-1)
Writes an wstring to the buffer and advances the buffer.
Base DNA class used against &#39;atdna&#39;.
Definition: DNA.hpp:45
virtual void writeUBytes(const atUint8 *data, atUint64 len)=0
Writes the given buffer with the specified length, buffers can be bigger than the length however it&#39;s...
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
Definition: DNA.hpp:181
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
Definition: DNA.hpp:221
Concrete converting-wstring type used by DNA::WStringAsString.
Definition: DNA.hpp:33
The IStreamReader class defines a basic API for reading from streams, Implementors are provided with ...
Concrete buffer type used by DNA::Buffer.
Definition: DNA.hpp:24
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
Definition: DNA.hpp:155
virtual void read(IStreamReader &)=0
Common virtual read function for all DNA types.
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
Definition: DNA.hpp:164
void writeStringAsWString(const std::string &str, atInt32 fixedLen=-1)
Converts a UTF8 string to a wide-char string in the buffer and advances the buffer. It also swaps the bytes depending on the platform and Stream settings.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
Definition: DNA.hpp:223
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
Definition: DNA.hpp:225
Concrete wstring type used by DNA::WString.
Definition: DNA.hpp:30
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
Definition: DNA.hpp:177
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
Definition: DNA.hpp:160
std::string readString(atInt32 fixedLen=-1)
Reads a string and advances the position in the file.
Meta Template signaling atdna to insert an aligning stream seek where it&#39;s used.
Definition: DNA.hpp:126
Meta Template signaling atdna to insert a stream seek where it&#39;s used.
Definition: DNA.hpp:119
T Value
Template type signaling atdna to capture the value where it&#39;s used.
Definition: DNA.hpp:70
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
Definition: DNA.hpp:179
void writeString(const std::string &str, atInt32 fixedLen=-1)
Writes an string to the buffer and advances the buffer.
Concrete string type used by DNA::String.
Definition: DNA.hpp:27
std::vector< T > Vector
Template type wrapping std::vector and signaling atdna to manipulate it where it&#39;s used...
Definition: DNA.hpp:79
virtual atUint64 readUBytesToBuf(void *buf, atUint64 len)=0
Attempts to read a fixed length of data into a pre-allocated buffer, this function is client defined ...
static size_t __EnumerateSize(size_t __isz, const T &v)
Internal DNA helper for accumulating binarySize.
Definition: DNA.hpp:140
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
Definition: DNA.hpp:196