program_options custom type problem
пометка
1 сообщение - Свернуть все
/groups/adfetch?adid=LAIQWxEAAADgyKomqZkKkIk2WvcRagciFSRgCP-avRN4YT0eROC0jw
program_options custom type problem - Отправлять мне обновления по электронной почте  
Сообщение будет отправлено в группу Usenet. Когда Вы отправляете сообщения в такие группы, Ваш адрес электронной почты публикуется в Интернете.
Ваш ответ не был отправлен.
Сообщение отправлено успешно.
 
Автор:
Кому:
Копия:
В ответ на:
Добавить копию | Добавить заголовок "В ответ на" | Изменить тему
Тема:
Утверждение:
Для подтверждения введите символы, изображенные на картинке ниже, или цифры, которые вы услышите, нажав на значок упрощенного доступа. Слушайте и вводите услышанные числа
 
1.  Jacques Kerner  
Просмотреть профиль   Перевести на Переведено (просмотреть оригинал)
 Дополнительные параметры 22 дек 2003, 17:32
Автор: Jacques Kerner <jacques.ker...@oktal.fr>
Дата: Mon, 22 Dec 2003 16:32:23 +0100
Местное время: Пн. 22 дек 2003 17:32
Тема: [Boost-users] program_options custom type problem
Hi,

I am trying to use a custom type with the program_options library, but I
get the "invalid option value" error when running my application,
although inline parameters are correctly read. Here is what I do :

I define a class ApplicationLocator consisting of two integer members,
"site" and "application" and the corresponding >> and << operator
towards and from stringstream. The following method in lexical_cast.hpp
returns false :

            bool operator>>(InputStreamable &output)
            {
                return !is_pointer<InputStreamable>::value &&
                       stream >> output &&
                       (stream >> std::ws).eof();
            }

which results in an exception being thrown eventually, by this code
(also in lexical_cast.hpp) :

    template<typename Target, typename Source>
    Target lexical_cast(Source arg)
    {
        detail::lexical_stream<Target, Source> interpreter;
        Target result;

        if(!(interpreter << arg && interpreter >> result))
            throw_exception(bad_lexical_cast(typeid(Target),
typeid(Source)));
        return result;
    }

The problem is that my >> operator returns true alright. Maybe I am
wrong, but apparently what returns false is (stream >> std::ws).eof();
which I do not quite understand, let alone know how to avoid. Could
someone kindly give me guidance on this ? Here is my custom type related
code :

#include <strstream>
#include <boost/algorithm/string/split.hpp>
#include <boost/lambda/lambda.hpp>

class ApplicationLocator
{
public:
    ApplicationLocator() : site(0), application(0) {}
    ApplicationLocator(const int & aSite, const int & anApplication) :
site(aSite), application(anApplication) {}
    int site;
    int application;

};

bool operator>>(const std::stringstream & input, ApplicationLocator &output)
{
    std::vector<std::string>    splitString;
    boost::algorithm::split( splitString, std::string(input.str()),
boost::lambda::_1=='.' );
    if (splitString.size() > 1)
    {
        output.site         =    atoi(splitString[0].c_str());
        output.application  =    atoi(splitString[1].c_str());
        return true;
    }
    else
    {
        return true;
    }
    return true;

}

std::stringstream & operator<<(std::stringstream & output, const
ApplicationLocator & input)
{
    output   << input.site << "." << input.application << '\0';
    return output;

}

and the program_options related code :

    boost::program_options::positional_options_description
positional;      
    boost::program_options::variables_map configurationVariablesMap;

    try
    {
        boost::program_options::options_description desc("Allowed options");
        desc.add_options()
            ("help,h",                      "produce help message")
            ("self-id,s",                  
boost::program_options::value<ApplicationLocator>()->default_value(Applicat ionLocator(1,1)),
"This application's DIS identifier : site.application (separated by a dot)")
            ("instructor-id,i",            
boost::program_options::value<ApplicationLocator>()->default_value(Applicat ionLocator(2,1)),
"This instructor's DIS identifier : site.application (separated by a dot)")
        ;

boost::program_options::store(boost::program_options::command_line_parser(a rgc,
argv).options(desc).run(), configurationVariablesMap);
        boost::program_options::notify(configurationVariablesMap);

        if (configurationVariablesMap.count("help"))
        {
            std::cout << "Usage: options_description [options]\n";
            std::cout << desc;
            return 0;
        }
    }
    catch(exception& e)
    {
        std::cout << e.what() << std::endl;
        return 1;
    }  

Thank you all in advance.

Jacques Kerner
_______________________________________________
Boost-users mailing list
Boost-us...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


    Переслать  
Чтобы отправлять сообщения, сначала необходимо Войти.
Чтобы отправлять сообщения, сначала необходимо присоединиться к группе.
Перед отправкой сообщения обновите свой псевдоним на странице Настройки подписки.
У вас нет разрешения на отправку сообщений.

Создать группу - Группы Google - Главная страница Google - Условия предоставления услуг - Политика конфиденциальности
©2010 Google