Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,8 @@ static const struct ubus_method test_methods[] = {
static struct ubus_object_type test_object_type =
UBUS_OBJECT_TYPE("test", test_methods);

static struct ubus_object test_object = {
.name = "test",
.type = &test_object_type,
.methods = test_methods,
.n_methods = ARRAY_SIZE(test_methods),
};
static struct ubus_object test_object =
UBUS_OBJECT("test", test_object_type);

static void server_main(void)
{
Expand Down
5 changes: 5 additions & 0 deletions libubus-obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
return UBUS_STATUS_INVALID_ARGUMENT;
}

if (obj->n_methods < 0 && obj->type) { /* initialized via UBUS_OBJECT() */
obj->methods = obj->type->methods;
obj->n_methods = obj->type->n_methods;
}

if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
return UBUS_STATUS_INVALID_ARGUMENT;

Expand Down
13 changes: 13 additions & 0 deletions libubus.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ typedef bool (*ubus_new_object_handler_t)(struct ubus_context *ctx, struct ubus_
#define UBUS_TAG_ADMIN (1ul << 1)
#define UBUS_TAG_PRIVATE (1ul << 2)

#define UBUS_OBJECT(_name, _type) \
{ \
.avl = {}, \
.name = _name, \
.id = 0, \
.path = NULL, \
.type = &(_type), \
.subscribe_cb = NULL, \
.has_subscribers = false, \
.methods = NULL, \
.n_methods = -1 \
}

struct ubus_method {
const char *name;
ubus_handler_t handler;
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FOREACH(test_case ${test_cases})
ENDFOREACH(test_case)

ENABLE_LANGUAGE(CXX)
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:CXX>:-std=gnu++11>)
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:CXX>:-std=gnu++20>)
ADD_UNIT_TEST_CPP(test-cplusplus)
ADD_TEST(NAME cplusplus COMMAND test-cplusplus)

Expand Down
5 changes: 3 additions & 2 deletions tests/test-cplusplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ constexpr ubus_method test_methods[] = {
UBUS_METHOD_TAG_NOARG("hello5", handler, UBUS_TAG_STATUS),
};

constexpr ubus_object_type test_object_type = UBUS_OBJECT_TYPE("test", test_methods);
constinit ubus_object_type test_object_type = UBUS_OBJECT_TYPE("test", test_methods);
constinit ubus_object test_object = UBUS_OBJECT("testobj", test_object_type);

int main()
{
(void) test_object_type;
(void) test_object;
return 0;
}