Object-oriented design patterns in the kernel, part 1
Posted Jun 2, 2011 11:12 UTC (Thu) by
juliank (subscriber, #45896)
In reply to:
Object-oriented design patterns in the kernel, part 1 by Cyberax
Parent article:
Object-oriented design patterns in the kernel, part 1
> Why?? Opaque object pointers are nice for some parts of ABI,
> but internally they are rarely required.
In user space code, at least 99% of all ABI should be opaque. There should be no inline code (and thus no templates), no complete types, the only things there should be are functions and incomplete types.
Everything else is going to fail. Even in Qt, where they try harder to avoid such problems, we still have an ABI that only lasts 4 years (Qt3) or is expected to last 6 years (Qt4). In other parts of the world, we have ABI breaks in every minor release (V8), or all few months (APT).
C ABIs in contrast can last much longer, as evident by the fact that GLib 2 was released in 2002, and did not have any ABI break yet. Of course, there are also bad C ABIs, such as Python's, which are allowed to break with every release, but C makes it much easier to create a consistent long-term-stable ABI than C++.
> You can't call a method of an incomplete class
> (duh, it's _INCOMPLETE_). But you can pass it
> as a parameter to a function/method. Just like
> in C.
But then you're in C world again, and have no advantage of using C++.
In C, the struct can be incomplete, yet I can call functions on it which can be virtual (methods) and other functions, all using one common interface, via functions, such as the following two, defined in the my_object.c.
struct MyObject {
int some_internal_int_field;
void (*method_a)(MyObject *o);
};
int my_object_function_a(MyObject *o)
{
return o->some_internal_int_field;
}
void my_object_method_a(MyObject *o)
{
o->method_a(o);
}
The external world only sees the header file:
typedef struct MyObject MyObject;
int my_object_function_a(MyObject *o)
void my_object_method_a(MyObject *o)
The language coming closest to the optimum is Vala. Private fields are not part of an objects ABI, neither is (for callees) whether a method is virtual or not.
(
Log in to post comments)