
I have a question about the use of Any, especially when used in a larger facility such as program_options. There doesn't seem to be a (easy) way to post-process the results of a facility that has generically stuffed a bunch of values into a list of Any's. 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). Granted, I could derive a class and add methods to do so, but that doesn't work when Any is already embedded in the program_options implementation. I certainly could be missing something here. I haven't used Any or program_options yet, and trying to understand all of the possibilities from the spec-like doc is hard. How do the rest of you do such things? -- Jeffery Cavallaro, 1055 Joaquin-191A Infrastructure Engineering Google Inc. 1600 Amphitheatre Parkway Mountain View, CA 94043 650-214-2552

Jeffery Cavallaro
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

On Wed, Apr 23, 2008 at 05:06:58AM +0000, Ryan Gallagher wrote:
Jeffery Cavallaro
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:
I've done something similar as well. If you have a lot of types, and you want support for vectors of those types, you may want to look at the boost preprocessor macro library: ... #define TYPE_LIST ( unsigned char, ( unsigned short, ( unsigned int, ( unsigned long, ( signed char, ( signed short, ( signed int, ( signed long, ( bool, ( char, ( std::string, BOOST_PP_NIL))))))))))) #define MAKE_VECTOR_LIST( d, data, elem ) std::vector<elem> #define VECTOR_LIST BOOST_PP_LIST_TRANSFORM( MAKE_VECTOR_LIST, _, TYPE_LIST ) #define CAST_SIMPLE( r, data, elem ) if ( ti == typeid( elem ) ) { elem e = anyValue->as<elem>(); return boost::lexical_cast<string>( e ); } #define CAST_VECTOR( r, data, elem ) if ( ti == typeid( elem ) ) { /* iterate through the vector... */ } BOOST_PP_LIST_FOR_EACH(CAST_SIMPLE, _, TYPE_LIST); BOOST_PP_LIST_FOR_EACH(CAST_VECTOR, _, VECTOR_LIST); ... Andrew.

On Wed, Apr 23, 2008 at 05:06:58AM +0000, Ryan Gallagher wrote:
Jeffery Cavallaro
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:
I've done something similar as well. If you have a lot of types, and you want support for vectors of those types, you may want to look at the boost preprocessor macro library:
So, could somebody write a visit_any function / macro, or do we need to do some kind of hack each time. I am imagining a visit_any function that takes an mpl vector of types as an argument ... -- John Femiani

On 24/04/2008, John Femiani
On Wed, Apr 23, 2008 at 05:06:58AM +0000, Ryan Gallagher wrote:
Jeffery Cavallaro
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:
I've done something similar as well. If you have a lot of types, and you want support for vectors of those types, you may want to look at the boost preprocessor macro library:
So, could somebody write a visit_any function / macro, or do we need to do some kind of hack each time. I am imagining a visit_any function that takes an mpl vector of types as an argument ...
This has been discussed many times in the dev list I think, search for 'any visitor' in the archives. I posted a small utility some time ago as a base for discussion. It was named badly (any_variant), since I needed a visitor mechanism identical to the one in boost::variant , maybe it could be of some use to someone =) http://lists.boost.org/Archives/boost/2007/09/127126.php / Christian
-- John Femiani
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hello, I've got a class template that one of its members is boost::variant: template> class MyClass { boost::variant*bounded types*/> var_; }; At the stage of MyClass instantiation, I'd like to specify variant' bounded types list - which is variable. What's the most simple way to do this? Can I use mpl::list somehow - both as MyClass template parameter and in the variant specialization? Thank you. _________________________________________________________________ Explore the seven wonders of the world http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE

Jeffery Cavallaro schrieb:
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). Granted, I could derive a class and add methods to do so, but that doesn't work when Any is already embedded in the program_options implementation.
I made a wrapper class for boost::any (output_any) that allows to output
any type that is output streamable. This method is my poor man's
serialization method to convert a bunch of objects to a text
representation. A full code example is below.
Wouldn't it make sense to have something like output_any as a companion
to boost::any in the boost distribution?
Regards
James
BTW:
If runtime performance is more important then memory, the calls to
outputter_factory::get_outputter<T>() can be replaced by "new outputter<T>"
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include
participants (7)
-
Andrew Agno
-
Christian Holmquist
-
Igor R.
-
Jean-Pierre Bergamin
-
Jeffery Cavallaro
-
John Femiani
-
Ryan Gallagher