athena/include/Athena/DNA.hpp

137 lines
3.8 KiB
C++
Raw Normal View History

2015-06-15 02:49:02 +00:00
#ifndef DNA_HPP
#define DNA_HPP
2015-06-17 02:50:12 +00:00
/* BIG FAT WARNING!!!
*
* The type-structure of this file is expected to remain consistent for 'atdna'
* Any changes to the types or namespacing must be reflected in 'atdna/main.cpp'
*/
2015-06-16 02:29:53 +00:00
#include "Global.hpp"
#include "IStreamReader.hpp"
#include "IStreamWriter.hpp"
2015-06-15 02:49:02 +00:00
#include <vector>
#include <memory>
2015-06-15 02:49:02 +00:00
namespace Athena
{
namespace io
{
2015-06-16 02:29:53 +00:00
/**
* @brief Base DNA class used against 'atdna'
*
* Athena bundles a build-tool called 'atdna'. This tool functions
2015-06-17 02:50:12 +00:00
* just like the 'clang' compiler, except it emits a full .cpp implementation
* with all read/write calls necessary to marshal the DNA structure to/from
* a streamed medium
2015-06-16 02:29:53 +00:00
*/
2015-06-17 00:25:48 +00:00
template <Endian DNAE>
2015-06-15 02:49:02 +00:00
struct DNA
{
2015-07-07 03:23:37 +00:00
virtual void read(IStreamReader&)=0;
virtual void write(IStreamWriter&) const=0;
2015-06-16 07:11:56 +00:00
template <typename T, Endian VE = DNAE>
2015-06-16 02:29:53 +00:00
using Value = T;
2015-06-15 02:49:02 +00:00
2015-06-17 00:25:48 +00:00
template <typename T, size_t cntVar, Endian VE = DNAE>
2015-06-16 02:29:53 +00:00
using Vector = std::vector<T>;
2015-06-15 02:49:02 +00:00
2015-07-01 04:50:58 +00:00
struct Delete {};
template <size_t sizeVar>
struct Buffer : public DNA, public std::unique_ptr<atUint8[]>
{
Delete expl;
inline void read(IStreamReader& reader)
{
reset(new atUint8[sizeVar]);
reader.readUBytesToBuf(get(), sizeVar);
}
inline void write(IStreamWriter& writer) const
{
writer.writeUBytes(get(), sizeVar);
}
};
template <atInt32 sizeVar = -1>
struct String : public DNA, public std::string
{
Delete expl;
inline void read(IStreamReader& reader)
{*this = reader.readString(sizeVar);}
inline void write(IStreamWriter& writer) const
{writer.writeString(*this, sizeVar);}
inline std::string& operator=(const std::string& __str)
{return this->assign(__str);}
inline std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}
};
template <atInt32 sizeVar = -1, Endian VE = DNAE>
struct WString : public DNA, public std::wstring
{
Delete expl;
inline void read(IStreamReader& reader)
{
reader.setEndian(VE);
*this = reader.readWString(sizeVar);
}
inline void write(IStreamWriter& writer) const
{
writer.setEndian(VE);
writer.writeWString(*this, sizeVar);
}
inline std::wstring& operator=(const std::wstring& __str)
{return this->assign(__str);}
inline std::wstring& operator=(std::wstring&& __str)
{this->swap(__str); return *this;}
};
template <atInt32 sizeVar = -1>
struct UTF8 : public DNA, public std::string
{
Delete expl;
inline void read(IStreamReader& reader)
2015-07-01 09:28:40 +00:00
{*this = reader.readUnicode(sizeVar);}
inline void write(IStreamWriter& writer) const
2015-07-01 09:28:40 +00:00
{writer.writeUnicode(*this, sizeVar);}
inline std::string& operator=(const std::string& __str)
{return this->assign(__str);}
inline std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}
};
template <off_t offset, SeekOrigin direction>
struct Seek {};
template <size_t align>
struct Align {};
2015-06-16 02:29:53 +00:00
};
2015-06-15 02:49:02 +00:00
2015-06-17 00:25:48 +00:00
/** Macro to automatically declare read/write methods in subclasses */
#define DECL_DNA \
void read(Athena::io::IStreamReader&); \
void write(Athena::io::IStreamWriter&) const; \
/** Macro to automatically declare read/write methods and prevent outputting implementation */
#define DECL_EXPLICIT_DNA \
void read(Athena::io::IStreamReader&); \
void write(Athena::io::IStreamWriter&) const; \
Delete __dna_delete;
2015-06-20 04:40:15 +00:00
/** Macro to supply count variable to atdna and mute it for other compilers */
#ifdef __clang__
#define DNA_COUNT(cnt) sizeof(cnt)
#else
#define DNA_COUNT(cnt) 0
#endif
2015-06-15 02:49:02 +00:00
}
}
#endif // DNA_HPP