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:
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);
}
From Objective-C, this C++ API would be called as follows:
- (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, &error);
// If database file not found, create it and create the schema
if (error.GetSQLCode() == SQLE_ULTRALITE_DATABASE_NOT_FOUND) {
// Handle error
}
}
}
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?
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.
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.
I think your best solution will be to create an objective-c class around the C++ class. Then binding with btouch should be very straightforward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With