Boost logo

Boost Users :

From: Ryan Gallagher (ryan.gallagher_at_[hidden])
Date: 2008-04-23 01:06:58


Jeffery Cavallaro <jcavalla <at> google.com> writes:
[...]
> For example, if I wanted to a program to spit out all of its options
> to standard out, there doesn't seem to be any hooks to extract an Any
> value in a generic way (e.g., as a string).

Okay, this isn't exactly generic and only addresses this specific case
for the issue that you brought up, but for program_options where you
normally just have a limited number of types I've used:

...
for ( po::variables_map::const_iterator cur = _options.begin()
    , end = _options.end()
    ; cur != end
    ; ++cur) {
   #define CLI_print_for_type(Type) \
      else if (cur->second.value().type() == typeid(Type)) { \
         LogKeyValuePair( logger, option_entry_format \
                        , cur->first \
                        , boost::any_cast<Type>(cur->second.value())); \
      } // end macro definition

   if (cur->second.value().type() == typeid(void)) {
      logger.Write(str(option_entry_format % cur->first % "<empty>"));
   }
   CLI_print_for_type(int)
   CLI_print_for_type(bool)
   CLI_print_for_type(std::string)
   else {
      logger.Write(str(option_entry_format % cur->first % "<unknown>"));
   }

   #undef CLI_print_for_type
}
...

-Ryan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net