views:

135

answers:

2

BACKGROUND: In the API I am porting, there are a large number of functions prefixed with sqlite3_. They are encapsulated in a class named Sqlite3, so the function call is Sqlite3.sqlite3_...

I've created a number of alias calls, similar to the following,

//C# alias for call    
public static void result_error_toobig(sqlite3_context pCtx)
{ sqlite3_result_error_toobig(pCtx); }

//Native call   
public static void sqlite3_result_error_toobig(sqlite3_context pCtx)
{
  Debug.Assert(sqlite3_mutex_held(pCtx.s.db.mutex));
  pCtx.isError = SQLITE_ERROR;
  setResultStrOrError(pCtx, "string or blob too big", -1,
  SQLITE_UTF8, SQLITE_STATIC);
}

This allows me to write code, like Sqlite3.result_error_toobig(pCtx);

QUESTION:

  • Will the compiler optimize the call, so the overhead is minimal?
  • Is there an easier way to create this type of alias?
A: 

Will the compiler optimize the call, so the overhead is minimal?

Yes, the compiler will optimize the call and effectively remove your wrapper method. Edit: in release mode it will get optimized, debug mode will not.

Is there an easier way to create this type of alias?

Depending on the number of aliases you need to create, you could write a macro in Visual Studio to do the repetative work for you. Just depends on if it will take longer to write the macro or not.

pk

Paul Kearney - pk
+2  A: 

One time saver you could employ is using public delegate fields rather than creating methods for each function. As long as the parameter signatures are the same, you could do something like this:

public static class Sqlite3
{
    public static Action<sqlite3_context> ResultErrorTooBig =
        sqlite3_result_error_toobig;
    public static Func<T1, T2> AnotherMethod = 
        sqlite3_another_method;
}

Edit:

If you need to pass parameters by reference, you probably can't use those convenient Action and Func classes. However, you can declare your own delegate types, like this:

delegate int StatementDelegate(ref sqlite3_stmt pStmt);

Then, in your static Sqlite3 class, you could do something like this:

public static StatementDelegate Finalize = sqlite3_finalize;
Jacob
This is working well, thank you. A followup, if you have a moment. What is the proper syntax when the function has a by reference, such as public static int sqlite3_finalize(ref sqlite3_stmt pStmt)public static Func<ref sqlite3_stmt>Finalize = sqlite3_finalize; does not seem to work.
Noah
See my untested edit. Basically, you can define your own delegate type.
Jacob
+1 for followup
Noah