A proof of concept for exposing C++-API with pure-interfaces to C-consumers preserving type safety.
Given some C++-API which contains some pure interfaces, for example
//some pure cpp-interface
class Interface{
public:
virtual void foo() = 0;
virtual int bar(int) = 0;
virtual ~Interface() = default;
};
//functionality using cpp-interface
void use_interface(Interface &obj);
we would like to use it from pure C-code. Yet because there are no virtual classes in C, we cannot use this functionality out-of-the-box.
The prof of concept can be found in the prototype
-folder and consist of the following part.
- Core C++-functionality (
prototype/cpp_interface.*
). - C++-wrapper around the core-functionality exposing the core-functionality as C-API (
prototype/c_interface.*
). - An example of a pure C-implementation of C++-interfaces wrapped in the above C-API.
- C-consumer of the above C-API (
prototype/main.c
).
The C-API wrapper makes by hand, what C++-compiler would do automatically:
- use
struct
for aggregation of the data member of the mocked c++-class. - first element of the above struct is a virtual table, which can be overwritted by the "derived" subclasses.
- An adapter wraps data-struct + vt into a c++-representation of the interface.
Build the prototype in the prototype
-folder via
sh build.sh
and run it via
./prog