0

I have a enum, SDKEnum which I need to convert into a AppEnum. I need to convert it, since my project shall be a wrapper around a .NET SDK and I cannot publish internal enums of that SDK. So I need to write my "own".

Now, for every enum I have I need a method that does the following:

AppEnum GetAppEnum (SDKEnum type)
{
  switch (type)
  {
    // Return the correct constant, since AppEnum and SDKEnum have equal constants
  }
}

That way I put a SDK enum into that method and get the corresponding App enum back.

I dont want to write that method for each enum I have.. . Is there a more generic or better way of doing this?

I'm pretty new to C++/CLI / C++ coming from C#.

Thanks a lot!

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Anon
  • 134
  • 1
  • 9
  • Ensure your AppEnum members have the same values as the native enum. Then it is simply a one-liner cast through *int*, like you'd do it in C# code, return (AppEnum)(int)type. Note that if you use VS2010 or higher then AppEnum requires `public enum class` to be declared properly, C++11 also adopted enum class and the only way the compiler can see the difference is through *public*. – Hans Passant Sep 27 '18 at 08:33
  • @HansPassant we use VS2010 but have not access to C++11 compiler. If I use public enum class, the compiler tells me that I am not allowed to mix managed and unmanaged code. So I assume he thinks public enum class is managed. Do I need to use the cast via (AppEnum)(int)type ? Would only (AppEnum)type work for my enum? – Anon Sep 27 '18 at 08:42
  • VS2010 had several C++11 features already implemented. The easy ones, "enum class" was easy. Not 100% sure, it has been too long ago. Just try this yourself to get a hard fact, the compiler always reminds you when it is wrong. – Hans Passant Sep 27 '18 at 08:56
  • @HansPassant right now, it seems like AppEnum and SDKEnum are not equal. They have different integer values for their constants. That means casting would create wrong ouputs, since the integers don't correspond. Any Idea how to fix that issue? – Anon Sep 27 '18 at 09:02
  • Quote: "Ensure your AppEnum members have the same values as the native enum". You do that the same way you do it in C#, use `=`. – Hans Passant Sep 27 '18 at 09:05
  • @HansPassant I dont know the values of the SDK enum. I have no access to the implementation.. – Anon Sep 27 '18 at 09:06
  • Of course you do, the compiler got the definition from a .h file. Enums do not have an implementation, only a definition. I don't remember if VS2010 had the "Go To Definition" right-click context menu command, just search the .h files if it doesn't. – Hans Passant Sep 27 '18 at 09:39
  • @HansPassant again, it is from a .NET SDK (C#). No .h file. However, I created a test app and output the integer values for each constant. Then I was able to synchronise the values of the enums. Now it works fine. Thanks for your time and help! – Anon Sep 27 '18 at 09:42

2 Answers2

1

I don't know how these enums are defined, but most likely they are either (old) C-style enums or (since C++11) strongly-typed enum classes.

The following code compiles, thus static_cast is your friend here:

namespace
{
    enum OLD_ENUM1 { A=1,B=2};
    enum OLD_ENUM2 { C=1,D=2};
    enum class NEW_ENUM1 { A=1,B=2};
    enum class NEW_ENUM2 { A=1,B=2};
}


int main()
{
    OLD_ENUM1 o1(A);
    OLD_ENUM2 o2(static_cast<OLD_ENUM2>(o1));
    NEW_ENUM1 n1(NEW_ENUM1::A);
    NEW_ENUM2 n2(static_cast<NEW_ENUM2>(n1));

    return EXIT_SUCCESS;
}

Note that for OLD_ENUM1 and OLD_ENUM2, I cannot use the same names for the enumeration values, since they are addressed without namespace. For the strongly-typed enums, I can do that because since they are classes, they have their own namespaces.

Demosthenes
  • 1,515
  • 10
  • 22
  • I found out that I can use `Regular Casts` to solve my problem. However, if I cast via Regular Cast like that: `AppEnum enum = (AppEnum)sdkenum;` I get wrong integers back. I checked if the result was correct: Both `AppEnum` and `SDKEnum` have a enum with integer number `8` as a constant. But I got a `5` from my method back. – Anon Sep 27 '18 at 08:31
  • So I guess the integer constants used for those enums in the SDK differ from those I set. – Anon Sep 27 '18 at 08:49
0

Alright, so what did the trick was the following regular cast:

enum SDKEnum
{
 One, // (2)
 Two, // (1)
 Three // (0)
}

enum AppEnum
{
 One,
 Two,
 Three
}

AppEnum MethoThatNeededTheConversion(SDKEnum sdkenum)
{
 // DoStuff
 return static_cast<AppEnum>(sdkenum); // Wrong int values returned
}

Important to note for me was: The SDKEnum had other integer values for the constants then my AppEnum. So I created a test application and gave out those integer values so I can sync both enums and use regular cast.

Thanks for the help @all

Anon
  • 134
  • 1
  • 9
  • You should never do a C-style cast in C++, your code is C, not C++. There is a reason C++ has type-aware casts. C-style casts are - in most cases - either a `static_cast` or `reinterpret_cast`. There's a substantial difference between those two, but if you're in doubt: if a `static_cast` compiles, you want a `static_cast`. – Demosthenes Sep 27 '18 at 14:45
  • @Demosthenes I read [this](https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast) post and thought, using C-Style cast would be okay. But I can understand your concerns. I change my answer and code accordingly. Thank you. – Anon Sep 28 '18 at 06:44