Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:40:33+00:00 2026-05-26T23:40:33+00:00

I’m working on a message parser/generator subsystem. I’m creating an auto-generator that uses a

  • 0

I’m working on a message parser/generator subsystem. I’m creating an auto-generator that uses a database that contains all of the information about this protocol, including enum lists, to generate the code. One thing I came across is the need for hierarchical enumerations.

updated

(I was trying to simplify things by not describing the full problem, but the comments below make it obvious that I erred by simplifying too much.)

The Database being used will store things as simplified strings (customer decision), but the protocol only speaks “byte triplets” (aka Hierarchical Enum). The full problem could be described as such:

Given a set of unique strings that each correspond with a unique triplet, 1) find the triplet for any given string, and 2) find the string for any given triplet. Make sure to account for “Undefined” and “No Statement” enumerations (which do not have strings associated with them). [As one poster noted, yes it is insane.]

(Caveat: I’ve been doing C++ for well over a decade, but I’ve been doing Java this last year — my C++ is probably “corrupted”.)

So, to use an admittedly contrived example, given:

// There is only one category
// POP= "P", COUNTRY= "K", CLASSICAL= "C"
enum Category {POP, COUNTRY, CLASSICAL};

// There is one Type enum for each Category.
// ROCK= "R", BIG_BAND = "B", COUNTRY_POP= "C" 
enum PopType {ROCK, BIG_BAND, COUNTRY_POP};
enum CountryType {CLASSICAL_COUNTRY, MODERN_COUNTRY, BLUEGRASS, COUNTRY_AND_WESTERN};
// ...

// There is one Subtype for each Type
// EIGHTIES= "E", HEAVY_METAL= "H", SOFT_ROCK= "S"
enum RockSubType { EIGHTIES, HEAVY_METAL, SOFT_ROCK};
// ...

When I get 0, 0, 0 (Pop, Rock, Eighties), I need to translate that to “PRE”. Conversely, if I see “PC” in the Database, that needs to be sent out the wire as 0, 2 (Pop, Country, NULL).

I’m blatantly ignoring “Undefined” and No Statement” at this point. Generating a triplet from a string seems straight forward (use an unordered map, string to triple). Generating a string from a triplet (that may contain a NULL in the last entry) … not so much. Most of the “enum tricks” that I know won’t work: for instance, Types repeat values — each Type enum starts at zero — so I can’t index an array based on the Enum value to grab the string.

What’s got me is the relationship. At first glance it appears to be a fairly straight forward “is-a” relationship, but that doesn’t work because this case is bidirectional. The leaf -> root navigation is very straight forward, and would be appropriate for a class hierarchy; unfortunately, going the other way is not so straight forward.

I cannot “hand roll” this — I have to generate the code — so that probably eliminates any XML based solutions. It also has to be “reasonably fast”. The “Java Solution” involves using protected static variables, initialized on construction, and abstract base classes; however, I do not believe this would work in C++ (order of initialization, etc.). Plus, aesthetically, I feel this should be … more “const”. Other code I’ve seen that tackles this problem uses unions, explicitly listing all of the enum types in the union.

The only other thing I can come up with is using Template Specialization and explicit specialization, but I’m at a loss. I did a web search on this, but I found nothing that would tell me if it would even work. Still, if it can be done with a union, can’t it be done with Template Specialization?

Is it possible to do something like this using templates, specialization, explicit specialization? Is there another, more obvious, solution (i.e. a design pattern that I’ve forgotten) that I’m missing?

Oh, before I forget — the solution must be portable. More specifically, it must work on Windows (Visual Studio 2010) and Redhat Enterprise 6/Centos 6 (GCC 4.4.4 IIRC).

And, lest I forget, this protocol is huge. The theoretical max on this is about 133,000 entries; once I include “Undefined” and “No Statement” I’ll probably have that many entries.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:40:33+00:00Added an answer on May 26, 2026 at 11:40 pm

    First, thanks to everyone for their help. I wasn’t actually able to use any of the answers “as is” because of the nature of this problem:

    • enums repeat their values (every enum can have the same numerical values as it’s siblings, but with a different label and “meaning”)
    • strings associated with the enum can be repeated as well (a given enum can have the same string as a sibling, but with a different meaning).

    I eventually found Boost bimaps and it turns out that a bimap hierarchy works well for this problem. For those that haven’t seen them, Boost `bimap’ is a bidirectional container that uses either of the pair as key and the other as value.

    I can make a bimap of “integer, string” (uint8_t in this case, since the enums here are all guaranteed to be small) and add the, errr, “sub-enum”, as information associated with the bimap using with_info.

    The hierarchy code looks something like this:

    // Tags
    struct category_enum_value {};
    struct type_enum_value {};
    struct subtype_enum_value {};
    struct category_string {};
    struct music_type_string {};
    struct music_subtype_string {};
    struct music_type_info {};
    struct music_subtype_info {};
    
    // Typedefs
    typedef bimap<
        unordered_set_of< tagged<uint8_t, subtype_enum_value> >,
        unordered_set_of< tagged<std::string, music_subtype_string> >
    > music_subtype;
    typedef music_subtype::value_type music_subtype_value;
    
    typedef bimap<
        unordered_set_of< tagged<uint8_t, type_enum_value> >,
        unordered_set_of< tagged<std::string, music_type_string> >,
        with_info< tagged<music_subtype, music_subtype_info> >
    > music_type_type;
    typedef music_type_type::value_type music_type_value;
    
    typedef bimap<
        unordered_set_of< tagged<uint8_t, category_enum_value> >,
        unordered_set_of< tagged<std::string, category_string> >,
        with_info< tagged<music_type_type, music_type_info> > 
    > category_type;
    typedef category_type::value_type category_value;
    

    I chose unordered_set for performance reasons. Since this is strictly a “constant” hierarchy, I don’t have to worry about insertion and deletion times. And because I’ll never be comparing order, I don’t have to worry about sorting.

    To get the Category information by enum value (get string values when given the enum), I use the category_enum_value tag:

        category_type::map_by<category_enum_value>::iterator cat_it = categories.by<category_enum_value>().find(category);
    if(cat_it != categories.by<category_enum_value>().end())
    {
        const std::string &categoryString = cat_it->get_right();
                // ...
    

    I get the appropriate Type information from this by doing this, using the type_enum_value tag (subtype is nearly identical):

        music_type_type &music_type_reference = cat_it->get<music_type_info>();
        music_type_type::map_by<type_enum_value>::iterator type_it = music_type_reference.by<type_enum_value>().find(type);
        if(type_it != music_type_reference.by<type_enum_value>().end())
        {
                   // ... second verse, same as the first ...
    

    To get the enum values given the string, change the tag to category_string and use similar methods as before:

        std::string charToFind = stringToFind.substr(0, 1);
        category_type::map_by<category_string>::iterator cat_it = categories.by<category_string>().find(charToFind);
        if(cat_it != categories.by<category_string>().end())
        {
            retval.first = cat_it->get_left();
                        // ... and the beat goes on ...
    

    Any additional information that I need for any given level (say, menu item strings) can be added by changing the info type from a bimap to a struct containing a bimap and whatever information I might need.

    Since this is all constant values, I can do all the hard work “up front” and design simple look-up functions — O(1) — to get what I need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.