Skip to content

Commit 09d790b

Browse files
xyz347Leroy.H.Y
andauthored
Qt (#25)
* support qt * long/unsigned long for macOS * support max 99 members Co-authored-by: Leroy.H.Y <gh@lhy0403.top>
1 parent f2200a5 commit 09d790b

File tree

15 files changed

+539
-52
lines changed

15 files changed

+539
-52
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.user

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ x2struct
2020
* [customize type](#customize-type)
2121
* [thirdparty class](#thirdparty-class)
2222
* [Format indent](#format-indent)
23+
* [Qt support](#qt-support)
2324
* [xml bson libconfig](#xml-bson-libconfig)
2425
* [Generate Golang struct](#generate-golang-struct)
2526
* [IMPORTANT](#important)
@@ -367,6 +368,10 @@ Format indent
367368
----
368369
- last two parameters of tojson control format indent
369370
371+
Qt support
372+
----
373+
- modify config.h to enable macro XTOSTRUCT_QT
374+
- support QString/QList/QMap/QVector now
370375
371376
thirdparty class
372377
----

README.zh-cn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ x2struct
1818
* [自定义类型](#自定义类型)
1919
* [第三方类和结构体](#第三方类和结构体)
2020
* [格式化缩进](#格式化缩进)
21+
* [Qt支持](#qt支持)
2122
* [xml bson libconfig](#xml-bson-libconfig)
2223
* [生成Golang结构体](#生成golang结构体)
2324
* [重要说明](#重要说明)
@@ -416,6 +417,11 @@ int main(int argc, char *argv[]) {
416417
- tojson的最后两个参数控制
417418

418419

420+
Qt支持
421+
----
422+
- 修改config.h,开启XTOSTRUCT_QT这个宏
423+
- 当前支持 QString/QMap/QList/QVector
424+
419425
xml bson libconfig
420426
----
421427
- 如果需要这些功能,需要修改[config.h](config.h)来开启

bson_writer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@
3131
#include "util.h"
3232
#include "xtypes.h"
3333
#include "traits.h"
34+
#include "xwriter.h"
3435

3536
struct _bson_t;
3637

3738
namespace x2struct {
3839

39-
class BsonWriter {
40+
class BsonWriter:public XWriter<BsonWriter> {
4041
enum {
4142
top,
4243
doc,
4344
array
4445
};
4546
public:
47+
friend class XWriter<BsonWriter>;
48+
using xdoc_type::convert;
49+
4650
BsonWriter(const char*key="", _bson_t*parent=0, int type=top) {
4751
_parent = parent;
4852
_bson = new bson_t;
@@ -252,11 +256,6 @@ class BsonWriter {
252256
}
253257
return *this;
254258
}
255-
256-
template <typename T>
257-
void convert(const char*key, const XType<T>& data) {
258-
data.__struct_to_str(*this, key);
259-
}
260259
private:
261260
mutable _bson_t* _parent;
262261
mutable _bson_t* _bson;

config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
#endif
1717
#endif
1818

19+
// support qt
20+
//#define XTOSTRUCT_QT
21+
1922
#endif

config_writer.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@
3030
#include "util.h"
3131
#include "xtypes.h"
3232
#include "traits.h"
33+
#include "xwriter.h"
3334

3435
#define LIBCONFIG_BUFFER_SIZE 1024
3536
#define LIBCONFIG_TYPE_OBJECT 0
3637
#define LIBCONFIG_TYPE_ARRAY 1
3738

3839
namespace x2struct {
3940

40-
class ConfigWriter {
41+
class ConfigWriter:public XWriter<ConfigWriter> {
4142
public:
43+
friend class XWriter<ConfigWriter>;
44+
using xdoc_type::convert;
45+
4246
ConfigWriter(int indentCount=0, char indentChar=' '):_indentCount(indentCount),_indentChar(indentChar) {
4347
if (_indentCount > 0) {
4448
if (_indentChar!=' ' && _indentChar!='\t') {
@@ -148,12 +152,6 @@ class ConfigWriter {
148152

149153
return *this;
150154
}
151-
#ifdef XTOSTRUCT_SUPPORT_CHAR_ARRAY
152-
ConfigWriter& convert(const char*key, const char val[]) {
153-
std::string str(val);
154-
return this->convert(key, str);
155-
}
156-
#endif
157155
ConfigWriter& convert(const char*key, bool val) {
158156
indent();
159157
x2struct_set_key(key);
@@ -321,12 +319,6 @@ class ConfigWriter {
321319
data.__struct_to_str(*this, key);
322320
this->object_end();
323321
}
324-
325-
template <typename T>
326-
void convert(const char*key, const XType<T>& data) {
327-
data.__struct_to_str(*this, key);
328-
}
329-
330322
private:
331323
void append(const char* str, int len) {
332324
if (len < 0) {

json_writer.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929

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

3334
namespace x2struct {
3435

35-
class JsonWriter {
36+
class JsonWriter:public XWriter<JsonWriter> {
3637
typedef rapidjson::StringBuffer JSON_WRITER_BUFFER;
3738
typedef rapidjson::Writer<rapidjson::StringBuffer> JSON_WRITER_WRITER;
3839
typedef rapidjson::PrettyWriter<rapidjson::StringBuffer> JSON_WRITER_PRETTY;
3940
public:
41+
friend class XWriter<JsonWriter>;
42+
using xdoc_type::convert;
43+
4044
JsonWriter(int indentCount=0, char indentChar=' ') {
4145
_buf = new JSON_WRITER_BUFFER;
4246
if (indentCount < 0) {
@@ -115,12 +119,6 @@ class JsonWriter {
115119
}
116120
return *this;
117121
}
118-
#ifdef XTOSTRUCT_SUPPORT_CHAR_ARRAY
119-
JsonWriter& convert(const char*key, const char val[]) {
120-
std::string str(val);
121-
return this->convert(key, str);
122-
}
123-
#endif
124122
JsonWriter& convert(const char*key, bool val) {
125123
x2struct_set_key(key);
126124
if (0 != _writer) {
@@ -301,12 +299,6 @@ class JsonWriter {
301299
data.__struct_to_str(*this, key);
302300
this->object_end();
303301
}
304-
305-
template <typename T>
306-
void convert(const char*key, const XType<T>& data) {
307-
data.__struct_to_str(*this, key);
308-
}
309-
310302
private:
311303
JSON_WRITER_BUFFER* _buf;
312304
JSON_WRITER_WRITER* _writer;

test/example.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <x2struct.hpp>
2323
//#include <xtypes.h>
2424

25+
#ifdef XTOSTRUCT_QT
26+
#include <QString>
27+
#endif
28+
2529
using namespace std;
2630
using namespace x2struct;
2731

@@ -59,7 +63,11 @@ struct xstruct {
5963
int id;
6064
XDate start;
6165
int tint;
66+
#ifdef XTOSTRUCT_QT
67+
QString tstring;
68+
#else
6269
string tstring;
70+
#endif
6371
char chArray[16];
6472
#ifdef X_SUPPORT_C0X
6573
std::shared_ptr<SharePtr> sp;

test/gtest_stub.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <vector>
2323
#include <iostream>
2424

25+
#ifdef XTOSTRUCT_QT
26+
#include <QString>
27+
#endif
28+
2529
typedef void(*test_case)();
2630

2731
struct text_ctx {
@@ -65,14 +69,29 @@ class Status {
6569
}
6670
};
6771

72+
class COUT {
73+
public:
74+
template <class TYPE>
75+
static void out(const TYPE &d) {
76+
std::cout<<d;
77+
}
78+
#ifdef XTOSTRUCT_QT
79+
static void out(const QString &d) {
80+
std::cout<<d.toStdString();
81+
}
82+
#endif
83+
};
84+
6885
#define TEST(a, b) static void a##_##b(); static AUTO_ADD_TC __aat__##a##_##b(a##_##b, #a, #b); static void a##_##b()
6986

7087
#define EXPECT_EQ(a,b) \
7188
do {\
7289
if (!((a)==(b))) {\
7390
std::cout<<std::endl<<"++++++++++"<<__FILE__<<':'<<__LINE__<<'['<<__FUNCTION__<<']'<<"EXPECT_EQ fail."<<std::endl;\
7491
std::cout<<"expect:"<<b<<std::endl;\
75-
std::cout<<"actual:"<<a<<std::endl;\
92+
std::cout<<"actual:";\
93+
COUT::out(a);\
94+
std::cout<<std::endl;\
7695
++Status::c();\
7796
}\
7897
}while(false)

test/makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ check:
3939
./$@
4040
@-rm $@
4141

42+
checkqt:
43+
$(GPP) -o $@ -Wextra -Wall check.cpp $(INC) $(LIBCONFIG) $(LIBBSON) -DXTOSTRUCT_QT -I /usr/include/qt4 -I /usr/include/qt4/QtCore -lQtCore
44+
./$@
45+
@-rm $@
46+
4247
checkc11:
4348
$(GPP) -o $@ -fsanitize=address -Wextra -Wall check.cpp $(INC) $(LIBCONFIG) $(LIBBSON) -std=c++11
4449
./$@

0 commit comments

Comments
 (0)