44#include < cstdint>
55#include < cstdio>
66#include < cstdlib>
7+ #include < cstring>
78#include < functional>
89#include < string>
10+
911namespace utild ::lipc {
1012template <typename T> class StringHandler ;
1113
12- template <typename T>
13- using SimpleStringSetterCB = LIPCcode(StringHandler<T> *_this, LIPC *lipc, std::string value, void *data);
14+ class LIPCString {
15+ public:
16+ LIPCString (char *value, void *data) : value(value), data(data) {
17+ }
18+ LIPCcode check (std::string value) {
19+ if (value.length () + 1 > *(int *)data) {
20+ *(int *)data = value.length () + 1 ;
21+ return LIPC_ERROR_BUFFER_TOO_SMALL;
22+ }
23+ return LIPC_OK;
24+ }
25+
26+ LIPCcode set (const std::string value) {
27+ auto result = check (value);
28+ if (result != LIPC_OK)
29+ return result;
30+ strcpy (this ->value , value.c_str ());
31+ return LIPC_OK;
32+ }
33+
34+ char *get () {
35+ return value;
36+ }
37+
38+ std::string toString () {
39+ return std::string (value);
40+ }
41+
42+ private:
43+ char *value;
44+ void *data;
45+ };
1446
1547template <typename T>
16- using SimpleStringGetterCB = LIPCcode(StringHandler<T> *_this, LIPC *lipc, char *value, void *data);
48+ using StringCallback = LIPCcode(StringHandler<T> *_this, LIPC *lipc, LIPCString *value);
49+
1750
1851template <typename T> struct Data {
19- Data () : handler(nullptr ), data(nullptr ) {}
52+ Data () : handler(nullptr ), data(nullptr ) {
53+ }
2054 void *data;
2155 StringHandler<T> *handler;
2256};
23- inline std::unordered_map<std::string, void *> g_string_handlers;
57+ inline std::unordered_map<std::string, void *> g_string_handlers;
2458template <typename T> class StringHandler : public IHandler {
2559 public:
2660 StringHandler (const std::string &command) : command(command) {
@@ -29,26 +63,26 @@ template <typename T> class StringHandler : public IHandler {
2963
3064 static LIPCcode propGetCallback (LIPC *lipc, const char *property, void *value, void *data) {
3165
32- auto _this = static_cast <StringHandler<T>*>(g_string_handlers[property]);
66+ auto _this = static_cast <StringHandler<T> *>(g_string_handlers[property]);
3367 if (_this->getter_cb ) {
34- return _this->getter_cb (_this, lipc, ( char *)value, data);
68+ return _this->getter_cb (_this, lipc, new LIPCString (( char *)value, data) );
3569 }
3670 return LIPC_ERROR_NO_SUCH_PROPERTY;
3771 }
3872
3973 static LIPCcode propSetCallback (LIPC *lipc, const char *property, void *value, void *data) {
4074
41- auto _this = static_cast <StringHandler<T>*>(g_string_handlers[property]);
75+ auto _this = static_cast <StringHandler<T> *>(g_string_handlers[property]);
4276 if (_this->setter_cb ) {
43- return _this->setter_cb (_this, lipc, reinterpret_cast < char *>(value) , data);
77+ return _this->setter_cb (_this, lipc, new LIPCString (( char *)value , data) );
4478 }
4579 return LIPC_ERROR_NO_SUCH_PROPERTY;
4680 }
47- StringHandler *setGetter (std::function<SimpleStringGetterCB <T>> callback) {
81+ StringHandler *setGetter (std::function<StringCallback <T>> callback) {
4882 this ->getter_cb = callback;
4983 return this ;
5084 }
51- StringHandler *setSetter (std::function<SimpleStringSetterCB <T>> callback) {
85+ StringHandler *setSetter (std::function<StringCallback <T>> callback) {
5286 this ->setter_cb = callback;
5387 return this ;
5488 }
@@ -68,8 +102,8 @@ template <typename T> class StringHandler : public IHandler {
68102 private:
69103 std::string command;
70104 T data;
71- std::function<SimpleStringGetterCB <T>> getter_cb;
72- std::function<SimpleStringSetterCB <T>> setter_cb;
105+ std::function<StringCallback <T>> getter_cb;
106+ std::function<StringCallback <T>> setter_cb;
73107};
74108
75109} // namespace utild::lipc
0 commit comments