Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would implement the library in C++. If you need C access, then this is easily done by hand, by providing wrapper functions that take an additional "this" argument.</p> <p>Before you start though, read the <a href="http://swig.org" rel="nofollow">SWIG</a> documentation, in particular its limitations and things to avoid. If you design your C++ interface with SWIG in mind you can get the bindings for a lot of languages generated for you without effort.</p> <p><b>Edit</b>: Here is a quick example of a C wrapper for a C++ class. Let's say this is the C++ class to wrap, let's call it <code>test.h</code>:</p> <pre><code>class Test { public: Test(); int do_something(char* arg); private: is_valid(); // see below for why you need this method }; </code></pre> <p>This is your C header <code>test_c.h</code>:</p> <pre><code>typedef void* TestHandle; TestHandle newTest(); int deleteTest(TestHandle h); int Test_do_something(TestHandle h, char* arg); </code></pre> <p>And your C implementation will be a C++ file with C functions, let's say <code>test_c.cpp</code>:</p> <pre><code>extern "C" TestHandle newTest() { return (void*)new Test(); } extern "C" int deleteTest(TestHandle h) { Test* this = static_cast&lt;Test*&gt;(h); if (!this-&gt;is_valid()) return -1; // here we define -1 as "invalid handle" error delete this; return 0; // here we define 0 as the "ok" error code } extern "C" int Test_do_something(TestHandle h, char* arg) { Test* this = static_cast&lt;Test*&gt;(h); if (!this-&gt;is_valid()) return -1; // here we define -1 as "invalid handle" error return this-&gt;do_something(arg); } </code></pre> <p>The <code>is_valid()</code> method is there to guarantee that you are not passed a bad handle. For example, you can store a <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)" rel="nofollow">magic number</a> in all your instances, then <code>is_valid()</code> just ensures the magic number is there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload