Groups | Blog | Home
all groups > dotnet interop > august 2006 >

dotnet interop : C macro traslation in C# wrapper


Giorgio Tino
8/17/2006 11:08:14 AM
Hello everyone,

I'm writing a C# wrapper for a C library, and I needed some 'design' help.
What's the best way to translate to C# something like:

#define RECEIVED_ON_PORT_0 ( a ) (((a) & 0x00008000) == 0)
#define RECEIVED_ON_PORT_1 ( a ) (((a) & 0x00008000) != 0)


The C example is like:

int flags;

/* Some code that sets the flags */

if(RECEIVED_ON_PORT_0(flags))
{
/* Do something */
}
else if(RECEIVED_ON_PORT_1(flags))
{
/* Do something else */
}

I'd like to write in C# something like:

[Flags]
public enum ReceivedOn : int
{
Port0 = /*something*/;
Port1 = /*something else*/;
}

//
// ...
//

int flags

//
// Some code that sets the flags
//

if(flags & ReceivedOn.Port0)
{
/* Do something */
}
else if(flags & ReceivedOn.Port1)
{
/* Do something else */
}

ok, this code is just to give an idea of how my API should look like, don't
know if it is possible or if it works, but I'd like to use this in some way
to avoid a class with just two methods for this porpouse (but I'll do that
if this sounds ok...)... but probably this is a common problem and the
solution is out there, without reinventing the wheel... what do yuo think?

Thanks,
GT

Mattias Sjögren
8/20/2006 9:21:08 PM
[quoted text, click to view]

Well the most direct translation is to a function like this, but I
guess that's not quite what you're looking for.

bool ReceivedOnPort0(int a) { return (a & 0x8000) == 0; }



[quoted text, click to view]

If there are only two options (Port 0 and Port 1) I'd use a non-flags
enum like this

public enum ReceivedOn
{
Port0 = 0,
Port1 = 1
}

....
ReceivedOn ro = ...
if (ro == ReceivedOn.Port0) ...


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button