diff --git a/app/gson/test/test1.bak/Makefile b/app/gson/test/test1.bak/Makefile new file mode 100644 index 000000000..f3f60643b --- /dev/null +++ b/app/gson/test/test1.bak/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.in +ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) + EXTLIBS += -L/usr/local/lib -liconv +endif +PROG = test diff --git a/app/gson/test/test1.bak/gson.cpp b/app/gson/test/test1.bak/gson.cpp new file mode 100644 index 000000000..d795e1c7e --- /dev/null +++ b/app/gson/test/test1.bak/gson.cpp @@ -0,0 +1,214 @@ +#include "stdafx.h" +#include "struct.h" +#include "gson.h" +#include "acl_cpp/serialize/gson_helper.ipp" +namespace acl +{ + acl::json_node& gson(acl::json &$json, const people &$obj) + { + acl::json_node &$node = $json.create_node(); + + if (check_nullptr($obj.aa)) + $node.add_null("aa"); + else + $node.add_text("aa", acl::get_value($obj.aa)); + + if (check_nullptr($obj.bb)) + $node.add_null("bb"); + else + $node.add_text("bb", acl::get_value($obj.bb)); + + + return $node; + } + + acl::json_node& gson(acl::json &$json, const people *$obj) + { + return gson ($json, *$obj); + } + + + acl::string gson(const people &$obj) + { + acl::json $json; + acl::json_node &$node = acl::gson ($json, $obj); + return $node.to_string (); + } + + + std::pair gson(acl::json_node &$node, people &$obj) + { + acl::json_node *aa = $node["aa"]; + acl::json_node *bb = $node["bb"]; + std::pair result; + + if(aa) + gson(*aa, &$obj.aa); + + if(bb) + gson(*bb, &$obj.bb); + + return std::make_pair(true,""); + } + + + std::pair gson(acl::json_node &$node, people *$obj) + { + return gson($node, *$obj); + } + + + acl::json_node& gson(acl::json &$json, const user &$obj) + { + acl::json_node &$node = $json.create_node(); + + if (check_nullptr($obj.type)) + $node.add_null("type"); + else + $node.add_child("type", acl::gson($json, $obj.type)); + + if (check_nullptr($obj.cmd)) + $node.add_null("cmd"); + else + $node.add_text("cmd", acl::get_value($obj.cmd)); + + if (check_nullptr($obj.user_list)) + $node.add_null("user_list"); + else + $node.add_child("user_list", acl::gson($json, $obj.user_list)); + + if (check_nullptr($obj.user_vector)) + $node.add_null("user_vector"); + else + $node.add_child("user_vector", acl::gson($json, $obj.user_vector)); + + if (check_nullptr($obj.user_map)) + $node.add_null("user_map"); + else + $node.add_child("user_map", acl::gson($json, $obj.user_map)); + + if (check_nullptr($obj.user_list_ptr)) + $node.add_null("user_list_ptr"); + else + $node.add_child("user_list_ptr", acl::gson($json, $obj.user_list_ptr)); + + if (check_nullptr($obj.user_vector_ptr)) + $node.add_null("user_vector_ptr"); + else + $node.add_child("user_vector_ptr", acl::gson($json, $obj.user_vector_ptr)); + + if (check_nullptr($obj.user_map_ptr)) + $node.add_null("user_map_ptr"); + else + $node.add_child("user_map_ptr", acl::gson($json, $obj.user_map_ptr)); + + if (check_nullptr($obj.n)) + $node.add_null("n"); + else + $node.add_number("n", acl::get_value($obj.n)); + + if (check_nullptr($obj.n1)) + $node.add_null("n1"); + else + $node.add_number("n1", acl::get_value($obj.n1)); + + if (check_nullptr($obj.n2)) + $node.add_null("n2"); + else + $node.add_number("n2", acl::get_value($obj.n2)); + + if (check_nullptr($obj.n3)) + $node.add_null("n3"); + else + $node.add_number("n3", acl::get_value($obj.n3)); + + if (check_nullptr($obj.u)) + $node.add_null("u"); + else + $node.add_child("u", acl::gson($json, $obj.u)); + + + return $node; + } + + acl::json_node& gson(acl::json &$json, const user *$obj) + { + return gson ($json, *$obj); + } + + + acl::string gson(const user &$obj) + { + acl::json $json; + acl::json_node &$node = acl::gson ($json, $obj); + return $node.to_string (); + } + + + std::pair gson(acl::json_node &$node, user &$obj) + { + acl::json_node *type = $node["type"]; + acl::json_node *cmd = $node["cmd"]; + acl::json_node *user_list = $node["user_list"]; + acl::json_node *user_vector = $node["user_vector"]; + acl::json_node *user_map = $node["user_map"]; + acl::json_node *user_list_ptr = $node["user_list_ptr"]; + acl::json_node *user_vector_ptr = $node["user_vector_ptr"]; + acl::json_node *user_map_ptr = $node["user_map_ptr"]; + acl::json_node *n = $node["n"]; + acl::json_node *n1 = $node["n1"]; + acl::json_node *n2 = $node["n2"]; + acl::json_node *n3 = $node["n3"]; + acl::json_node *u = $node["u"]; + std::pair result; + + if(!type ||!type->get_obj()||!(result = gson(*type->get_obj(), &$obj.type), result.first)) + return std::make_pair(false, "required [user.type] failed:{"+result.second+"}"); + + if(!cmd ||!(result = gson(*cmd, &$obj.cmd), result.first)) + return std::make_pair(false, "required [user.cmd] failed:{"+result.second+"}"); + + if(!user_list ||!user_list->get_obj()||!(result = gson(*user_list->get_obj(), &$obj.user_list), result.first)) + return std::make_pair(false, "required [user.user_list] failed:{"+result.second+"}"); + + if(!user_vector ||!user_vector->get_obj()||!(result = gson(*user_vector->get_obj(), &$obj.user_vector), result.first)) + return std::make_pair(false, "required [user.user_vector] failed:{"+result.second+"}"); + + if(!user_map ||!user_map->get_obj()||!(result = gson(*user_map->get_obj(), &$obj.user_map), result.first)) + return std::make_pair(false, "required [user.user_map] failed:{"+result.second+"}"); + + if(!user_list_ptr ||!user_list_ptr->get_obj()||!(result = gson(*user_list_ptr->get_obj(), &$obj.user_list_ptr), result.first)) + return std::make_pair(false, "required [user.user_list_ptr] failed:{"+result.second+"}"); + + if(!user_vector_ptr ||!user_vector_ptr->get_obj()||!(result = gson(*user_vector_ptr->get_obj(), &$obj.user_vector_ptr), result.first)) + return std::make_pair(false, "required [user.user_vector_ptr] failed:{"+result.second+"}"); + + if(!user_map_ptr ||!user_map_ptr->get_obj()||!(result = gson(*user_map_ptr->get_obj(), &$obj.user_map_ptr), result.first)) + return std::make_pair(false, "required [user.user_map_ptr] failed:{"+result.second+"}"); + + if(!n ||!(result = gson(*n, &$obj.n), result.first)) + return std::make_pair(false, "required [user.n] failed:{"+result.second+"}"); + + if(!n1 ||!(result = gson(*n1, &$obj.n1), result.first)) + return std::make_pair(false, "required [user.n1] failed:{"+result.second+"}"); + + if(!n2 ||!(result = gson(*n2, &$obj.n2), result.first)) + return std::make_pair(false, "required [user.n2] failed:{"+result.second+"}"); + + if(!n3 ||!(result = gson(*n3, &$obj.n3), result.first)) + return std::make_pair(false, "required [user.n3] failed:{"+result.second+"}"); + + if(u&& u->get_obj()) + gson(*u->get_obj(), &$obj.u); + + return std::make_pair(true,""); + } + + + std::pair gson(acl::json_node &$node, user *$obj) + { + return gson($node, *$obj); + } + + +}///end of acl. diff --git a/app/gson/test/test1.bak/gson.h b/app/gson/test/test1.bak/gson.h new file mode 100644 index 000000000..6c1f03ae7 --- /dev/null +++ b/app/gson/test/test1.bak/gson.h @@ -0,0 +1,15 @@ +namespace acl +{ + //people + acl::string gson(const people &$obj); + acl::json_node& gson(acl::json &$json, const people &$obj); + acl::json_node& gson(acl::json &$json, const people *$obj); + std::pair gson(acl::json_node &$node, people &$obj); + std::pair gson(acl::json_node &$node, people *$obj); + //user + acl::string gson(const user &$obj); + acl::json_node& gson(acl::json &$json, const user &$obj); + acl::json_node& gson(acl::json &$json, const user *$obj); + std::pair gson(acl::json_node &$node, user &$obj); + std::pair gson(acl::json_node &$node, user *$obj); +}///end of acl. diff --git a/app/gson/test/test1.bak/main.cpp b/app/gson/test/test1.bak/main.cpp new file mode 100644 index 000000000..a7a4090f0 --- /dev/null +++ b/app/gson/test/test1.bak/main.cpp @@ -0,0 +1,180 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include "struct.h" +#include "gson.h" + +static void print_msg(message& msg) +{ + printf("=======================================================\r\n"); + printf("type: %d\r\n", msg.type); + printf("cmd: %s\r\n", msg.cmd.c_str()); + + printf("-------------------- user list ------------------------\r\n"); + size_t i = 0; + for (auto cit : msg.user_list) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit.username.c_str(), cit.domain.c_str(), + cit.age, cit.male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------- user vector ----------------------\r\n"); + i = 0; + for (auto cit : msg.user_vector) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit.username.c_str(), cit.domain.c_str(), + cit.age, cit.male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("------------------- user map --------------------------\r\n"); + i = 0; + for (auto cit : msg.user_map) + { + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second.username.c_str(), + cit.second.domain.c_str(), + cit.second.age, + cit.second.male ? "true" : "false"); + + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second.get_username(), + cit.second.get_domain(), + cit.second.get_age(), + cit.second.is_male() ? "true" : "false"); + + if (++i >= 10) + break; + } + printf("-------------------- user list ptr --------------------\r\n"); + i = 0; + for (auto cit : *msg.user_list_ptr) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit->username.c_str(), cit->domain.c_str(), + cit->age, cit->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------- user vector ptr ------------------\r\n"); + i = 0; + for (auto cit : *msg.user_vector_ptr) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit->username.c_str(), cit->domain.c_str(), + cit->age, cit->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("------------------- user map ptr ----------------------\r\n"); + i = 0; + for (auto cit : *msg.user_map_ptr) + { + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second->username.c_str(), + cit.second->domain.c_str(), + cit.second->age, + cit.second->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------------------------------------------\r\n"); +} + +static void test1(void) +{ + message msg; + msg.user_list_ptr = new std::list; + msg.user_vector_ptr = new std::vector; + msg.user_map_ptr = new std::map; + + msg.type = 1; + msg.cmd = "add"; + + user u = {"zsx1", "263.net", 11, true}; + msg.user_list.push_back(u); + msg.user_list.emplace_back(u); + msg.user_list.emplace_back("zsx1", "263.net", 13, false); + + u = {"zsx2", "263.net", 11, true}; + msg.user_vector.push_back(u); + msg.user_vector.emplace_back(u); + msg.user_vector.emplace_back("zsx2", "263.net4", 14, true); + + u = {"zsx31", "263.net", 11, true}; + msg.user_map[u.username] = u; + msg.user_map["zsx32"] = {"zsx32", "263.net", 11, true }; + + msg.user_list_ptr->push_back(new user("zsx4", "263.net1", 11, true)); + msg.user_list_ptr->push_back(new user("zsx4", "263.net2", 12, true)); + + msg.user_vector_ptr->push_back(new user("zsx5", "263.net1", 11, true)); + msg.user_vector_ptr->push_back(new user("zsx5", "263.net2", 12, true)); + + (*msg.user_map_ptr)["zsx61"] = new user("zsx61:", "263.net1", 11, true); + (*msg.user_map_ptr)["zsx62"] = new user("zsx62", "263.net2", 12, true); + + acl::json json; + acl::json_node& node = acl::gson(json, msg); + + message msg1; + acl::json json1; + json1.update(node.to_string()); + + printf("------------------------------------------------------\r\n"); + printf("json to_string: %s\r\n", json.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + printf("node to_string: %s\r\n", node.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + printf("json1 to_string: %s\r\n", json1.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + + std::pair ret = acl::gson(json1.get_root(), msg1); + if (ret.first == false) + printf("error: %s\r\n", ret.second.c_str()); + else + { + printf("==================== All OK ===================\r\n"); + print_msg(msg); + } +} + +static void usage(const char* procname) +{ + printf("usage: %s -h [help]\r\n", procname); +} + +int main(int argc, char* argv[]) +{ + int ch; + + while ((ch = getopt(argc, argv, "h")) > 0) + { + switch (ch) + { + case 'h': + usage(argv[0]); + return 0; + default: + break; + } + } + + test1(); + return 0; +} diff --git a/app/gson/test/test1.bak/main.cpp.bak b/app/gson/test/test1.bak/main.cpp.bak new file mode 100644 index 000000000..a7a4090f0 --- /dev/null +++ b/app/gson/test/test1.bak/main.cpp.bak @@ -0,0 +1,180 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include "struct.h" +#include "gson.h" + +static void print_msg(message& msg) +{ + printf("=======================================================\r\n"); + printf("type: %d\r\n", msg.type); + printf("cmd: %s\r\n", msg.cmd.c_str()); + + printf("-------------------- user list ------------------------\r\n"); + size_t i = 0; + for (auto cit : msg.user_list) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit.username.c_str(), cit.domain.c_str(), + cit.age, cit.male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------- user vector ----------------------\r\n"); + i = 0; + for (auto cit : msg.user_vector) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit.username.c_str(), cit.domain.c_str(), + cit.age, cit.male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("------------------- user map --------------------------\r\n"); + i = 0; + for (auto cit : msg.user_map) + { + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second.username.c_str(), + cit.second.domain.c_str(), + cit.second.age, + cit.second.male ? "true" : "false"); + + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second.get_username(), + cit.second.get_domain(), + cit.second.get_age(), + cit.second.is_male() ? "true" : "false"); + + if (++i >= 10) + break; + } + printf("-------------------- user list ptr --------------------\r\n"); + i = 0; + for (auto cit : *msg.user_list_ptr) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit->username.c_str(), cit->domain.c_str(), + cit->age, cit->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------- user vector ptr ------------------\r\n"); + i = 0; + for (auto cit : *msg.user_vector_ptr) + { + printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", + cit->username.c_str(), cit->domain.c_str(), + cit->age, cit->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("------------------- user map ptr ----------------------\r\n"); + i = 0; + for (auto cit : *msg.user_map_ptr) + { + printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", + cit.first.c_str(), + cit.second->username.c_str(), + cit.second->domain.c_str(), + cit.second->age, + cit.second->male ? "true" : "false"); + if (++i >= 10) + break; + } + + printf("-------------------------------------------------------\r\n"); +} + +static void test1(void) +{ + message msg; + msg.user_list_ptr = new std::list; + msg.user_vector_ptr = new std::vector; + msg.user_map_ptr = new std::map; + + msg.type = 1; + msg.cmd = "add"; + + user u = {"zsx1", "263.net", 11, true}; + msg.user_list.push_back(u); + msg.user_list.emplace_back(u); + msg.user_list.emplace_back("zsx1", "263.net", 13, false); + + u = {"zsx2", "263.net", 11, true}; + msg.user_vector.push_back(u); + msg.user_vector.emplace_back(u); + msg.user_vector.emplace_back("zsx2", "263.net4", 14, true); + + u = {"zsx31", "263.net", 11, true}; + msg.user_map[u.username] = u; + msg.user_map["zsx32"] = {"zsx32", "263.net", 11, true }; + + msg.user_list_ptr->push_back(new user("zsx4", "263.net1", 11, true)); + msg.user_list_ptr->push_back(new user("zsx4", "263.net2", 12, true)); + + msg.user_vector_ptr->push_back(new user("zsx5", "263.net1", 11, true)); + msg.user_vector_ptr->push_back(new user("zsx5", "263.net2", 12, true)); + + (*msg.user_map_ptr)["zsx61"] = new user("zsx61:", "263.net1", 11, true); + (*msg.user_map_ptr)["zsx62"] = new user("zsx62", "263.net2", 12, true); + + acl::json json; + acl::json_node& node = acl::gson(json, msg); + + message msg1; + acl::json json1; + json1.update(node.to_string()); + + printf("------------------------------------------------------\r\n"); + printf("json to_string: %s\r\n", json.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + printf("node to_string: %s\r\n", node.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + printf("json1 to_string: %s\r\n", json1.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + + std::pair ret = acl::gson(json1.get_root(), msg1); + if (ret.first == false) + printf("error: %s\r\n", ret.second.c_str()); + else + { + printf("==================== All OK ===================\r\n"); + print_msg(msg); + } +} + +static void usage(const char* procname) +{ + printf("usage: %s -h [help]\r\n", procname); +} + +int main(int argc, char* argv[]) +{ + int ch; + + while ((ch = getopt(argc, argv, "h")) > 0) + { + switch (ch) + { + case 'h': + usage(argv[0]); + return 0; + default: + break; + } + } + + test1(); + return 0; +} diff --git a/app/gson/test/test1.bak/stdafx.cpp b/app/gson/test/test1.bak/stdafx.cpp new file mode 100644 index 000000000..86cfa1350 --- /dev/null +++ b/app/gson/test/test1.bak/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : 只包括标准包含文件的源文件 +// wizard.pch 将成为预编译头 +// stdafx.obj 将包含预编译类型信息 + +#include "stdafx.h" + +// TODO: 在 STDAFX.H 中 +//引用任何所需的附加头文件,而不是在此文件中引用 diff --git a/app/gson/test/test1.bak/stdafx.h b/app/gson/test/test1.bak/stdafx.h new file mode 100644 index 000000000..eb2ba8f57 --- /dev/null +++ b/app/gson/test/test1.bak/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : 标准系统包含文件的包含文件, +// 或是常用但不常更改的项目特定的包含文件 +// + +#pragma once + + +//#include +//#include + +// TODO: 在此处引用程序要求的附加头文件 + +#include "lib_acl.h" +#include "acl_cpp/lib_acl.hpp" +#include "lib_protocol.h" diff --git a/app/gson/test/test1.bak/struct.h b/app/gson/test/test1.bak/struct.h new file mode 100644 index 000000000..e8059e80e --- /dev/null +++ b/app/gson/test/test1.bak/struct.h @@ -0,0 +1,122 @@ +#pragma once + +struct people +{ + //Gson@optional + acl::string aa; + //Gson@optional + acl::string bb; +}; + +struct user +{ + user(const char* user_name, const char* user_domain, + int user_age, bool user_male) + : username(user_name) + , domain(user_domain) + , age(user_age) + , male(user_male) + {} + + user() {} + ~user() {} + + user& set_username(const char* v) + { + username = v; + return *this; + } + user& set_domain(const char* v) + { + domain = v; + return *this; + } + user& set_age(int v) + { + age = v; + return *this; + } + user& set_male(bool v) + { + male =v; + return *this; + } + const char* get_username(void) const + { + return username.c_str(); + } + const char* get_domain(void) const + { + return domain.c_str(); + } + int get_age(void) const + { + return age; + } + bool is_male(void) const + { + return male; + } + + acl::string username; + acl::string domain; + int age; + bool male; +}; + +struct message +{ + int type; + acl::string cmd; + std::list user_list; + std::list user_vector; + std::map user_map; + + std::list *user_list_ptr = nullptr; + std::vector *user_vector_ptr = nullptr; + std::map *user_map_ptr = nullptr; + + //const char* ptr = nullptr; // error + //const char* data = "hello world"; // error + //char* data = "hello world"; // error + //char ch = 'A'; // error + //unsigned char ch = 'b'; // error + //char ch1; // error + //const user* u1 = nullptr; // error + int n = 100; // ok + long n1 = 1000; // ok + long long int n2 = 1000; // ok + short n3 = 100; // ok + //short* n3_1 = &n3; // error + //Gson@optional + user* u = nullptr; // ok + + //const int n4 = 100; // error + //const long n5 = 1000; // error + //const long long int n6 = 1000; // error + //const short n7 = 100; // error + + message() {} + + ~message() + { + if (user_list_ptr) + { + for (auto it : *user_list_ptr) + delete it; + delete user_list_ptr; + } + if (user_vector_ptr) + { + for (auto it : *user_vector_ptr) + delete it; + delete user_vector_ptr; + } + if (user_map_ptr) + { + for (auto it : *user_map_ptr) + delete it.second; + delete user_map_ptr; + } + } +}; diff --git a/app/gson/test/test1.bak/struct.stub b/app/gson/test/test1.bak/struct.stub new file mode 100644 index 000000000..e8059e80e --- /dev/null +++ b/app/gson/test/test1.bak/struct.stub @@ -0,0 +1,122 @@ +#pragma once + +struct people +{ + //Gson@optional + acl::string aa; + //Gson@optional + acl::string bb; +}; + +struct user +{ + user(const char* user_name, const char* user_domain, + int user_age, bool user_male) + : username(user_name) + , domain(user_domain) + , age(user_age) + , male(user_male) + {} + + user() {} + ~user() {} + + user& set_username(const char* v) + { + username = v; + return *this; + } + user& set_domain(const char* v) + { + domain = v; + return *this; + } + user& set_age(int v) + { + age = v; + return *this; + } + user& set_male(bool v) + { + male =v; + return *this; + } + const char* get_username(void) const + { + return username.c_str(); + } + const char* get_domain(void) const + { + return domain.c_str(); + } + int get_age(void) const + { + return age; + } + bool is_male(void) const + { + return male; + } + + acl::string username; + acl::string domain; + int age; + bool male; +}; + +struct message +{ + int type; + acl::string cmd; + std::list user_list; + std::list user_vector; + std::map user_map; + + std::list *user_list_ptr = nullptr; + std::vector *user_vector_ptr = nullptr; + std::map *user_map_ptr = nullptr; + + //const char* ptr = nullptr; // error + //const char* data = "hello world"; // error + //char* data = "hello world"; // error + //char ch = 'A'; // error + //unsigned char ch = 'b'; // error + //char ch1; // error + //const user* u1 = nullptr; // error + int n = 100; // ok + long n1 = 1000; // ok + long long int n2 = 1000; // ok + short n3 = 100; // ok + //short* n3_1 = &n3; // error + //Gson@optional + user* u = nullptr; // ok + + //const int n4 = 100; // error + //const long n5 = 1000; // error + //const long long int n6 = 1000; // error + //const short n7 = 100; // error + + message() {} + + ~message() + { + if (user_list_ptr) + { + for (auto it : *user_list_ptr) + delete it; + delete user_list_ptr; + } + if (user_vector_ptr) + { + for (auto it : *user_vector_ptr) + delete it; + delete user_vector_ptr; + } + if (user_map_ptr) + { + for (auto it : *user_map_ptr) + delete it.second; + delete user_map_ptr; + } + } +}; diff --git a/app/gson/test/test1.bak/struct.stub.bak b/app/gson/test/test1.bak/struct.stub.bak new file mode 100644 index 000000000..e8059e80e --- /dev/null +++ b/app/gson/test/test1.bak/struct.stub.bak @@ -0,0 +1,122 @@ +#pragma once + +struct people +{ + //Gson@optional + acl::string aa; + //Gson@optional + acl::string bb; +}; + +struct user +{ + user(const char* user_name, const char* user_domain, + int user_age, bool user_male) + : username(user_name) + , domain(user_domain) + , age(user_age) + , male(user_male) + {} + + user() {} + ~user() {} + + user& set_username(const char* v) + { + username = v; + return *this; + } + user& set_domain(const char* v) + { + domain = v; + return *this; + } + user& set_age(int v) + { + age = v; + return *this; + } + user& set_male(bool v) + { + male =v; + return *this; + } + const char* get_username(void) const + { + return username.c_str(); + } + const char* get_domain(void) const + { + return domain.c_str(); + } + int get_age(void) const + { + return age; + } + bool is_male(void) const + { + return male; + } + + acl::string username; + acl::string domain; + int age; + bool male; +}; + +struct message +{ + int type; + acl::string cmd; + std::list user_list; + std::list user_vector; + std::map user_map; + + std::list *user_list_ptr = nullptr; + std::vector *user_vector_ptr = nullptr; + std::map *user_map_ptr = nullptr; + + //const char* ptr = nullptr; // error + //const char* data = "hello world"; // error + //char* data = "hello world"; // error + //char ch = 'A'; // error + //unsigned char ch = 'b'; // error + //char ch1; // error + //const user* u1 = nullptr; // error + int n = 100; // ok + long n1 = 1000; // ok + long long int n2 = 1000; // ok + short n3 = 100; // ok + //short* n3_1 = &n3; // error + //Gson@optional + user* u = nullptr; // ok + + //const int n4 = 100; // error + //const long n5 = 1000; // error + //const long long int n6 = 1000; // error + //const short n7 = 100; // error + + message() {} + + ~message() + { + if (user_list_ptr) + { + for (auto it : *user_list_ptr) + delete it; + delete user_list_ptr; + } + if (user_vector_ptr) + { + for (auto it : *user_vector_ptr) + delete it; + delete user_vector_ptr; + } + if (user_map_ptr) + { + for (auto it : *user_map_ptr) + delete it.second; + delete user_map_ptr; + } + } +}; diff --git a/app/gson/test/test1.bak/valgrind.sh b/app/gson/test/test1.bak/valgrind.sh new file mode 100755 index 000000000..0b1bb2988 --- /dev/null +++ b/app/gson/test/test1.bak/valgrind.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +valgrind --tool=memcheck --leak-check=yes -v ./test diff --git a/app/gson/test/test5/Makefile b/app/gson/test/test5/Makefile new file mode 100644 index 000000000..f3f60643b --- /dev/null +++ b/app/gson/test/test5/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.in +ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) + EXTLIBS += -L/usr/local/lib -liconv +endif +PROG = test diff --git a/app/gson/test/test5/main.cpp b/app/gson/test/test5/main.cpp new file mode 100644 index 000000000..0831d6409 --- /dev/null +++ b/app/gson/test/test5/main.cpp @@ -0,0 +1,129 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include "struct.h" +#include "gson.h" + +static void print_user(user& u) +{ + printf("\t\tuser ---> name: %s, addr: %s, male: %s, age: %d\r\n", + u.get_name(), u.addr.c_str(), u.male ? "yes" : "no", u.age); +} + +static void print_group(const group& g) +{ + printf("\tgroup --> name: %s\r\n", g.name.c_str()); + for (auto cit : g.users) + print_user(cit); +} + +static void print_company(const company& com) +{ + printf("\r\ncompany ---> name: %s, addr: %s\r\n", + com.name.c_str(), com.addr.c_str()); + + for (auto cit : com.groups) + print_group(cit); + + for (auto cit : com.users) + printf("%s=%s\r\n", cit.first.c_str(), cit.second.c_str()); +} + +static void test(void) +{ + company com; + com.name = "263"; + com.addr = "beijing"; + + group g; + + g.name = "dev1"; + g.users.emplace_back("zsx11", 11, true); + g.users.emplace_back("zsx12", 12, false); + g.users.emplace_back("zsx13", 13, true); + com.groups.emplace_back(g); + + g.name = "dev2"; + g.users.clear(); + g.users.emplace_back("zsx21", 11, true); + g.users.emplace_back("zsx22", 12, false); + g.users.emplace_back("zsx23", 13, true); + com.groups.emplace_back(g); + + g.name = "dev3"; + g.users.clear(); + g.users.emplace_back("zsx31", 11, true); + g.users.emplace_back("zsx32", 12, false); + g.users.emplace_back("zsx33", 13, true); + com.groups.emplace_back(g); + + g = {"dev4", {{"zsx41", 11, true}, {"zsx42", 11, true}}}; + com.groups.emplace_back(g); + + g.set_name("dev5"); + g.users.clear(); + user u; + u.set_name("zsx51").set_age(11).set_male(true); + g.users.push_back(u); + u.set_name("zsx52").set_age(12).set_male(false); + g.users.push_back(u); + u.set_name("zsx53").set_age(13).set_male(true).set_nick("zsxxsz"); + g.users.push_back(u); + com.groups.push_back(g); + + com.users["zsx1"] = "xsz1"; + com.users["zsx2"] = "xsz2"; + com.users["zsx3"] = "xsz3"; + + acl::json json; + acl::json_node& node = acl::gson(json, com); + + printf("------------------------------------------------------\r\n"); + printf("node to_string: %s\r\n", node.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + + company com1; + acl::json json1; + json1.update(node.to_string()); + + printf("json1 to_string: %s\r\n", json1.to_string().c_str()); + printf("------------------------------------------------------\r\n"); + + std::pair ret = acl::gson(json1.get_root(), com1); + if (ret.first == false) + printf("error: %s\r\n", ret.second.c_str()); + else + { + printf("==================== All OK ===================\r\n"); + print_company(com1); + } +} + +static void usage(const char* procname) +{ + printf("usage: %s -h [help]\r\n", procname); +} + +int main(int argc, char* argv[]) +{ + int ch; + + while ((ch = getopt(argc, argv, "h")) > 0) + { + switch (ch) + { + case 'h': + usage(argv[0]); + return 0; + default: + break; + } + } + + test(); + return 0; +} diff --git a/app/gson/test/test5/stdafx.cpp b/app/gson/test/test5/stdafx.cpp new file mode 100644 index 000000000..86cfa1350 --- /dev/null +++ b/app/gson/test/test5/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : 只包括标准包含文件的源文件 +// wizard.pch 将成为预编译头 +// stdafx.obj 将包含预编译类型信息 + +#include "stdafx.h" + +// TODO: 在 STDAFX.H 中 +//引用任何所需的附加头文件,而不是在此文件中引用 diff --git a/app/gson/test/test5/stdafx.h b/app/gson/test/test5/stdafx.h new file mode 100644 index 000000000..eb2ba8f57 --- /dev/null +++ b/app/gson/test/test5/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : 标准系统包含文件的包含文件, +// 或是常用但不常更改的项目特定的包含文件 +// + +#pragma once + + +//#include +//#include + +// TODO: 在此处引用程序要求的附加头文件 + +#include "lib_acl.h" +#include "acl_cpp/lib_acl.hpp" +#include "lib_protocol.h" diff --git a/app/gson/test/test5/struct.stub b/app/gson/test/test5/struct.stub new file mode 100644 index 000000000..3ed8e698c --- /dev/null +++ b/app/gson/test/test5/struct.stub @@ -0,0 +1,75 @@ +#pragma once + +struct user +{ + user(const char* user_name, int user_age, bool user_male) + : name(user_name) + , age(user_age) + , male(user_male) + {} + + user() {} + ~user() {} + + //Gson@required + std::string name; + int age; + bool male; + + //Gson@optional + std::string addr; + + user& set_name(const char* v) + { + name = v; + return *this; + } + user& set_age(int v) + { + age = v; + return *this; + } + user& set_male(bool v) + { + male = v; + return *this; + } + const char* get_name(void) + { + return name.c_str(); + } + + /* + const char* get_name(void) const + { + return name.c_str(); + } + */ + + //Gson@skip + acl::string nick; + user& set_nick(const char* v) + { + nick = v; + return *this; + } +}; + +struct group +{ + std::string name; + std::vector users; + + void set_name(const char* v) + { + name = v; + } +}; + +struct company +{ + std::string name; + std::string addr; + std::vector groups; + std::map users; +}; diff --git a/app/gson/test/test5/valgrind.sh b/app/gson/test/test5/valgrind.sh new file mode 100755 index 000000000..0b1bb2988 --- /dev/null +++ b/app/gson/test/test5/valgrind.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +valgrind --tool=memcheck --leak-check=yes -v ./test diff --git a/lib_acl_cpp/changes.txt b/lib_acl_cpp/changes.txt index 501626bb9..e86c14d7d 100644 --- a/lib_acl_cpp/changes.txt +++ b/lib_acl_cpp/changes.txt @@ -1,6 +1,10 @@ 修改历史列表: ----------------------------------------------------------------------- +460) 2016.12.31 +460.1) bugfix: gson 对象序列化模块修改了 std::map 第二个参数不能为类对象的问题 +460.2) feature: gson 对象序列化模块支持 struct 对象的成员函数及非序列化成员变量 + 459) 2016.12.22 459.1) feature: json.cpp 中 json 类及 json_node 类中的 to_string 方法允许复用 用户传入的缓冲区 diff --git a/lib_fiber/c/include/fiber/lib_fiber.h b/lib_fiber/c/include/fiber/lib_fiber.h index 9b04f1475..dea654d80 100644 --- a/lib_fiber/c/include/fiber/lib_fiber.h +++ b/lib_fiber/c/include/fiber/lib_fiber.h @@ -42,7 +42,7 @@ ACL_FIBER *acl_fiber_running(void); /** * 鑾峰緱鎵缁欏崗绋嬬殑鍗忕▼ ID 鍙 - * @param fiber {const ACL_FIBER*} 鐢 acl_fiber_create 鍒涘缓鐨勫崗绋嬪璞 + * @param fiber {const ACL_FIBER*} acl_fiber_create 鍒涘缓鐨勫崗绋嬪璞★紝蹇呴』闈炵┖ * @return {int} 鍗忕▼ ID 鍙 */ int acl_fiber_id(const ACL_FIBER *fiber); @@ -55,14 +55,14 @@ int acl_fiber_self(void); /** * 璁剧疆鎵缁欏崗绋嬬殑閿欒鍙 - * @param fiber {ACL_FIBER*} 鍗忕▼瀵硅薄 + * @param fiber {ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝涓 NULL 鍒欎娇鐢ㄥ綋鍓嶈繍琛岀殑鍗忕▼ * @param errnum {int} 閿欒鍙 */ void acl_fiber_set_errno(ACL_FIBER *fiber, int errnum); /** * 鑾峰緱鎸囧畾鍗忕▼鐨勯敊璇彿 - * @param fiber {ACL_FIBER*} 鍗忕▼瀵硅薄 + * @param fiber {ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝鑻ヤ负 NULL 鍒欎娇鐢ㄥ綋鍓嶅崗绋嬪璞 * @return {int} 鎵缁欏崗绋嬮敊璇彿 */ int acl_fiber_errno(ACL_FIBER *fiber); @@ -70,31 +70,46 @@ int acl_fiber_errno(ACL_FIBER *fiber); /** * 鏄惁淇濇寔鎵鎸囧畾鍗忕▼鐨勯敊璇彿锛屽綋璁剧疆涓衡滀繚鎸佲濆悗锛屽垯璇ュ崗绋嬩粎淇濇寔褰撳墠鐘舵佷笅鐨 * 閿欒鍙凤紝涔嬪悗璇ュ崗绋嬬殑閿欒鍙 errno 灏嗕笉鍐嶆敼鍙橈紝璧板埌鍐嶆璋冪敤鏈嚱鏁板彇娑堜繚鎸 - * @param fiber {ACL_FIBER*} 鍗忕▼瀵硅薄 + * @param fiber {ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝涓 NULL 鍒欎娇鐢ㄥ綋鍓嶈繍琛岀殑鍗忕▼ * @param yesno {int} 鏄惁淇濇寔 */ void acl_fiber_keep_errno(ACL_FIBER *fiber, int yesno); /** * 鑾峰緱鎸囧畾鍗忕▼鐨勫綋鍓嶇姸鎬 - * @param fiber {const ACL_FIBER*} 鍗忕▼瀵硅薄 + * @param fiber {const ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝涓 NULL 鍒欎娇鐢ㄥ綋鍓嶅崗绋 * @return {int} 鍗忕▼鐘舵 */ int acl_fiber_status(const ACL_FIBER *fiber); /** - * 閫氱煡鎸囧畾鍗忕▼閫鍑 - * @param fiber {const ACL_FIBER*} 鍗忕▼瀵硅薄 + * 閫氱煡澶勪簬浼戠湢鐘舵佺殑鍗忕▼閫鍑 + * @param fiber {const ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝蹇呴』闈 NULL */ void acl_fiber_kill(ACL_FIBER *fiber); /** * 妫鏌ユ湰鍗忕▼鏄惁琚叾瀹冨崗绋嬮氱煡閫鍑 - * @param fiber {const ACL_FIBER*} 鍗忕▼瀵硅薄 + * @param fiber {const ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝鑻ヤ负 NULL 鍒欒嚜鍔ㄤ娇鐢ㄥ綋鍓 + * 姝e湪杩愯鐨勫崗绋 * @return {int} 杩斿洖鍊间负 0 琛ㄧず娌℃湁琚氱煡閫鍑猴紝闈 0 琛ㄧず琚氱煡閫鍑 */ int acl_fiber_killed(ACL_FIBER *fiber); +/** + * 鍞ら啋鍥 IO 绛夊師鍥犲浜庝紤鐪犵殑鍗忕▼ + * @param fiber {const ACL_FIBER*} 鍗忕▼瀵硅薄锛屽繀椤婚潪 NULL + * @param signum {int} SIGINT, SIGKILL, SIGTERM ... 鍙傝冪郴缁熶腑 bits/signum.h + */ +void acl_fiber_signal(ACL_FIBER *fiber, int signum); + +/** + * 鑾峰緱鍏跺畠鍗忕▼鍙戦佺粰鎸囧畾鍗忕▼鐨勪俊鍙峰 + * @param fiber {const ACL_FIBER*} 鎸囧畾鐨勫崗绋嬪璞★紝涓 NULL 鏃跺垯浣跨敤褰撳墠鍗忕▼ + * @retur {int} 杩斿洖鎸囧畾鍗忕▼鏀跺埌鐨勪俊鍙峰 + */ +int acl_fiber_signum(ACL_FIBER *fiber); + /** * 灏嗗綋鍓嶈繍琛岀殑鍗忕▼鎸傝捣锛岀敱璋冨害鍣ㄩ夋嫨涓嬩竴涓渶瑕佽繍琛岀殑鍗忕▼ * @return {int} @@ -103,7 +118,7 @@ int acl_fiber_yield(void); /** * 灏嗘寚瀹氬崗绋嬪璞$疆鍏ュ緟杩愯闃熷垪涓 - * @param fiber {ACL_FIBER*} + * @param fiber {ACL_FIBER*} 鎸囧畾鍗忕▼锛屽繀椤婚潪 NULL */ void acl_fiber_ready(ACL_FIBER *fiber); diff --git a/lib_fiber/c/src/fiber.c b/lib_fiber/c/src/fiber.c index 27bd680f0..22c56ac67 100644 --- a/lib_fiber/c/src/fiber.c +++ b/lib_fiber/c/src/fiber.c @@ -216,20 +216,29 @@ void *realloc(void *ptr, size_t size) void acl_fiber_set_errno(ACL_FIBER *fiber, int errnum) { - fiber->errnum = errnum; + if (fiber == NULL) + fiber = acl_fiber_running(); + if (fiber) + fiber->errnum = errnum; } int acl_fiber_errno(ACL_FIBER *fiber) { - return fiber->errnum; + if (fiber == NULL) + fiber = acl_fiber_running(); + return fiber ? fiber->errnum : 0; } void acl_fiber_keep_errno(ACL_FIBER *fiber, int yesno) { - if (yesno) - fiber->flag |= FIBER_F_SAVE_ERRNO; - else - fiber->flag &= ~FIBER_F_SAVE_ERRNO; + if (fiber == NULL) + fiber = acl_fiber_running(); + if (fiber) { + if (yesno) + fiber->flag |= FIBER_F_SAVE_ERRNO; + else + fiber->flag &= ~FIBER_F_SAVE_ERRNO; + } } void fiber_save_errno(void) @@ -407,6 +416,18 @@ ACL_FIBER *acl_fiber_running(void) } void acl_fiber_kill(ACL_FIBER *fiber) +{ + acl_fiber_signal(fiber, SIGKILL); +} + +int acl_fiber_killed(ACL_FIBER *fiber) +{ + if (fiber) + fiber = acl_fiber_running(); + return fiber && (fiber->flag & FIBER_F_KILLED); +} + +void acl_fiber_signal(ACL_FIBER *fiber, int signum) { ACL_FIBER *curr = __thread_fiber->running; @@ -422,7 +443,9 @@ void acl_fiber_kill(ACL_FIBER *fiber) return; } - fiber->flag |= FIBER_F_KILLED; + if (signum == SIGKILL || signum == SIGTERM || signum == SIGQUIT) + fiber->flag |= FIBER_F_KILLED; + fiber->signum = signum; if (fiber == curr) // just return if kill myself return; @@ -433,9 +456,11 @@ void acl_fiber_kill(ACL_FIBER *fiber) acl_fiber_yield(); } -int acl_fiber_killed(ACL_FIBER *fiber) +int acl_fiber_signum(ACL_FIBER *fiber) { - return fiber->flag & FIBER_F_KILLED; + if (fiber) + fiber = acl_fiber_running(); + return fiber ? fiber->signum : 0; } void fiber_exit(int exit_code) @@ -555,6 +580,7 @@ static ACL_FIBER *fiber_alloc(void (*fn)(ACL_FIBER *, void *), size = fiber->size; fiber->errnum = 0; + fiber->signum = 0; fiber->fn = fn; fiber->arg = arg; fiber->size = size; @@ -629,7 +655,9 @@ int acl_fiber_self(void) int acl_fiber_status(const ACL_FIBER *fiber) { - return fiber->status; + if (fiber == NULL) + fiber = acl_fiber_running(); + return fiber ? fiber->status : 0; } static void fiber_init(void) __attribute__ ((constructor)); diff --git a/lib_fiber/c/src/fiber.h b/lib_fiber/c/src/fiber.h index 9e60a1a40..e7dfd9313 100644 --- a/lib_fiber/c/src/fiber.h +++ b/lib_fiber/c/src/fiber.h @@ -27,6 +27,7 @@ struct ACL_FIBER { acl_int64 when; int errnum; int sys; + int signum; unsigned int flag; #define FIBER_F_SAVE_ERRNO (unsigned) 1 << 0 #define FIBER_F_KILLED (unsigned) 1 << 1 diff --git a/lib_fiber/changes.txt b/lib_fiber/changes.txt index c7de56de0..278956c20 100644 --- a/lib_fiber/changes.txt +++ b/lib_fiber/changes.txt @@ -1,3 +1,6 @@ +37) 2016.12.31 +37.1) feature: 澧炲姞 acl_fiber_signal 鍑芥暟锛屽厑璁稿崗绋嬬粰鍏跺畠鍗忕▼鍙戦佸敜閱掍俊鍙 + 36) 2016.12.27 36.1) bugfix: fiber.cpp 涓 kill 鏂规硶闇灏 f_ 鎴愬憳缃 NULL锛岄槻姝㈠祵濂楄闂潪娉曞湴鍧 36.2) feature: fiber.cpp 鍏佽灏嗗綋鍓嶈繍琛岀殑鍗忕▼涓 fiber 绫诲璞$粦瀹(鍙渶鍦ㄦ瀯閫