|
Boost Users : |
Subject: Re: [Boost-users] Command Line Options Questions
From: Daniel Hofmann (daniel_at_[hidden])
Date: 2016-03-07 11:28:32
Take a look at the following docs for the validate function:
>
http://www.boost.org/doc/libs/1_60_0/doc/html/program_options/howto.html#idm45555218221088
On 03/07/2016 02:32 PM, Jon Lederman wrote:
> Hi,
>
> I tried like this:
>
> std::ostream& operator<<(std::ostream& os, const dsp_type& obj)
> {
> // write obj to stream
> os << obj.value;
> return os;
> }
> std::istream& operator>>(std::istream& is, dsp_type& obj)
> {
> // read obj from stream
> if ( 0 /* T could not be constructed */ )
> is.setstate(std::ios::failbit);
> is >> obj.value;
> return is;
>
> }
>
> However now it letâs everything through and doesnât validate. What is the proper way to do this?
>
> -Jon
>> On Mar 6, 2016, at 3:50 PM, Daniel Hofmann <daniel_at_[hidden]> wrote:
>>
>> You have to implement operator<< otherwise the compiler does not know how to print (pipe it into an ostream) dsp_type.
>>
>> You can use Boost.Fusion's I/O feature and BOOST_ADAPT_STRUCT like this
>>> https://github.com/daniel-j-h/argparse-generic/blob/master/example.cc
>> to make your life easier.
>>
>> On March 6, 2016 5:43:27 PM GMT+01:00, Jon Lederman <jonlederman_at_[hidden]> wrote:
>>> Hi,
>>>
>>> I have two questions re: the boost command line options library:
>>>
>>> 1) I am compiling a dynamic library that links to the boost command
>>> line options library as a shared library. When I try to unload the
>>> dynamic library, I find that it isnât properly unloaded and the boost
>>> library seems to be causing the issue. I did find that things were
>>> helped somewhat when I linked to the boost static libraries. However,
>>> it still feels unstable. Are there any particular things I should know
>>> about in this context in terms of how my makefile should be linking to
>>> boost? Or are there other things I should look at.
>>>
>>> 2) When using program options, I am getting the following error. For
>>> some reason this seemed to work with the dynamic libraries (or it may
>>> be a coincidence). Am I required to implement the << and >> operators?
>>> It had been compiling previously and stopped. After googling this
>>> issue there seems to be a lot of confusion. Any help would be greatly
>>> appreciated.
>>>
>>>
>>>
>>> in file included from /usr/include/boost/any.hpp:27:0,
>>> from /usr/include/boost/program_options/value_semantic.hpp:12,
>>> from /usr/include/boost/program_options/options_description.hpp:13,
>>> from /usr/include/boost/program_options.hpp:15,
>>> from
>>> /usr/local/src/freeswitch/src/mod/applications/mod_sc_dsp/mod_sc_dsp.cpp:23:
>>> /usr/include/boost/lexical_cast.hpp: In instantiation of 'struct
>>> boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<dsp_type>
>>>> ':
>>> /usr/include/boost/lexical_cast.hpp:415:89: required from 'struct
>>> boost::detail::deduce_target_char<dsp_type>'
>>> /usr/include/boost/lexical_cast.hpp:674:92: required from 'struct
>>> boost::detail::lexical_cast_stream_traits<std::basic_string<char>,
>>> dsp_type>'
>>> /usr/include/boost/lexical_cast.hpp:2363:19: required from 'static
>>> Target boost::detail::lexical_cast_do_cast<Target,
>>> Source>::lexical_cast_impl(const Source&) [with Target = dsp_type;
>>> Source = std::basic_string<char>]'
>>> /usr/include/boost/lexical_cast.hpp:2543:50: required from 'Target
>>> boost::lexical_cast(const Source&) [with Target = dsp_type; Source =
>>> std::basic_string<char>]'
>>> /usr/include/boost/program_options/detail/value_semantic.hpp:89:38:
>>> required from 'void boost::program_options::validate(boost::any&, const
>>> std::vector<std::basic_string<charT> >&, T*, long int) [with T =
>>> dsp_type; charT = char]'
>>> /usr/include/boost/program_options/detail/value_semantic.hpp:170:55:
>>> required from 'void boost::program_options::typed_value<T,
>>> charT>::xparse(boost::any&, const std::vector<std::basic_string<charT>
>>>> &) const [with T = dsp_type; charT = char]'
>>> /usr/local/src/freeswitch/src/mod/applications/mod_sc_dsp/mod_sc_dsp.cpp:484:1:
>>> required from here
>>> /usr/include/boost/lexical_cast.hpp:388:13: error: static assertion
>>> failed: Target type is neither std::istream`able nor std::wistream`able
>>> BOOST_STATIC_ASSERT_MSG((result_t::value ||
>>> boost::has_right_shift<std::basic_istream<wchar_t>, T >::value),
>>>
>>>
>>> desc.add_options()
>>> ("help", "Generates help message")
>>> ("dsp_type", po::value<dsp_type>(¶ms.dsp_type)->required(), â")
>>>
>>> where dsp_type is this:
>>>
>>> struct dsp_type {
>>> dsp_type() {}
>>> dsp_type(std::string const& val):
>>> value(val)
>>> { }
>>>
>>> std::string value;
>>> };
>>>
>>> and I have a validator defined as this:
>>>
>>> void validate(boost::any& v,
>>> std::vector<std::string> const& values,
>>> dsp_type* /* target_type */,
>>> int)
>>> {
>>> using namespace boost::program_options;
>>> //namespace po = boost::program_options;
>>>
>>> // Make sure no previous assignment to 'v' was made.
>>> validators::check_first_occurrence(v);
>>>
>>> // Extract the first string from 'values'. If there is more than
>>> // one string, it's an error, and exception will be thrown.
>>> std::string const& s = validators::get_single_string(values);
>>>
>>> if (s == "ladspa" || s == "audioweaver" || s == "gstreamer" || s ==
>>> "lv2") {
>>> v = boost::any(dsp_type(s));
>>> } else {
>>> throw validation_error(validation_error::invalid_option_value);
>>> }
>>> }
>>>
>>>
>>>
>>>
>>>
>>> /usr/local/src/freeswitch/build/modmake.rules:214: recipe for target
>>> 'mod_sc_dsp.lo' failed
>>> make[1]: *** [mod_sc_dsp.lo] Error 1
>>>
>>> ------------------------------------------------------------------------
>>>
>>> _______________________________________________
>>> 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