From c2be09e62d5961a64dde40c32a4d19a7fe5bccd8 Mon Sep 17 00:00:00 2001 From: Bruno Ribeiro Date: Wed, 30 Jan 2019 17:30:27 -0200 Subject: [PATCH] Move static code to 'base.cc' and change version --- CMakeLists.txt | 2 +- library/cpp/base.cc | 40 ++++++++++++++++++++++++++++++++- library/cpp/cppgen.cc | 52 +------------------------------------------ 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e4c853f..f1727ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8) set(PROTOGEN_MAJOR 0) set(PROTOGEN_MINOR 5) -set(PROTOGEN_PATCH 0) +set(PROTOGEN_PATCH 1) add_definitions(-DPROTOGEN_MAJOR=${PROTOGEN_MAJOR}) add_definitions(-DPROTOGEN_MINOR=${PROTOGEN_MINOR}) diff --git a/library/cpp/base.cc b/library/cpp/base.cc index 3298ca7..f2e196d 100644 --- a/library/cpp/base.cc +++ b/library/cpp/base.cc @@ -7,6 +7,44 @@ #include #include + +#define PROTOGEN_TRAIT_MACRO(MSGTYPE) \ + namespace protogen { \ + template<> struct traits { \ + static void clear( MSGTYPE &value ) { value.clear(); } \ + static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \ + template static bool read( protogen::InputStream &in, MSGTYPE &value ) { return value.deserialize(in); } \ + static void swap( MSGTYPE &a, MSGTYPE &b ) { a.swap(b); } \ + }; \ + } + +#if __cplusplus >= 201103L +#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) Field( Field &&that ) { this->value_.swap(that.value_); } +#else +#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) +#endif + +#define PROTOGEN_FIELD_TEMPLATE(MSGTYPE) \ + namespace protogen { \ + template<> class Field { \ + protected: \ + MSGTYPE value_; \ + public: \ + Field() { clear(); } \ + PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \ + void swap( Field &that ) { traits::swap(this->value_, that.value_); } \ + const MSGTYPE &operator()() const { return value_; } \ + MSGTYPE &operator()() { return value_; } \ + void operator ()(const MSGTYPE &value ) { this->value_ = value; } \ + bool undefined() const { return value_.undefined(); } \ + void clear() { traits::clear(value_); } \ + Field &operator=( const Field &that ) { this->value_ = that.value_; return *this; } \ + bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \ + bool operator==( const Field &that ) const { return this->value_ == that.value_; } \ + }; \ + } + + namespace protogen { #ifndef PROTOGEN_FIELD_TYPES @@ -400,7 +438,7 @@ template class Field #if __cplusplus >= 201103L Field( Field &&that ) { this->undefined_ = that.undefined_; if (!undefined_) this->value_.swap(that.value_); } #endif - void swap( Field &that ) { traits::swap(this->value_, that.value_); } + void swap( Field &that ) { traits::swap(this->value_, that.value_); traits::swap(this->undefined_, that.undefined_); } const T &operator()() const { return value_; } void operator ()(const T &value ) { this->value_ = value; this->undefined_ = false; } bool undefined() const { return undefined_; } diff --git a/library/cpp/cppgen.cc b/library/cpp/cppgen.cc index b139b9b..a1b2f9c 100644 --- a/library/cpp/cppgen.cc +++ b/library/cpp/cppgen.cc @@ -333,7 +333,7 @@ static void generateDeserializer( GeneratorContext &ctx, const Message &message "else\n" "// ignore the current field\n" "{\n\tif (!protogen::json::ignore(in)) return false;\n" - "if (!protogen::json::next(in)) return false;\n\b}\n\b\b}\n"); + "if (!protogen::json::next(in)) return false;\n\b}\n\b}\n"); ctx.printer("if (required && ("); for (size_t i = 0, t = message.fields.size(); i < t; ++i) @@ -382,21 +382,6 @@ static void generateSerializer( GeneratorContext &ctx, const Message &message ) } -static void generateTraitMacro( GeneratorContext &ctx ) -{ - ctx.printer( - "\n#define PROTOGEN_TRAIT_MACRO(MSGTYPE) \\\n" - "\tnamespace protogen { \\\n" - "\ttemplate<> struct traits { \\\n" - "\tstatic void clear( MSGTYPE &value ) { value.clear(); } \\\n" - "static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \\\n" - "template \\\n" - "static bool read( protogen::InputStream &in, MSGTYPE &value ) { return value.deserialize(in); } \\\n" - "static void swap( MSGTYPE &a, MSGTYPE &b ) { a.swap(b); } \\\n" - "\b}; \\\n\b} \n\b\b"); -} - - static void generateTrait( GeneratorContext &ctx, const Message &message ) { ctx.printer( @@ -448,37 +433,6 @@ static void generateNamespace( GeneratorContext &ctx, const Message &message, bo } -static void generateFieldTemplateMacro( GeneratorContext &ctx ) -{ - ctx.printer( - "\n#if __cplusplus >= 201103L\n" - "#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) Field( Field &&that ) { this->value_.swap(that.value_); }\n" - "#else\n" - "#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) \n" - "#endif\n"); - - ctx.printer( - "\n#define PROTOGEN_FIELD_TEMPLATE(MSGTYPE) \\\n" - "\tnamespace protogen {" - "template<> class Field { \\\n" - "\tprotected: \\\n" - "\tMSGTYPE value_; \\\n" - "\bpublic: \\\n" - "\tField() { clear(); } \\\n" - "PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \\\n" - "void swap( Field &that ) { traits::swap(this->value_, that.value_); } \\\n" - "const MSGTYPE &operator()() const { return value_; } \\\n" - "MSGTYPE &operator()() { return value_; } \\\n" - "void operator ()(const MSGTYPE &value ) { this->value_ = value; } \\\n" - "bool undefined() const { return value_.undefined(); } \\\n" - "void clear() { traits::clear(value_); } \\\n" - "Field &operator=( const Field &that ) { this->value_ = that.value_; return *this; } \\\n" - "bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \\\n" - "bool operator==( const Field &that ) const { return this->value_ == that.value_; } \\\n" - "\b\b}; }\n\b"); -} - - static void generateFieldTemplate( GeneratorContext &ctx, const Message &message ) { ctx.printer( @@ -667,10 +621,6 @@ static void generateModel( GeneratorContext &ctx ) // base template ctx.printer.output() << BASE_TEMPLATE; - // macros for custom templates - generateTraitMacro(ctx); - generateFieldTemplateMacro(ctx); - // forward declarations ctx.printer("\n// forward declarations\n"); for (auto mi = ctx.root.messages.begin(); mi != ctx.root.messages.end(); ++mi)