Building boost::options from a string/boost::any map

后端 未结 2 1079
感情败类
感情败类 2020-12-08 17:58

I have a map which represents a configuration. It\'s a map of std::string and boost::any.

This map is initialized at the start and I\'d

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 18:20

    template
    bool any_is(const boost::any& a)
    {
        try
        {
            boost::any_cast(a);
            return true;
        }
        catch(boost::bad_any_cast&)
        {
            return false;
        }
    }
    
    // ...
    
        po::options_description desc;
        std::for_each(m_Config.getTuples().begin(),
                      m_Config.getTuples().end(),
                      [&desc](const TuplePair& _pair)
        {
            if(any_is(_pair.first))
            {
                desc.add_options() { _pair.first, po::value, ""};
            }
            else if(any_is(_pair.first))
            {
                desc.add_options() { _pair.first, po::value, ""};
            }
            else
            {
                // ...
            }
        });
    
    // ...
    

    If you have more than a handful of types consider using typelists.

提交回复
热议问题