PropertyMap: allow any type

This commit is contained in:
Robert Haschke 2018-06-01 06:37:28 +02:00
parent abea2ee82b
commit abc4c5de50
3 changed files with 16 additions and 3 deletions

View File

@ -218,6 +218,7 @@ public:
const std::string& description = "") {
return declare(name, typeid(T), description, default_value, &Property::serialize<T>);
}
/// declare all given properties also in other PropertyMap
void exposeTo(PropertyMap& other, const std::set<std::string>& properties);

View File

@ -52,7 +52,7 @@ Property::Property(const Property::type_index& type_index, const std::string& de
, serialize_(serialize)
{
// default value's type should match declared type by construction
assert(default_.empty() || default_.type() == type_index_);
assert(default_.empty() || default_.type() == type_index_ || type_index_ == typeid(boost::any));
}
void Property::setValue(const boost::any &value) {
@ -62,7 +62,7 @@ void Property::setValue(const boost::any &value) {
void Property::setCurrentValue(const boost::any &value)
{
if (!value.empty() && value.type() != type_index_)
if (!value.empty() && type_index_ != typeid(boost::any) && value.type() != type_index_)
throw Property::type_error(value.type().name(), type_index_.name());
value_ = value;
@ -112,7 +112,9 @@ Property& PropertyMap::declare(const std::string &name, const Property::type_ind
const Property::SerializeFunction &serialize)
{
auto it_inserted = props_.insert(std::make_pair(name, Property(type_index, description, default_value, serialize)));
if (!it_inserted.second && type_index != it_inserted.first->second.type_index_)
// if name was already declared, the new declaration should match in type (except it was boost::any)
if (!it_inserted.second && it_inserted.first->second.type_index_ != typeid(boost::any) &&
type_index != it_inserted.first->second.type_index_)
throw Property::type_error(type_index.name(), it_inserted.first->second.type_index_.name());
return it_inserted.first->second;
}

View File

@ -77,6 +77,16 @@ TEST(Property, reset) {
EXPECT_EQ(props.get<double>("double1"), 1.0);
}
TEST(Property, anytype) {
PropertyMap props;
props.declare<boost::any>("any", "store any type");
props.set("any", 1);
EXPECT_EQ(props.get<int>("any"), 1);
props.set("any", std::string("foo"));
EXPECT_EQ(props.get<std::string>("any"), "foo");
}
TEST(Property, serialize) {
PropertyMap props;
props.declare<int>("int");