Skip to content

Commit c2be09e

Browse files
author
Bruno Ribeiro
committed
Move static code to 'base.cc' and change version
1 parent bc151e8 commit c2be09e

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8)
44

55
set(PROTOGEN_MAJOR 0)
66
set(PROTOGEN_MINOR 5)
7-
set(PROTOGEN_PATCH 0)
7+
set(PROTOGEN_PATCH 1)
88

99
add_definitions(-DPROTOGEN_MAJOR=${PROTOGEN_MAJOR})
1010
add_definitions(-DPROTOGEN_MINOR=${PROTOGEN_MINOR})

library/cpp/base.cc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,44 @@
77
#include <cstdlib>
88
#include <locale.h>
99

10+
11+
#define PROTOGEN_TRAIT_MACRO(MSGTYPE) \
12+
namespace protogen { \
13+
template<> struct traits<MSGTYPE> { \
14+
static void clear( MSGTYPE &value ) { value.clear(); } \
15+
static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \
16+
template<typename I> static bool read( protogen::InputStream<I> &in, MSGTYPE &value ) { return value.deserialize(in); } \
17+
static void swap( MSGTYPE &a, MSGTYPE &b ) { a.swap(b); } \
18+
}; \
19+
}
20+
21+
#if __cplusplus >= 201103L
22+
#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) Field( Field<MSGTYPE> &&that ) { this->value_.swap(that.value_); }
23+
#else
24+
#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE)
25+
#endif
26+
27+
#define PROTOGEN_FIELD_TEMPLATE(MSGTYPE) \
28+
namespace protogen { \
29+
template<> class Field<MSGTYPE> { \
30+
protected: \
31+
MSGTYPE value_; \
32+
public: \
33+
Field() { clear(); } \
34+
PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \
35+
void swap( Field<MSGTYPE> &that ) { traits<MSGTYPE>::swap(this->value_, that.value_); } \
36+
const MSGTYPE &operator()() const { return value_; } \
37+
MSGTYPE &operator()() { return value_; } \
38+
void operator ()(const MSGTYPE &value ) { this->value_ = value; } \
39+
bool undefined() const { return value_.undefined(); } \
40+
void clear() { traits<MSGTYPE>::clear(value_); } \
41+
Field<MSGTYPE> &operator=( const Field<MSGTYPE> &that ) { this->value_ = that.value_; return *this; } \
42+
bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \
43+
bool operator==( const Field<MSGTYPE> &that ) const { return this->value_ == that.value_; } \
44+
}; \
45+
}
46+
47+
1048
namespace protogen {
1149

1250
#ifndef PROTOGEN_FIELD_TYPES
@@ -400,7 +438,7 @@ template<typename T> class Field
400438
#if __cplusplus >= 201103L
401439
Field( Field<T> &&that ) { this->undefined_ = that.undefined_; if (!undefined_) this->value_.swap(that.value_); }
402440
#endif
403-
void swap( Field<T> &that ) { traits<T>::swap(this->value_, that.value_); }
441+
void swap( Field<T> &that ) { traits<T>::swap(this->value_, that.value_); traits<bool>::swap(this->undefined_, that.undefined_); }
404442
const T &operator()() const { return value_; }
405443
void operator ()(const T &value ) { this->value_ = value; this->undefined_ = false; }
406444
bool undefined() const { return undefined_; }

library/cpp/cppgen.cc

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static void generateDeserializer( GeneratorContext &ctx, const Message &message
333333
"else\n"
334334
"// ignore the current field\n"
335335
"{\n\tif (!protogen::json::ignore(in)) return false;\n"
336-
"if (!protogen::json::next(in)) return false;\n\b}\n\b\b}\n");
336+
"if (!protogen::json::next(in)) return false;\n\b}\n\b}\n");
337337

