diff options
author | John Hodge (sonata) <tpg@mutabah.net> | 2015-01-11 15:00:32 +0800 |
---|---|---|
committer | John Hodge (sonata) <tpg@mutabah.net> | 2015-01-11 15:00:32 +0800 |
commit | 86938c184b32ce004d5247ad80f924f0ae7a3c86 (patch) | |
tree | 66dfdee288a8f3806dd869a9bccb910b8c854ab6 /src/include/serialise.hpp | |
parent | d9cba0738c5fe7928ea345f510f505fe777fd8ea (diff) | |
download | mrust-86938c184b32ce004d5247ad80f924f0ae7a3c86.tar.gz |
Add hacky text output of AST
Diffstat (limited to 'src/include/serialise.hpp')
-rw-r--r-- | src/include/serialise.hpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/include/serialise.hpp b/src/include/serialise.hpp new file mode 100644 index 00000000..f4b86133 --- /dev/null +++ b/src/include/serialise.hpp @@ -0,0 +1,59 @@ +/* + */ +#ifndef _SERIALSE_HPP_INCLUDED_ +#define _SERIALSE_HPP_INCLUDED_ + +#include <vector> +#include <string> + +class Serialiser; + +#define SERIALISABLE_PROTOTYPES()\ + virtual const char* serialise_tag() const override; \ + virtual void serialise(::Serialiser& s) const override +#define SERIALISE_TYPE(method_prefix, tag_str, body) \ + const char* method_prefix serialise_tag() const { return tag_str; } \ + void method_prefix serialise(::Serialiser& s) const { body } + +class Serialisable +{ +public: + virtual const char* serialise_tag() const = 0; + virtual void serialise(Serialiser& s) const = 0; +}; + +class Serialiser +{ +protected: + virtual void start_object(const char *tag) = 0; + virtual void end_object(const char *tag) = 0; + virtual void start_array(unsigned int size) = 0; + virtual void end_array() = 0; +public: + virtual Serialiser& operator<<(bool val) = 0; + virtual Serialiser& operator<<(unsigned int val) = 0; + virtual Serialiser& operator<<(const ::std::string& s) = 0; + Serialiser& operator<<(const Serialisable& subobj); + + template<typename T> + Serialiser& operator<<(const ::std::vector<T>& v) + { + start_array(v.size()); + for(const auto& ent : v) + *this << ent; + end_array(); + return *this; + } + template<typename T1, typename T2> + Serialiser& operator<<(const ::std::pair<T1,T2>& v) + { + start_array(2); + *this << v.first; + *this << v.second; + end_array(); + return *this; + } +}; + +#endif + |