Fwd: C++ libubus wrapper

Wojciech Jowsa wojciech.jowsa at gmail.com
Thu Feb 4 16:21:40 EST 2021


> For me, putting void * pointers everywhere is an anti-pattern which I
> discourage with the way the ubus api is set up. It is a lazy way to
> avoid making the code more type safe, and it adds a bit of unnecessary
> bloat to data structures.

Void pointer has one advantage i.e. it is portable and easier to use.
However, I agree that it increases memory usage so this might be a blocker.

> The ubus API is designed in a way that you typically embed the struct
> ubus_object into your own object-like data structure.
> Simple pseudo-code example:
>
> struct my_obj {
>         ...
>         struct ubus_object ubus;
>         ...
> };
>
>
> static int
> my_obj_test(struct ubus_context *ctx, struct ubus_object *obj,
>             struct ubus_request_data *req, const char *method,
>             struct blob_attr *msg)
> {
>         struct my_obj *obj = container_of(obj, struct my_obj, ubus);
>
>         ...
> }
>
> static struct ubus_method my_obj_methods[] = {
>         { .name = "test", .handle = my_obj_test },
> };
>
> As you can see, this pattern preserves some type-safety when going from
> struct ubus_object to struct my_obj and it does not waste space for an
> extra pointer. It is also more efficient, since the compiler will turn
> it into simple pointer arithmetic instead of an extra memory access.
>
> I think it would be very useful if your C++ wrapper could preserve this
> pattern and make it just as convenient to use.
>
> Maybe a C++ wrapper around ubus_object could simply be embedded as a
> class attribute in user-defined classes and you could write some code to
> generate thin wrappers that derive the object pointer from the address
> of the struct ubus_object pointer and call plain C++ methods with a
> fixed type.
>
> I don't know enough C++ to come up with an example of what that wrapper
> would look like, but it seems to me that if C can do this, it should be
> possible in C++ as well.
>
> Does that make sense?
>
> - Felix

Yes it does and as I write I think it will work. This pattern might be
not intuitive for C++
programmers but it is fine with me.



More information about the openwrt-devel mailing list