338338
ctx.printer("if (required && (");
339339
for (size_t i = 0, t = message.fields.size(); i < t; ++i)
@@ -382,21 +382,6 @@ static void generateSerializer( GeneratorContext &ctx, const Message &message )
382382
}
383383

384384

385-
static void generateTraitMacro( GeneratorContext &ctx )
386-
{
387-
ctx.printer(
388-
"\n#define PROTOGEN_TRAIT_MACRO(MSGTYPE) \\\n"
389-
"\tnamespace protogen { \\\n"
390-
"\ttemplate<> struct traits<MSGTYPE> { \\\n"
391-
"\tstatic void clear( MSGTYPE &value ) { value.clear(); } \\\n"
392-
"static void write( std::ostream &out, const MSGTYPE &value ) { value.serialize(out); } \\\n"
393-
"template<typename I> \\\n"
394-
"static bool read( protogen::InputStream<I> &in, MSGTYPE &value ) { return value.deserialize(in); } \\\n"
395-
"static void swap( MSGTYPE &a, MSGTYPE &b ) { a.swap(b); } \\\n"
396-
"\b}; \\\n\b} \n\b\b");
397-
}
398-
399-
400385
static void generateTrait( GeneratorContext &ctx, const Message &message )
401386
{
402387
ctx.printer(
@@ -448,37 +433,6 @@ static void generateNamespace( GeneratorContext &ctx, const Message &message, bo
448433
}
449434

450435

451-
static void generateFieldTemplateMacro( GeneratorContext &ctx )
452-
{
453-
ctx.printer(
454-
"\n#if __cplusplus >= 201103L\n"
455-
"#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) Field( Field<MSGTYPE> &&that ) { this->value_.swap(that.value_); }\n"
456-
"#else\n"
457-
"#define PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE) \n"
458-
"#endif\n");
459-
460-
ctx.printer(
461-
"\n#define PROTOGEN_FIELD_TEMPLATE(MSGTYPE) \\\n"
462-
"\tnamespace protogen {"
463-
"template<> class Field<MSGTYPE> { \\\n"
464-
"\tprotected: \\\n"
465-
"\tMSGTYPE value_; \\\n"
466-
"\bpublic: \\\n"
467-
"\tField() { clear(); } \\\n"
468-
"PROTOGEN_FIELD_MOVECTOR_TEMPLATE(MSGTYPE); \\\n"
469-
"void swap( Field<MSGTYPE> &that ) { traits<MSGTYPE>::swap(this->value_, that.value_); } \\\n"
470-
"const MSGTYPE &operator()() const { return value_; } \\\n"
471-
"MSGTYPE &operator()() { return value_; } \\\n"
472-
"void operator ()(const MSGTYPE &value ) { this->value_ = value; } \\\n"
473-
"bool undefined() const { return value_.undefined(); } \\\n"
474-
"void clear() { traits<MSGTYPE>::clear(value_); } \\\n"
475-
"Field<MSGTYPE> &operator=( const Field<MSGTYPE> &that ) { this->value_ = that.value_; return *this; } \\\n"
476-
"bool operator==( const MSGTYPE &that ) const { return this->value_ == that; } \\\n"
477-
"bool operator==( const Field<MSGTYPE> &that ) const { return this->value_ == that.value_; } \\\n"
478-
"\b\b}; }\n\b");
479-
}
480-
481-
482436
static void generateFieldTemplate( GeneratorContext &ctx, const Message &message )
483437
{
484438
ctx.printer(
@@ -667,10 +621,6 @@ static void generateModel( GeneratorContext &ctx )
667621
// base template
668622
ctx.printer.output() << BASE_TEMPLATE;
669623

670-
// macros for custom templates
671-
generateTraitMacro(ctx);
672-
generateFieldTemplateMacro(ctx);
673-
674624
// forward declarations
675625
ctx.printer("\n// forward declarations\n");
676626
for (auto mi = ctx.root.messages.begin(); mi != ctx.root.messages.end(); ++mi)

0 commit comments

Comments
 (0)