Boost logo

Boost Users :

Subject: Re: [Boost-users] Program Options: What would be involved in implementing add_options()(optionNameWork, po::value<std::pair<int, float>>() ?
From: Diederick C. Niehorster (dcnieho_at_[hidden])
Date: 2012-06-30 00:23:26


Hi Charles,

You would want to implement a method with the following signature.
There might be other options, but i've done this for my own class and
it does the trick. You should take the vecotr validator that comes
with boost.po as an example.

namespace boost { namespace program_options {

    template<class charT>
    void validate(boost::any& v,
                  const std::vector<std::basic_string<charT> >& s,
                  std::pair<​int, float>*,
                  int)
    {
        if (v.empty()) {
            v = boost::any(std::pair<​int, float>());
        }
        std::pair<​int, float>* tv = boost::any_cast< std::pair<​int,
float> >(&v);
        assert(NULL != tv);
        // do your parsing, write the result into tv
        // you can use
        boost::any a_int;
        validate(a_int, int_string, (int*)0, 0);
        // and
        boost::any a_float;
        validate(a_float, float_string, (float*)0, 0);
        // here to convert first and second
        // (this has the advantage of generating nice error
        // messages if the string is not convertible),
        // then assign to tv with
        boost::any_cast<T>(a_int); //etc
    }
}} // closing for namespaces

Partially hand-written, hope there are no mistakes.

I am not sure if all arguments behind the = in the config file get
passed in a single string to your function, but should be the case.
So, split (see split() and trim_copy_if() in
<boost/algorithm/string.hpp>), valicate each and write to output.

Then, simple declaring add_options()(optionNameWork,
po::value<std::pair<int, float>>() should work.

Not that there is a template involved, so you'll have to throw it in a
header somewhere. You could easily template one or both of the data
members of your std::pair as well to make your solution general.

Best,
Dee

On Sat, Jun 30, 2012 at 9:14 AM, Charles Mills <charlesm_at_[hidden]> wrote:
> I really like Boost Program Options.
>
> I came up with this wild hare of being able to handle something like
> foo=   int  float
>
> Obviously, I could do it by instead using
> foo_int=    int
> foo_float= float
>
> But what would be involved in being able to do something like
> add_options()(optionNameWork, po::value<std::pair<int, float>>() ?
>
> I would have to derive a class from boost::program_options::value_semantic?
> I would have to implement all of the abstract methods?
> The other classes also like abstract_variables_map?
> Or just variable_value?
>
> Volodya wrote in response to an earlier question "I would imagine this to be
> not hard. Either define operator>> for the class you use to represent
> rectangles, over override 'validate'"
>
> Is that what I should? Define operator>> for pair<int, float> ? Where is an
> example of a boost::program_options >> operator?
>
> Where is validate() declared?
>
> Sorry to be such a newbie at this level of C++ programming. You have to
> start somewhere.
>
> Charles
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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