开发者

Translating a C++ API into a managed code API

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 ULE开发者_如何学Gorror {
    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.


The way that I resolved this is to change the API being exposed to Monotouch with btouch. Specifically, methods such as OpenConnection I only required the connectionParms, not the pointer to the error class. I then did error checking in the open method and returned nil if an error occurred. In the wrapper class I also kept a ULError object which I named lastConnectionError and my C++ code passed a pointer to this object with the OpenConnection C++ call. I included a separate method in my wrapper class, GetLastConnectionError, that returned an enum representing the different possible error states.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