APIs
C often provide opaque pens that prevent the consumer from asking what they are and trying to stick it out. The fact that these pens are pointers does not matter and does not matter to the consumer, and should not be burdened with lexical interference of an additional star.
For example, it is possible to write a C ++ binding by specifying a handle:
typedef void * MyHandle;
Now we give blessed user C some functions:
MyHandle create_gizmo(); void destroy_gizmo(MyHandle); int do_magic(MyHandle, int, int);
Just. The user immediately sees how to use it:
#include "MagicAPI.h" MyHandle h = create_gizmo(); submit_result(do_magic(h, 12, 91)); destroy_gizmo(h);
And the C ++ library developer simply expands the descriptor and populates the API functions (which, of course, are extern "C" declared):
#include "MagicAPI.h" #include "SuperGizmo.hpp" MyHandle create_gizmo() { return static_cast<MyHandle>(new SuperGizmo); } void destroy_gizmo(MyHandle h) { delete static_cast<SuperGizmo *>(h); } int do_magic(MyHandle h, int a, int b) { return static_cast<SuperGizmo *>(h)->foo(a, b); }
source share