thass0 / blog-code Goto Github PK
View Code? Open in Web Editor NEWCode used in blog posts
Home Page: https://thasso.xyz
Code used in blog posts
Home Page: https://thasso.xyz
Good introduction, but there are ways to make this binding simpler and better. I just wrote about this on lobste.rs and I’ll copy it below.
FYI, if you want to see a large and complex example of a full C++ API wrapped in C, take a look at Couchbase Lite Core, my main work project. This C API is in turn used by language bindings in Objective-C, Swift, Java, Kotlin and C#.
——
I’ve done a lot of wrapping-C++-in-C in my career. This is a good basic intro, but there are several things that can be done better.
First, it’s much simpler to use extern “C”
— you don’t need to go to the lengths of making a macro for it! You just put a curly-braced block after it, and the C-ness applies to everything in that block. The super-common idiom for this is:
#ifdef __cplusplus
extern “C” {
#endif
// … any number of declarations here ...
#ifdef __cplusplus
}
#endif
Second, using void*
to represent an object pointer loses type-safety. It’s quite easy to do better. Another common idiom:
#ifndef __cplusplus
typedef struct Rational Rational;
#endif
extern_c Rational *make_rational(int numer, int denom);
extern_c int get_numer(const Rational *r);
extern_c int get_denom(const Rational *r);
extern_c void del_rational(Rational **rp);
This tells the C compiler there’s a struct called Rational, but the compiler never sees (nor needs to see) its definition since you only ever declare pointers to it. It’s an opaque type.
Note that the accessors take a const pointer. This is a very good practice to follow. It makes const Rational*
a read-only reference, so you can pass that type around and be assured that the called function(s) won’t modify (or free!) the object. (Unless they’re evil and cast away the const, but you wouldn’t do that, right?)
As a bonus, if the C++ class Rational
is in the default namespace, these actually refer to the same type, so in your C++ implementations of make_rational
etc. you don’t need to do any type-casting. However, that’s not usually the case, since a good C++ API puts its declarations in a unique namespace; so generally your implementations do have to cast between e.g. mymath::Rational*
and ::Rational*
. But it’s a small price to pay for type-safety.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.