Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslating a C++ API into a managed code API
    text
    copied!<p>I am trying to wrap a C++ library in Objective-C classes for importing into MonoTouch using btouch. I am trying to get to grips with how to translate the API for use in a managed environment. Specifically, how to handle methods that take in pointers to local variables, like the ULDatabaseManager::OpenConnection static method in the following example:</p> <pre><code>class UL_CLS_SPEC ULError { public: ULError(); /* Gets the error code (SQLCODE) for the last operation. */ inline an_sql_code GetSQLCode() const { return _info.sqlcode; } } class UL_CLS_SPEC ULDatabaseManager { public: /* Initializes the UltraLite runtime. */ static bool Init(); /* Finalizes the UltraLite runtime. */ static void Fini(); /* Opens a new connection to an existing database. */ static ULConnection * OpenConnection( const char * connParms, ULError * error = UL_NULL); } </code></pre> <p>From Objective-C, this C++ API would be called as follows:</p> <pre><code>- (void)openConnection { if (ULDatabaseManager::Init()) { const char * connectionParms; ULConnection * conn = nil; ULError error; connectionParms = [self getConnectionParms]; // Attempt connection to the database conn = ULDatabaseManager::OpenConnection(connectionParms, &amp;error); // If database file not found, create it and create the schema if (error.GetSQLCode() == SQLE_ULTRALITE_DATABASE_NOT_FOUND) { // Handle error } } } </code></pre> <p>In this API the caller of the OpenConnection static method is responsible for defining the ULError variable and passing it in as a reference pointer parameter. This paradigm does not seem to translate well into a managed environment, or at least to me it doesn't seem right to have the caller being responsible for instantiating the ULError object. What would be the best practice for translating this API into Objective-C classes? Which class would be responsible for creating and destroying the ULError object?</p> <p>I hope that my question makes sense, because I am very new to C++ and Objective-C (and MonoTouch for that matter) so am still at the point where I don't quite know what I don't know! :) And feeling a bit out of my depth here at the moment. So I would appreciate any advice or references to good articles that will help explaining how to wrap a C++ API.</p> <p>PS: I know this question pertains mainly to C++ and Objective-C, but I am including MonoTouch as a tag in case someone what has had experience with using btouch for importing Objective-C classes has some advice to offer.</p>
 

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