mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-03 04:17:52 +08:00
commit
57dad2dbba
5
app/gson/test/test1.bak/Makefile
Normal file
5
app/gson/test/test1.bak/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ../Makefile.in
|
||||
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
|
||||
EXTLIBS += -L/usr/local/lib -liconv
|
||||
endif
|
||||
PROG = test
|
214
app/gson/test/test1.bak/gson.cpp
Normal file
214
app/gson/test/test1.bak/gson.cpp
Normal file
@ -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<bool,std::string> gson(acl::json_node &$node, people &$obj)
|
||||
{
|
||||
acl::json_node *aa = $node["aa"];
|
||||
acl::json_node *bb = $node["bb"];
|
||||
std::pair<bool, std::string> result;
|
||||
|
||||
if(aa)
|
||||
gson(*aa, &$obj.aa);
|
||||
|
||||
if(bb)
|
||||
gson(*bb, &$obj.bb);
|
||||
|
||||
return std::make_pair(true,"");
|
||||
}
|
||||
|
||||
|
||||
std::pair<bool,std::string> 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<bool,std::string> 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<bool, std::string> 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<bool,std::string> gson(acl::json_node &$node, user *$obj)
|
||||
{
|
||||
return gson($node, *$obj);
|
||||
}
|
||||
|
||||
|
||||
}///end of acl.
|
15
app/gson/test/test1.bak/gson.h
Normal file
15
app/gson/test/test1.bak/gson.h
Normal file
@ -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<bool,std::string> gson(acl::json_node &$node, people &$obj);
|
||||
std::pair<bool,std::string> 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<bool,std::string> gson(acl::json_node &$node, user &$obj);
|
||||
std::pair<bool,std::string> gson(acl::json_node &$node, user *$obj);
|
||||
}///end of acl.
|
180
app/gson/test/test1.bak/main.cpp
Normal file
180
app/gson/test/test1.bak/main.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
#include "stdafx.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#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<user*>;
|
||||
msg.user_vector_ptr = new std::vector<user*>;
|
||||
msg.user_map_ptr = new std::map<acl::string, user*>;
|
||||
|
||||
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<bool, std::string> 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;
|
||||
}
|
180
app/gson/test/test1.bak/main.cpp.bak
Normal file
180
app/gson/test/test1.bak/main.cpp.bak
Normal file
@ -0,0 +1,180 @@
|
||||
#include "stdafx.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#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<user*>;
|
||||
msg.user_vector_ptr = new std::vector<user*>;
|
||||
msg.user_map_ptr = new std::map<acl::string, user*>;
|
||||
|
||||
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<bool, std::string> 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;
|
||||
}
|
8
app/gson/test/test1.bak/stdafx.cpp
Normal file
8
app/gson/test/test1.bak/stdafx.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// stdafx.cpp : 只包括标准包含文件的源文件
|
||||
// wizard.pch 将成为预编译头
|
||||
// stdafx.obj 将包含预编译类型信息
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: 在 STDAFX.H 中
|
||||
//引用任何所需的附加头文件,而不是在此文件中引用
|
15
app/gson/test/test1.bak/stdafx.h
Normal file
15
app/gson/test/test1.bak/stdafx.h
Normal file
@ -0,0 +1,15 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "lib_protocol.h"
|
122
app/gson/test/test1.bak/struct.h
Normal file
122
app/gson/test/test1.bak/struct.h
Normal file
@ -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> user_list;
|
||||
std::list<user> user_vector;
|
||||
std::map<acl::string, user> user_map;
|
||||
|
||||
std::list<user*> *user_list_ptr = nullptr;
|
||||
std::vector<user*> *user_vector_ptr = nullptr;
|
||||
std::map<acl::string, user*> *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;
|
||||
}
|
||||
}
|
||||
};
|
122
app/gson/test/test1.bak/struct.stub
Normal file
122
app/gson/test/test1.bak/struct.stub
Normal file
@ -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> user_list;
|
||||
std::list<user> user_vector;
|
||||
std::map<acl::string, user> user_map;
|
||||
|
||||
std::list<user*> *user_list_ptr = nullptr;
|
||||
std::vector<user*> *user_vector_ptr = nullptr;
|
||||
std::map<acl::string, user*> *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;
|
||||
}
|
||||
}
|
||||
};
|
122
app/gson/test/test1.bak/struct.stub.bak
Normal file
122
app/gson/test/test1.bak/struct.stub.bak
Normal file
@ -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> user_list;
|
||||
std::list<user> user_vector;
|
||||
std::map<acl::string, user> user_map;
|
||||
|
||||
std::list<user*> *user_list_ptr = nullptr;
|
||||
std::vector<user*> *user_vector_ptr = nullptr;
|
||||
std::map<acl::string, user*> *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;
|
||||
}
|
||||
}
|
||||
};
|
3
app/gson/test/test1.bak/valgrind.sh
Executable file
3
app/gson/test/test1.bak/valgrind.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
valgrind --tool=memcheck --leak-check=yes -v ./test
|
5
app/gson/test/test5/Makefile
Normal file
5
app/gson/test/test5/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ../Makefile.in
|
||||
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
|
||||
EXTLIBS += -L/usr/local/lib -liconv
|
||||
endif
|
||||
PROG = test
|
129
app/gson/test/test5/main.cpp
Normal file
129
app/gson/test/test5/main.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "stdafx.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#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<bool, std::string> 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;
|
||||
}
|
8
app/gson/test/test5/stdafx.cpp
Normal file
8
app/gson/test/test5/stdafx.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// stdafx.cpp : 只包括标准包含文件的源文件
|
||||
// wizard.pch 将成为预编译头
|
||||
// stdafx.obj 将包含预编译类型信息
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: 在 STDAFX.H 中
|
||||
//引用任何所需的附加头文件,而不是在此文件中引用
|
15
app/gson/test/test5/stdafx.h
Normal file
15
app/gson/test/test5/stdafx.h
Normal file
@ -0,0 +1,15 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "lib_protocol.h"
|
75
app/gson/test/test5/struct.stub
Normal file
75
app/gson/test/test5/struct.stub
Normal file
@ -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<user> users;
|
||||
|
||||
void set_name(const char* v)
|
||||
{
|
||||
name = v;
|
||||
}
|
||||
};
|
||||
|
||||
struct company
|
||||
{
|
||||
std::string name;
|
||||
std::string addr;
|
||||
std::vector<group> groups;
|
||||
std::map<acl::string, acl::string> users;
|
||||
};
|
3
app/gson/test/test5/valgrind.sh
Executable file
3
app/gson/test/test5/valgrind.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
valgrind --tool=memcheck --leak-check=yes -v ./test
|
@ -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 方法允许复用
|
||||
用户传入的缓冲区
|
||||
|
@ -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 则自动使用当前
|
||||
* 正在运行的协程
|
||||
* @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);
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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
|
||||
|
@ -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 类对象绑定(只需在构造
|
||||
|
Loading…
Reference in New Issue
Block a user