Thread: run time mapping of enums...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    run time mapping of enums...

    Hi,
    I have typedef'ed an enum with some elements in a particular order and am using them in my code.
    Now I have a need to typedef the same enum elements in a different order to use the same code for a different product. Could some one suggest the best way I can do this thing?

    For example:
    Code:
    typedef uint16 test_enum1_type;enum
    {
    xxx,
    YYY,
    ZZZ
    }
    Now I need the same elements in a different order,
    Code:
    typedef uint16 test_enum2_type;enum
    {
    YYY,
    xxx,
    ZZZ
    }
    Somehow I need to select required enum at run time based on the product type that I build.

    Let me know if I am not clear.

    Thanks!!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Somehow I need to select required enum at run time based on the product type that I build.
    'build' as in 'compile' ?...then you could use the preprocessor.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    Now going through my earlier message, it looked like I had a compile time option to select between products. Sorry for the confusion, that is not the case.
    I wish I had a preprocessor differentiating product type.But no, it just that the same software runs on both the products except for some minor changes.
    There is no way for me to differentiate products at compile time. But the is a way to differentiate during run time. That is why I am looking out for ways I can do this!

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Then you could use some particular characteristics of a particular product carefully, if it differs...
    Like...for example.. "sizeof(int)"...if you can find something differing..
    then depending on it...you can set a function pointer to use the dedicated function for that case...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Are you saying you have no preprocessor? Nothing to handle #ifdef's? Or that it's not an option to choose different #defines at compile time?

    If that's the case, you need a sort of "discovery" function that goes out and determines what "product" you are in - do you have such a function? Even then, I don't think an enum alone is your solution. You need some kind of configuration structure that you initialize after "discovery" to the enum values you want.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you had no preprocessor, you could not use any #include statements. That is highly unlikely if you're building something that's big enough to have multiple product types. Do you mean you don't know how to make the preprocessor select different blocks of code for different product types? Do you mean you have no control over the build process, makefiles, configuration files, etc and can't control the preprocessor flags?

    Almost certainly, if you have multiple product types, you have compile-time (preprocessor) flags that are set based on the product type, that you can use to select different blocks of code. Also, it's highly likely that you have run-time functions that identify the product type, but those won't allow you to change the enum order. You would have to use helper functions to get the value of XXX, YYY and ZZZ for a given product type.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    Quote Originally Posted by mike65535 View Post
    Are you saying you have no preprocessor? Nothing to handle #ifdef's? Or that it's not an option to choose different #defines at compile time?

    If that's the case, you need a sort of "discovery" function that goes out and determines what "product" you are in - do you have such a function? Even then, I don't think an enum alone is your solution. You need some kind of configuration structure that you initialize after "discovery" to the enum values you want.
    I have a function which determines the product type.
    Yes, I am planning to implement some configuration structure which at some point initializes the enum values based on the function return value.
    I am just seeing if there is any better way to handle this. Looks like the one you mentioned(same as one I was thinking) is the best way. I will go with that.
    Let me know if you happen to know of any other way.

    Thanks!!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tummala_005 View Post
    I have a function which determines the product type.
    Yes, I am planning to implement some configuration structure which at some point initializes the enum values based on the function return value.
    I am just seeing if there is any better way to handle this. Looks like the one you mentioned(same as one I was thinking) is the best way. I will go with that.
    Let me know if you happen to know of any other way.

    Thanks!!
    Ok, put both enums in your code, with different names. The discovery function can then set a pointer to the correct enum and your software can reference the pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serialization of Enums
    By khdani in forum C# Programming
    Replies: 1
    Last Post: 04-27-2009, 10:57 AM
  2. Function mapping at run-time
    By kris.c in forum C Programming
    Replies: 3
    Last Post: 03-24-2008, 08:48 AM
  3. Using enums in classes
    By eaane74 in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2007, 03:00 PM
  4. Enums and Templates
    By einarp in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2006, 05:07 AM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM