1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// This class contains the value of the parsed option
// It also contains the map from string to enum used to know wich is wich
template<typename Enum>
struct EnumOption
{
public:
EnumOption(const Enum& newValue) : value(newValue) {}
Enum value;
static std::map<std::string, Enum> param_map;
};
template<typename Enum>
std::map<std::string, Enum> EnumOption<Enum>::param_map;
// This method is used to validate and parse the string and find the right enum
template<typename Enum>
void validate(boost::any& value,
const std::vector<std::string>& values,
EnumOption<Enum>*, int)
{
boost::program_options::validators::check_first_occurrence(value);
const std::string& valueAsString = boost::program_options::validators::get_single_string(values);
typename std::map<std::string,Enum>::const_iterator it = EnumOption<Enum>::param_map.find( valueAsString );
if ( it == EnumOption<Enum>::param_map.end() )
{
throw boost::program_options::validation_error("invalid value");
}
else
{
value = boost::any(EnumOption<Enum>(it->second));
}
} |
Partager