从 string/boost::any 映射构建 boost::options
我有一张代表配置的地图。它是 std::string
和 boost::any
的映射。
该地图在开始时初始化,我希望用户能够在命令行上覆盖这些选项。
我想做的是使用 options_description::add_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<???>, "");
});
办法这样构建它,还是我需要自己做?
提前致谢!
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 like the user to be able to override these options on the command line.
What I'd love to do is build the program options from this map using the options_description::add_option()
method. However, it takes a template argument po::value<>
whereas all I have is boost::any
.
So far, I just have the shell of the code. m_Config
represents my configuration class, and getTuples()
returns a std::map<std::string, Tuple>
. TuplePair
is a typedef of std::pair<std::string, Tuple>
and the Tuple contains the boost::any
I am interested in.
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<???>, "");
});
Is there a way to build it this way, or do I need to resort to doing it myself?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::any
不适用于您的问题。它执行最基本形式的类型擦除:存储和(类型安全)检索,仅此而已。正如您所看到的,无法执行其他操作。正如 jhasse 指出的那样,您可以只测试您想要支持的每种类型,但这是维护的噩梦。更好的方法是扩展 boost::any 使用的想法。不幸的是,这需要一些样板代码。如果您想尝试一下,现在邮件列表上正在讨论一个新的 Boost 库(标题为“[boost] RFC:类型擦除”),它本质上是一个通用类型擦除实用程序:您可以定义要执行的操作就像您要支持的擦除类型一样,它会生成正确的实用程序类型。 (它可以模拟
boost::any
,例如,通过要求擦除的类型是可复制构造和类型安全的,并且可以模拟boost::function<>
。除此之外,您最好的选择可能是自己编写这样的类型 我会为您做的:
如果有不清楚的地方请告诉我。 :)
boost::any
is not applicable to your problem. It performs the most basic form of type erasure: storage and (type-safe) retrieval, and that's it. As you've seen, no other operations can be performed. As jhasse points out, you could just test every type you want to support, but this is a maintenance nightmare.Better would be to expand upon the idea
boost::any
uses. Unfortunately this requires a bit of boiler-plate code. If you'd like to try it, there's a new Boost library being discussed right now on the mailing list (titled "[boost] RFC: type erasure") that is essentially a generalized type erasure utility: you define the operations you'd like your erased type to support, and it generates the proper utility type. (It can simulateboost::any
, for example, by requiring the erased type be copy-constructible and type-safe, and can simulateboost::function<>
by additionally requiring the type be callable.)Aside from that, though, your best option is probably to write such a type yourself. I'll do it for you:
Let me know if something is unclear. :)
如果您有多种类型,请考虑使用类型列表。
If you have more than a handful of types consider using typelists.