C++ 从字符串生成boost::options/boost::any映射
我有一张表示配置的地图。它是C++ 从字符串生成boost::options/boost::any映射,c++,boost-program-options,boost-any,C++,Boost Program Options,Boost Any,我有一张表示配置的地图。它是std::string和boost::any的映射 此映射在开始时初始化,我希望用户能够在命令行上覆盖这些选项 我想做的是使用options\u description::add\u option()方法从这个映射构建程序选项。但是,它使用一个模板参数po::value,而我所拥有的只是boost::any 到目前为止,我只知道代码的外壳m_Config表示我的配置类,getTuples()返回一个std::mapTuplePair是std::pair的一个typed
std::string
和boost::any
的映射
此映射在开始时初始化,我希望用户能够在命令行上覆盖这些选项
我想做的是使用options\u description::add\u option()
方法从这个映射构建程序选项。但是,它使用一个模板参数po::value
,而我所拥有的只是boost::any
到目前为止,我只知道代码的外壳m_Config
表示我的配置类,getTuples()
返回一个std::map
TuplePair
是std::pair
的一个typedef,该Tuple包含我感兴趣的boost::any
po::options_description desc;
std::for_each(m_Config.getTuples().begin(),
m_Config.getTuples().end(),
[&desc](const TuplePair& _pair)
{
// what goes here? :)
// desc.add_options() ( _pair.first, po::value<???>, "");
});
po::选项\u描述说明;
std::for_each(m_Config.getTuples().begin(),
m_Config.getTuples().end(),
[&desc](常量TuplePair和\u对)
{
//这是什么?:)
//说明添加选项();
});
有没有办法这样做,还是我需要自己动手
提前谢谢 模板
template<class T>
bool any_is(const boost::any& a)
{
try
{
boost::any_cast<const T&>(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<int>(_pair.first))
{
desc.add_options() { _pair.first, po::value<int>, ""};
}
else if(any_is<std::string>(_pair.first))
{
desc.add_options() { _pair.first, po::value<std::string>, ""};
}
else
{
// ...
}
});
// ...
bool any_is(const boost::any&a)
{
尝试
{
boost::任意_cast(a);
返回true;
}
捕获(boost::bad\u any\u cast&)
{
返回false;
}
}
// ...
采购订单:选项描述说明;
std::for_each(m_Config.getTuples().begin(),
m_Config.getTuples().end(),
[&desc](常量TuplePair和\u对)
{
if(任意_为(_对第一))
{
描述添加选项(){u pair.first,po::value,“};
}
else if(任何_是(_pair.first))
{
描述添加选项(){u pair.first,po::value,“};
}
其他的
{
// ...
}
});
// ...
如果你有超过几种类型,考虑使用类型主义者。
<代码> Booo::任何不适用于你的问题。它执行最基本的类型擦除形式:存储和(类型安全)检索,就是这样。如您所见,无法执行其他操作。正如jhasse所指出的,您可以只测试您想要支持的每一种类型,但这是一场维护噩梦
更好的做法是扩展boost::any
使用的想法。不幸的是,这需要一点锅炉板代码。如果您想尝试一下,邮件列表上现在正在讨论一个新的Boost库(标题为“[Boost]RFC:type erasure”),它本质上是一个通用类型擦除实用程序:您定义希望擦除类型支持的操作,并生成适当的实用程序类型。(它可以模拟boost::any
,例如,通过要求被擦除的类型是可复制构造且类型安全的,并且可以通过另外要求该类型是可调用的来模拟boost::function
。)
不过,除此之外,最好的选择可能是自己编写这样的类型。我会为你做的:
#include <boost/program_options.hpp>
#include <typeinfo>
#include <stdexcept>
namespace po = boost::program_options;
class any_option
{
public:
any_option() :
mContent(0) // no content
{}
template <typename T>
any_option(const T& value) :
mContent(new holder<T>(value))
{
// above is where the erasure happens,
// holder<T> inherits from our non-template
// base class, which will make virtual calls
// to the actual implementation; see below
}
any_option(const any_option& other) :
mContent(other.empty() ? 0 : other.mContent->clone())
{
// note we need an explicit clone method to copy,
// since with an erased type it's impossible
}
any_option& operator=(any_option other)
{
// copy-and-swap idiom is short and sweet
swap(*this, other);
return *this;
}
~any_option()
{
// delete our content when we're done
delete mContent;
}
bool empty() const
{
return !mContent;
}
friend void swap(any_option& first, any_option& second)
{
std::swap(first.mContent, second.mContent);
}
// now we define the interface we'd like to support through erasure:
// getting the data out if we know the type will be useful,
// just like boost::any. (defined as friend free-function)
template <typename T>
friend T* any_option_cast(any_option*);
// and the ability to query the type
const std::type_info& type() const
{
return mContent->type(); // call actual function
}
// we also want to be able to call options_description::add_option(),
// so we add a function that will do so (through a virtual call)
void add_option(po::options_description desc, const char* name)
{
mContent->add_option(desc, name); // call actual function
}
private:
// done with the interface, now we define the non-template base class,
// which has virtual functions where we need type-erased functionality
class placeholder
{
public:
virtual ~placeholder()
{
// allow deletion through base with virtual destructor
}
// the interface needed to support any_option operations:
// need to be able to clone the stored value
virtual placeholder* clone() const = 0;
// need to be able to test the stored type, for safe casts
virtual const std::type_info& type() const = 0;
// and need to be able to perform add_option with type info
virtual void add_option(po::options_description desc,
const char* name) = 0;
};
// and the template derived class, which will support the interface
template <typename T>
class holder : public placeholder
{
public:
holder(const T& value) :
mValue(value)
{}
// implement the required interface:
placeholder* clone() const
{
return new holder<T>(mValue);
}
const std::type_info& type() const
{
return typeid(mValue);
}
void add_option(po::options_description desc, const char* name)
{
desc.add_options()(name, po::value<T>(), "");
}
// finally, we have a direct value accessor
T& value()
{
return mValue;
}
private:
T mValue;
// noncopyable, use cloning interface
holder(const holder&);
holder& operator=(const holder&);
};
// finally, we store a pointer to the base class
placeholder* mContent;
};
class bad_any_option_cast :
public std::bad_cast
{
public:
const char* what() const throw()
{
return "bad_any_option_cast: failed conversion";
}
};
template <typename T>
T* any_option_cast(any_option* anyOption)
{
typedef any_option::holder<T> holder;
return anyOption.type() == typeid(T) ?
&static_cast<holder*>(anyOption.mContent)->value() : 0;
}
template <typename T>
const T* any_option_cast(const any_option* anyOption)
{
// none of the operations in non-const any_option_cast
// are mutating, so this is safe and simple (constness
// is restored to the return value automatically)
return any_option_cast<T>(const_cast<any_option*>(anyOption));
}
template <typename T>
T& any_option_cast(any_option& anyOption)
{
T* result = any_option_cast(&anyOption);
if (!result)
throw bad_any_option_cast();
return *result;
}
template <typename T>
const T& any_option_cast(const any_option& anyOption)
{
return any_option_cast<T>(const_cast<any_option&>(anyOption));
}
// NOTE: My casting operator has slightly different use than
// that of boost::any. Namely, it automatically returns a reference
// to the stored value, so you don't need to (and cannot) specify it.
// If you liked the old way, feel free to peek into their source.
#include <boost/foreach.hpp>
#include <map>
int main()
{
// (it's a good exercise to step through this with
// a debugger to see how it all comes together)
typedef std::map<std::string, any_option> map_type;
typedef map_type::value_type pair_type;
map_type m;
m.insert(std::make_pair("int", any_option(5)));
m.insert(std::make_pair("double", any_option(3.14)));
po::options_description desc;
BOOST_FOREACH(pair_type& pair, m)
{
pair.second.add_option(desc, pair.first.c_str());
}
// etc.
}
#包括
#包括
#包括
名称空间po=boost::program\u选项;
将任何选项分类
{
公众:
任意选项():
mContent(0)//没有内容
{}
模板
任何_选项(常数和值):
M内容(新持有人(价值))
{
//上面是擦除发生的地方,
//holder继承自我们的非模板
//基类,它将进行虚拟调用
//到实际实现;见下文
}
任意选项(const任意选项和其他):
mContent(other.empty()?0:other.mContent->clone())
{
//注意我们需要一个显式的克隆方法来复制,
//因为用擦除的字体是不可能的
}
任意_选项&运算符=(任意_选项其他)
{
//“复制和交换”这个成语又短又甜
掉期(*本,其他);
归还*这个;
}
~any_option()
{
//完成后删除我们的内容
删除mContent;
}
bool empty()常量
{
返回!mContent;
}
朋友无效掉期(任意期权&第一,任意期权&第二)
{
std::swap(first.mContent,second.mContent);
}
//现在,我们定义希望通过擦除支持的接口:
//如果我们知道类型会很有用,就把数据拿出来,
//就像boost::any(定义为无好友函数)
模板
朋友T*任意选项(任意选项*);
//以及查询类型的能力
常量std::type_info&type()常量
{
返回mContent->type();//调用实际函数
}
//我们还希望能够调用选项描述::添加选项(),
//因此,我们添加了一个函数(通过虚拟调用)
void add_option(po::options_description desc,const char*name)
{
mContent->add_选项(desc,name);//调用实际函数
}
私人:
//完成了接口,现在我们定义了非模板基类,
//它有虚拟功能,我们需要类型擦除功能
类占位符
{
公众:
虚拟占位符()
{
//允许使用虚拟析构函数通过基删除
}
//支持任何_选项操作所需的接口:
//需要能够克隆存储的值
虚拟占位符*clone()常量=0;
//需要能够测试存储的类型,以便进行安全强制转换
虚拟常量std::type_info&type()常量=0;
//并且需要能够使用类型信息执行添加选项
虚拟无效添加选项(po::选项描述说明,
常量字符*名称)=0;
};
//以及模板派生类,该类将支持该接口
模板
类持有者:公共占位符
{
公众:
持有人(常数T和值):
mValue(值)
{}
//实现所需的接口:
砂醇