Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-03-26 10:27:04


Neal D. Becker wrote:

> Actually, just:
> ("angle", po::value(&angle))

Yea, you right :-)

> Now I'm trying to figure out how to handle an enum. I can't really find a
> good solution so far.

Well, you can specialize validator for the enum type and then use a map from
enumerator name to value.

Let me try to code it:

map<string, my_enum> my_enum_values;
bool demand_my_enum_values()
{
        my_enum_values["first"] = first;
        .....
        return true;
}

template<>
void validator<my_enum>::operator()(any& v, const vector<string>& xs)
    {
        // Initialize table of the first use.
        static bool b = demand_my_enum_values();

        check_first_occurence(v);
        string s(get_single_string(xs));
        if (my_enum_values.count(s) == 0)
           throw ....;
        else
           v = any<my_enum>(my_enum_values);
    }

Uhm... does not look overly nice. If we could steal Boost.Python syntax:

enum_<color>("color")
        .value("red", red)
        .value("green", green)
        ;

for describing enums and make validator use such descriptions, if you be
great. But Boost.Python's enum_ heavily depends on Boost.Python, it seems.

- Volodya


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk