Skip to content

Commit

Permalink
Qt (#25)
Browse files Browse the repository at this point in the history
* support qt

* long/unsigned long for macOS

* support max 99 members

Co-authored-by: Leroy.H.Y <[email protected]>
  • Loading branch information
xyz347 and Leroy.H.Y authored Jan 6, 2020
1 parent f2200a5 commit 09d790b
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.user
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ x2struct
* [customize type](#customize-type)
* [thirdparty class](#thirdparty-class)
* [Format indent](#format-indent)
* [Qt support](#qt-support)
* [xml bson libconfig](#xml-bson-libconfig)
* [Generate Golang struct](#generate-golang-struct)
* [IMPORTANT](#important)
Expand Down Expand Up @@ -367,6 +368,10 @@ Format indent
----
- last two parameters of tojson control format indent
Qt support
----
- modify config.h to enable macro XTOSTRUCT_QT
- support QString/QList/QMap/QVector now
thirdparty class
----
Expand Down
6 changes: 6 additions & 0 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ x2struct
* [自定义类型](#自定义类型)
* [第三方类和结构体](#第三方类和结构体)
* [格式化缩进](#格式化缩进)
* [Qt支持](#qt支持)
* [xml bson libconfig](#xml-bson-libconfig)
* [生成Golang结构体](#生成golang结构体)
* [重要说明](#重要说明)
Expand Down Expand Up @@ -416,6 +417,11 @@ int main(int argc, char *argv[]) {
- tojson的最后两个参数控制


Qt支持
----
- 修改config.h,开启XTOSTRUCT_QT这个宏
- 当前支持 QString/QMap/QList/QVector

xml bson libconfig
----
- 如果需要这些功能,需要修改[config.h](config.h)来开启
Expand Down
11 changes: 5 additions & 6 deletions bson_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@
#include "util.h"
#include "xtypes.h"
#include "traits.h"
#include "xwriter.h"

struct _bson_t;

namespace x2struct {

class BsonWriter {
class BsonWriter:public XWriter<BsonWriter> {
enum {
top,
doc,
array
};
public:
friend class XWriter<BsonWriter>;
using xdoc_type::convert;

BsonWriter(const char*key="", _bson_t*parent=0, int type=top) {
_parent = parent;
_bson = new bson_t;
Expand Down Expand Up @@ -252,11 +256,6 @@ class BsonWriter {
}
return *this;
}

template <typename T>
void convert(const char*key, const XType<T>& data) {
data.__struct_to_str(*this, key);
}
private:
mutable _bson_t* _parent;
mutable _bson_t* _bson;
Expand Down
3 changes: 3 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
#endif
#endif

// support qt
//#define XTOSTRUCT_QT

#endif
18 changes: 5 additions & 13 deletions config_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@
#include "util.h"
#include "xtypes.h"
#include "traits.h"
#include "xwriter.h"

#define LIBCONFIG_BUFFER_SIZE 1024
#define LIBCONFIG_TYPE_OBJECT 0
#define LIBCONFIG_TYPE_ARRAY 1

namespace x2struct {

class ConfigWriter {
class ConfigWriter:public XWriter<ConfigWriter> {
public:
friend class XWriter<ConfigWriter>;
using xdoc_type::convert;

ConfigWriter(int indentCount=0, char indentChar=' '):_indentCount(indentCount),_indentChar(indentChar) {
if (_indentCount > 0) {
if (_indentChar!=' ' && _indentChar!='\t') {
Expand Down Expand Up @@ -148,12 +152,6 @@ class ConfigWriter {

return *this;
}
#ifdef XTOSTRUCT_SUPPORT_CHAR_ARRAY
ConfigWriter& convert(const char*key, const char val[]) {
std::string str(val);
return this->convert(key, str);
}
#endif
ConfigWriter& convert(const char*key, bool val) {
indent();
x2struct_set_key(key);
Expand Down Expand Up @@ -321,12 +319,6 @@ class ConfigWriter {
data.__struct_to_str(*this, key);
this->object_end();
}

template <typename T>
void convert(const char*key, const XType<T>& data) {
data.__struct_to_str(*this, key);
}

private:
void append(const char* str, int len) {
if (len < 0) {
Expand Down
18 changes: 5 additions & 13 deletions json_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@

#include "xtypes.h"
#include "traits.h"
#include "xwriter.h"

namespace x2struct {

class JsonWriter {
class JsonWriter:public XWriter<JsonWriter> {
typedef rapidjson::StringBuffer JSON_WRITER_BUFFER;
typedef rapidjson::Writer<rapidjson::StringBuffer> JSON_WRITER_WRITER;
typedef rapidjson::PrettyWriter<rapidjson::StringBuffer> JSON_WRITER_PRETTY;
public:
friend class XWriter<JsonWriter>;
using xdoc_type::convert;

JsonWriter(int indentCount=0, char indentChar=' ') {
_buf = new JSON_WRITER_BUFFER;
if (indentCount < 0) {
Expand Down Expand Up @@ -115,12 +119,6 @@ class JsonWriter {
}
return *this;
}
#ifdef XTOSTRUCT_SUPPORT_CHAR_ARRAY
JsonWriter& convert(const char*key, const char val[]) {
std::string str(val);
return this->convert(key, str);
}
#endif
JsonWriter& convert(const char*key, bool val) {
x2struct_set_key(key);
if (0 != _writer) {
Expand Down Expand Up @@ -301,12 +299,6 @@ class JsonWriter {
data.__struct_to_str(*this, key);
this->object_end();
}

template <typename T>
void convert(const char*key, const XType<T>& data) {
data.__struct_to_str(*this, key);
}

private:
JSON_WRITER_BUFFER* _buf;
JSON_WRITER_WRITER* _writer;
Expand Down
8 changes: 8 additions & 0 deletions test/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <x2struct.hpp>
//#include <xtypes.h>

#ifdef XTOSTRUCT_QT
#include <QString>
#endif

using namespace std;
using namespace x2struct;

Expand Down Expand Up @@ -59,7 +63,11 @@ struct xstruct {
int id;
XDate start;
int tint;
#ifdef XTOSTRUCT_QT
QString tstring;
#else
string tstring;
#endif
char chArray[16];
#ifdef X_SUPPORT_C0X
std::shared_ptr<SharePtr> sp;
Expand Down
21 changes: 20 additions & 1 deletion test/gtest_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <vector>
#include <iostream>

#ifdef XTOSTRUCT_QT
#include <QString>
#endif

typedef void(*test_case)();

struct text_ctx {
Expand Down Expand Up @@ -65,14 +69,29 @@ class Status {
}
};

class COUT {
public:
template <class TYPE>
static void out(const TYPE &d) {
std::cout<<d;
}
#ifdef XTOSTRUCT_QT
static void out(const QString &d) {
std::cout<<d.toStdString();
}
#endif
};

#define TEST(a, b) static void a##_##b(); static AUTO_ADD_TC __aat__##a##_##b(a##_##b, #a, #b); static void a##_##b()

#define EXPECT_EQ(a,b) \
do {\
if (!((a)==(b))) {\
std::cout<<std::endl<<"++++++++++"<<__FILE__<<':'<<__LINE__<<'['<<__FUNCTION__<<']'<<"EXPECT_EQ fail."<<std::endl;\
std::cout<<"expect:"<<b<<std::endl;\
std::cout<<"actual:"<<a<<std::endl;\
std::cout<<"actual:";\
COUT::out(a);\
std::cout<<std::endl;\
++Status::c();\
}\
}while(false)
Expand Down
5 changes: 5 additions & 0 deletions test/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ check:
./$@
@-rm $@

checkqt:
$(GPP) -o $@ -Wextra -Wall check.cpp $(INC) $(LIBCONFIG) $(LIBBSON) -DXTOSTRUCT_QT -I /usr/include/qt4 -I /usr/include/qt4/QtCore -lQtCore
./$@
@-rm $@

checkc11:
$(GPP) -o $@ -fsanitize=address -Wextra -Wall check.cpp $(INC) $(LIBCONFIG) $(LIBBSON) -std=c++11
./$@
Expand Down
Loading

0 comments on commit 09d790b

Please sign in to comment.