Skip to content

Commit 3dc87f1

Browse files
author
huangxiyuan
committed
support enum
1 parent dc2f719 commit 3dc87f1

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

bson_writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "util.h"
3131
#include "xtypes.h"
32+
#include "traits.h"
3233

3334
struct _bson_t;
3435

@@ -187,7 +188,7 @@ class BsonWriter {
187188
}
188189

189190
template <typename T>
190-
BsonWriter& convert(const char*key, const T& data, typename x_enable_if<(sizeof(T)>sizeof(int)), T>::type *p=0) {
191+
BsonWriter& convert(const char*key, const T& data, x_for_class(T) *p=0) {
191192
if (_type!=top || key[0]!='\0') {
192193
BsonWriter child(key, _bson, doc);
193194
data.__struct_to_str(child, "");

config_writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "util.h"
3030
#include "xtypes.h"
31+
#include "traits.h"
3132

3233
#define LIBCONFIG_BUFFER_SIZE 1024
3334
#define LIBCONFIG_TYPE_OBJECT 0
@@ -260,7 +261,7 @@ class ConfigWriter {
260261
}
261262

262263
template <typename T>
263-
void convert(const char*key, const T& data, typename x_enable_if<(sizeof(T)>sizeof(int)), T>::type *p=0) {
264+
void convert(const char*key, const T& data, x_for_class(T) *p=0) {
264265
indent();
265266
x2struct_set_key(key);
266267
this->object_begin();

json_writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "thirdparty/rapidjson/stringbuffer.h"
2828

2929
#include "xtypes.h"
30+
#include "traits.h"
3031

3132
namespace x2struct {
3233

@@ -245,7 +246,7 @@ class JsonWriter {
245246
}
246247

247248
template <typename T>
248-
void convert(const char*key, const T& data, typename x_enable_if<(sizeof(T)>sizeof(int)), T>::type *p=0) {
249+
void convert(const char*key, const T& data, x_for_class(T) *p=0) {
249250
x2struct_set_key(key);
250251
this->object_begin();
251252
data.__struct_to_str(*this, key);

traits.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2019 YY Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#ifndef __X_TO_TRAITS_H
19+
#define __X_TO_TRAITS_H
20+
21+
#if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700
22+
#include <type_traits>
23+
#endif
24+
25+
using namespace std;
26+
27+
#if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700
28+
//template <class T>
29+
//using x_for_class = typename std::enable_if<std::is_class<T>::value, int>::type;
30+
31+
//template <class T>
32+
//using x_for_enum = typename std::enable_if<std::is_enum<T>::value, int>::type;
33+
34+
// <c++11 not support using template. so we use macro all
35+
#define x_for_class(T) typename std::enable_if<std::is_class<T>::value, int>::type
36+
#define x_for_enum(T) typename std::enable_if<std::is_enum<T>::value, int>::type
37+
#else
38+
template<bool B, class T = void>
39+
struct x_enable_if {};
40+
41+
template <class T>
42+
struct x_enable_if<true, T> { typedef T type; };
43+
44+
45+
template <class T>
46+
struct x_tovoid {typedef void type;};
47+
48+
template <class T, typename=void>
49+
struct x_is_class{static bool const value = false;};
50+
51+
template <class T>
52+
struct x_is_class<T, typename x_tovoid<int T::*>::type >{static bool const value = true;};
53+
54+
55+
#define x_for_class(T) typename x_enable_if<x_is_class<T>::value, int>::type
56+
// too complicated to implement is_enum
57+
#define x_for_enum(T) typename x_enable_if<!x_is_class<T>::value, int>::type
58+
59+
#endif
60+
61+
#endif
62+

xml_writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "util.h"
2929
#include "xtypes.h"
30+
#include "traits.h"
3031

3132
#define X2STRUCT_BUFFER_SIZE 1024
3233
#define X2STRUCT_TYPE_OBJECT 0
@@ -251,7 +252,7 @@ class XmlWriter {
251252
}
252253

253254
template <typename T>
254-
void convert(const char*key, const T& data, typename x_enable_if<(sizeof(T)>sizeof(int)), T>::type *p=0) {
255+
void convert(const char*key, const T& data, x_for_class(T) *p=0) {
255256
XmlKey xkey(key, this, false);
256257
this->object_begin();
257258
data.__struct_to_str(*this, key);

xreader.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#endif
3333

3434
#include "util.h"
35+
#include "traits.h"
3536

3637
namespace x2struct {
3738

@@ -123,9 +124,9 @@ class XReader {
123124
return true;
124125
}
125126

126-
// because struct has a set, it's size is bigger than sizeof(int). add this to avoid enum hit this function
127+
// add x_for_class to avoid enum hit this function
127128
template <typename TYPE>
128-
bool convert(const char*key, TYPE& val, typename x_enable_if<(sizeof(TYPE)>sizeof(int)), TYPE>::type *p=0) {
129+
bool convert(const char*key, TYPE& val, x_for_class(TYPE) *p=0) {
129130
doc_type tmp;
130131
doc_type *obj = get_obj(key, &tmp);
131132
if (NULL == obj) {
@@ -149,7 +150,7 @@ class XReader {
149150

150151
// for enum
151152
template <typename TYPE>
152-
bool convert(const char*key, TYPE& val, typename x_enable_if<(sizeof(TYPE)==sizeof(int)), TYPE>::type *p=0) {
153+
bool convert(const char*key, TYPE& val, x_for_enum(TYPE) *p=0) {
153154
return ((doc_type*)(this))->convert(key, *(int*)&val);
154155
}
155156

0 commit comments

Comments
 (0)