Skip to content

Commit 9062d05

Browse files
committed
feat: make working with lipc strings easier
1 parent de71e27 commit 9062d05

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

src/lipc/StringHandler.h

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,57 @@
44
#include <cstdint>
55
#include <cstdio>
66
#include <cstdlib>
7+
#include <cstring>
78
#include <functional>
89
#include <string>
10+
911
namespace utild::lipc {
1012
template <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

1547
template <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

1851
template <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;
2458
template <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

src/main.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,22 @@ int main(int argc, char *argv[]) {
124124

125125
utild::lipc::StringHandler<std::nullptr_t> exit_handler("exit");
126126
exit_handler.setSetter(
127-
[](utild::lipc::StringHandler<std::nullptr_t> *_this, LIPC *lipc, std::string value, void* _data) -> LIPCcode {
127+
[](utild::lipc::StringHandler<std::nullptr_t> *_this, LIPC *_lipc, utild::lipc::LIPCString* _value) -> LIPCcode {
128128
utild::keep_running = 0;
129129
return LIPC_OK;
130130
}
131-
)->setGetter([](utild::lipc::StringHandler<std::nullptr_t> *_this, LIPC *lipc, char* value, void* _data) -> LIPCcode {
132-
std::string message = "Write into this property to exit utild";
133-
// *value = reinterpret_cast<char*>(malloc(message.size() + 1));
134-
strcpy(value, message.c_str());
135-
return LIPC_OK;
131+
)->setGetter([](utild::lipc::StringHandler<std::nullptr_t> *_this, LIPC *_lipc, utild::lipc::LIPCString* value) -> LIPCcode {
132+
return value->set("Write into this property to exit utild");
136133
})->subscribe(handle);
137134

138135
utild::lipc::StringHandler<std::string> cmd_handler("runCMD");
139136

140137
cmd_handler
141-
.setSetter([](utild::lipc::StringHandler<std::string> *_this, LIPC *lipc, std::string value, void* _data) -> LIPCcode {
142-
_this->setData(exec(value));
138+
.setSetter([](utild::lipc::StringHandler<std::string> *_this, LIPC *_lipc, utild::lipc::LIPCString* value) -> LIPCcode {
139+
_this->setData(exec(value->toString()));
143140
return LIPC_OK;
144-
})->setGetter([](utild::lipc::StringHandler<std::string> *_this, LIPC *lipc, char* value, void* _data) -> LIPCcode {
145-
strcpy(value, _this->getData().empty() ? "No output yet." : _this->getData().c_str());
146-
_this->setData("");
147-
return LIPC_OK;
141+
})->setGetter([](utild::lipc::StringHandler<std::string> *_this, LIPC *_lipc, utild::lipc::LIPCString* value) -> LIPCcode {
142+
return value->set(_this->getData().empty() ? "No output yet." : _this->getData().c_str());
148143
})->subscribe(handle);
149144

150145
while (utild::keep_running) {

0 commit comments

Comments
 (0)