Using type_index to avoid RTTIs in program_options. (fixes #10347)

This commit is contained in:
Minmin Gong
2015-04-27 22:52:38 -07:00
committed by Vladimir Prus
parent 0efb385e99
commit 6feeeb3b92
3 changed files with 17 additions and 8 deletions

View File

@@ -12,11 +12,11 @@
#include <boost/any.hpp>
#include <boost/function/function1.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type_index.hpp>
#include <string>
#include <vector>
#include <typeinfo>
#include <limits>
namespace boost { namespace program_options {
@@ -163,6 +163,7 @@ namespace boost { namespace program_options {
bool m_zero_tokens;
};
#ifndef BOOST_NO_RTTI
/** Base class for all option that have a fixed type, and are
willing to announce this type to the outside world.
Any 'value_semantics' for which you want to find out the
@@ -174,17 +175,20 @@ namespace boost { namespace program_options {
public:
// Returns the type of the value described by this
// object.
virtual const std::type_info& value_type() const = 0;
virtual const boost::typeindex::type_info& value_type() const = 0;
// Not really needed, since deletion from this
// class is silly, but just in case.
virtual ~typed_value_base() {}
};
#endif
/** Class which handles value of a specific type. */
template<class T, class charT = char>
class typed_value : public value_semantic_codecvt_helper<charT>,
public typed_value_base
class typed_value : public value_semantic_codecvt_helper<charT>
#ifndef BOOST_NO_RTTI
, public typed_value_base
#endif
{
public:
/** Ctor. The 'store_to' parameter tells where to store
@@ -359,10 +363,12 @@ namespace boost { namespace program_options {
public: // typed_value_base overrides
const std::type_info& value_type() const
#ifndef BOOST_NO_RTTI
const boost::typeindex::type_info& value_type() const
{
return typeid(T);
return boost::typeindex::type_id<T>().type_info();
}
#endif
private:

View File

@@ -33,6 +33,7 @@ test-suite program_options :
[ po-test unrecognized_test.cpp ]
[ po-test required_test.cpp : required_test.cfg ]
[ po-test exception_txt_test.cpp ]
[ run options_description_test.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : options_description_no_rtti_test ]
;
exe test_convert : test_convert.cpp ;

View File

@@ -25,15 +25,17 @@ void test_type()
("bar", value<string>(), "")
;
#ifndef BOOST_NO_RTTI
const typed_value_base* b = dynamic_cast<const typed_value_base*>
(desc.find("foo", false).semantic().get());
BOOST_CHECK(b);
BOOST_CHECK(b->value_type() == typeid(int));
BOOST_CHECK(b->value_type() == boost::typeindex::type_id<int>());
const typed_value_base* b2 = dynamic_cast<const typed_value_base*>
(desc.find("bar", false).semantic().get());
BOOST_CHECK(b2);
BOOST_CHECK(b2->value_type() == typeid(string));
BOOST_CHECK(b2->value_type() == boost::typeindex::type_id<string>());
#endif
}
void test_approximation()