mirror of
https://github.com/moveit/moveit_task_constructor.git
synced 2025-11-04 14:49:57 +08:00
PropertyMap: allow any type
This commit is contained in:
parent
abea2ee82b
commit
abc4c5de50
@ -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);
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user