Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting from/to type Enum

Posted on 1999-06-29
12
Medium Priority
?
303 Views
Last Modified: 2010-04-02
I have an data structure of type Enum.  I'm trying to create a function which will take a string as a parameter (the string will be one of the items in the Enum), and return the Enumerator (the number).  I've gone in circles trying to accomplish this, and I know it involves some type of casting.  The compiler (MS Visual C++ 6.0) has been trying to tell me that my goal is unrealistic.  Do I have an invalid premise here?  If not, please give me some direction.
0
Comment
Question by:kpcapel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1198726
You have a container of class Enum, which contains some sort of objects. You want to search the conatiner for the occurrance of a key. The search should return an object of class Enumerator.
This sounds like something for STL's map.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198727
yes, use a map.
0
 

Author Comment

by:kpcapel
ID: 1198728
KangaRoo & nietod,

For me, this is easier said than done, but thanks for a new avenue to explore...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:ryanvs
ID: 1198729
It doesn't have to be difficult...

typedef enum tagStringValues {  svStr1 = 1, svStr2, svStr3 } EStringValues;

typedef struct tagStringMap {
    EStringValues  value;
    char          *pszString;
} StringMap;

StringMap  astStringMap[] = {
{ svStr1, "String 1" },
{ svStr2, "String 2" },
{ svStr3, "String 3" }
};

#define STRINGMAPSIZE  ( sizeof(astStringMap) / sizeof(astStringMap[0]) )

EStringValues StringLookup(char *pszString)
{
    int i;
    for (i = 0; i < STRINGMAPSIZE; i++)
    {
        if ( strcmp(astStringMap[i].pszString, pszString) == 0 )
            return astStringMap[i].value;
    }
   
    // Unknown String (Return 0 or -1, you chose)
    return 0;
}

0
 

Author Comment

by:kpcapel
ID: 1198730
You're right, it doesn't have to be difficult, and I would accept your answer if not for one issue which I really wanted to avoid.    

The enum in question can and will change over time (someone has to maintain it).  I really didn't want to list the same information twice, once in the enum and once in the StringMap structure proposed.  


0
 
LVL 22

Expert Comment

by:nietod
ID: 1198731
Sorry, there is no way to do that using built-in features of C/C++.  You need to have data structures like this.  A map would be better, but it would also need to be maintained.  What I do (as I have to do this and have 100s of these cases) is I have a pre-processor that scans my source code file looking for enum's (as well as many other things) and it "writes" a source file that contains tables and other information like this.  This other source file is then included into the main source file using an ordinary #include directive.  This is nearly automatic.  You don't have to do any of the work, but you do need to remember to run the preprocessor whenever you change things that depend on these tables.
0
 

Author Comment

by:kpcapel
ID: 1198732
neitod,

Thanks for the clarification.  I guess I knew that I was reaching...

ryanvs,

Seems I had no right to reject your answer.  My sincere appologies. Can I unreject it... anyone?
0
 
LVL 2

Accepted Solution

by:
ryanvs earned 400 total points
ID: 1198733
No apologies necessary, I understand your dilemma.  Depending on how dynamic the mapping structure is, you might consider loading the map from a text file or database table.  However, if you need to programmatically respond to certain values, this approach isn't quite as good.

For instance, if you are merely storing information such as auto manufacturer { Ford, Chevrolet, BMW, etc. }, this works well in a structure loaded from a database/text table.

If you need to do something special, such as credit inventory quantity when a product is returned (change from "Sold" to "Returned"), this approach does not work as well from a file/database because you need to respond to each and every value change.

Neitod's suggestion works well in this second case.

Good Luck


A little off the subject...
Neitod, I have a case where I need to translate some structures from Little Endian to Big Endian, transmit and receive, then convert from Big to Little.  I would like to build tables of the structures and their member data types, as you described, from preprocessor output but I'm not exactly sure how to do it.  The structures have a mix of characters, shorts, floats, ints, and arrays of other structs so I can't assume that I need to swap all the bytes.  Is this possible?  Thanks
0
 

Author Comment

by:kpcapel
ID: 1198734
Thanks to ryanvs for a solution, and thanks to the others for their insight.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1198735
Somewhere in BC's code I have seen amethod where they define an enumeration in a base class. The base class also has a lookup function that returns an associated string whith each enum value. Derived classses extend the enumeration and the lookup function! This makes maintenance easier.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198736
Can you "extend" an enumeration?  Or is this not a real "enum" enumeration.

>>  I would like to build tables of the structures and
>> their member data types, as you
>> described, from preprocessor output but I'm not
>> exactly sure how to do it.
I do that sort of thing--not for that reason.  Since parsing the C++ language is about the hardest thing in the world to do right, my preprocessor doesn't do it.  what i do is place all my structures and classes in comments using a simplified "defiition" language for the structure/class.  Then the preprocessor reads the information from the comments and generates the source code for the real c++ structure/class definitiion (places it in a .h file) and generates tables and data structures that record the class's structure and other information (these go in a seperate file that is included into the source code, not into the .h file.).
0
 

Expert Comment

by:MarvBudd
ID: 2667289
I think KangaRoo's reference is in chapter 7 of ATL Internals pub by Addison Wesley.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question