Skip to content

Commit

Permalink
Move static code to 'base.cc' and change version
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Ribeiro committed Jan 30, 2019
1 parent bc151e8 commit c2be09e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
40 changes: 39 additions & 1 deletion library/cpp/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@
#include <cstdlib>
#include <locale.h>


#define PROTOGEN_TRAIT_MACRO(MSGTYPE) \
namespace protogen { \
template<> struct traits<MSGTYPE> { \
static void clear( MSGTYPE &value ) { value.clear(); } \
static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \
template<typename I> static bool read( protogen::InputStream<I> &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<MSGTYPE> &&that ) { this->value_.swap(that.value_); }
#else
#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE)
#endif

#define PROTOGEN_FIELD_TEMPLATE(MSGTYPE) \
namespace protogen { \
template<> class Field<MSGTYPE> { \
protected: \
MSGTYPE value_; \
public: \
Field() { clear(); } \
PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \
void swap( Field<MSGTYPE> &that ) { traits<MSGTYPE>::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<MSGTYPE>::clear(value_); } \
Field<MSGTYPE> &operator=( const Field<MSGTYPE> &that ) { this->value_ = that.value_; return *this; } \
bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \
bool operator==( const Field<MSGTYPE> &that ) const { return this->value_ == that.value_; } \
}; \
}


namespace protogen {

#ifndef PROTOGEN_FIELD_TYPES
Expand Down Expand Up @@ -400,7 +438,7 @@ template<typename T> class Field
#if __cplusplus >= 201103L
Field( Field<T> &&that ) { this->undefined_ = that.undefined_; if (!undefined_) this->value_.swap(that.value_); }
#endif
void swap( Field<T> &that ) { traits<T>::swap(this->value_, that.value_); }
void swap( Field<T> &that ) { traits<T>::swap(this->value_, that.value_); traits<bool>::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_; }
Expand Down
52 changes: 1 addition & 51 deletions library/cpp/cppgen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<MSGTYPE> { \\\n"
"\tstatic void clear( MSGTYPE &value ) { value.clear(); } \\\n"
"static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \\\n"
"template<typename I> \\\n"
"static bool read( protogen::InputStream<I> &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(
Expand Down Expand Up @@ -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<MSGTYPE> &&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<MSGTYPE> { \\\n"
"\tprotected: \\\n"
"\tMSGTYPE value_; \\\n"
"\bpublic: \\\n"
"\tField() { clear(); } \\\n"
"PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \\\n"
"void swap( Field<MSGTYPE> &that ) { traits<MSGTYPE>::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<MSGTYPE>::clear(value_); } \\\n"
"Field<MSGTYPE> &operator=( const Field<MSGTYPE> &that ) { this->value_ = that.value_; return *this; } \\\n"
"bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \\\n"
"bool operator==( const Field<MSGTYPE> &that ) const { return this->value_ == that.value_; } \\\n"
"\b\b}; }\n\b");
}


static void generateFieldTemplate( GeneratorContext &ctx, const Message &message )
{
ctx.printer(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c2be09e

Please sign in to comment.