property.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #include <typeinfo>
31 #include <vector>
32 
33 #include <exotica_core/tools.h>
34 #include "exotica_core/property.h"
35 
36 namespace exotica
37 {
38 // * ///////////////////////////// Property //////////////////////////////////////
39 
40 boost::any Property::Get() const { return value_; }
41 Property::Property(const std::string& prop_name) : name_(prop_name), required_(true) {}
42 Property::Property(const std::string& prop_name, bool is_required) : name_(prop_name), required_(is_required) {}
43 Property::Property(const std::string& prop_name, bool is_required, boost::any val) : name_(prop_name), required_(is_required), value_(val) {}
44 bool Property::IsRequired() const { return required_; }
45 bool Property::IsSet() const { return !value_.empty(); }
46 bool Property::IsStringType() const { return value_.type() == typeid(std::string); }
47 bool Property::IsInitializerVectorType() const { return value_.type() == typeid(std::vector<exotica::Initializer>); }
48 const std::string& Property::GetName() const { return name_; }
49 std::string Property::GetType() const { return GetTypeName(value_.type()); }
50 Property::Property(std::initializer_list<boost::any> _val)
51 {
52  std::vector<boost::any> val(_val);
53  if (val.size() != 2 || val[0].type() != typeid(std::string)) ThrowPretty("Invalid property initialization!");
54  name_ = boost::any_cast<std::string>(val[0]);
55  value_ = val[1];
56 }
57 
58 // * ///////////////////////// InitializerBase ///////////////////////////////////
60 {
61 }
62 
63 Initializer::Initializer(const std::string& name) : name_(name)
64 {
65 }
66 
67 Initializer::Initializer(const std::string& name, const std::map<std::string, boost::any>& properties) : name_(name)
68 {
69  for (auto& prop : properties)
70  {
71  AddProperty(Property(prop.first, true, prop.second));
72  }
73 }
74 
75 const std::string& Initializer::GetName() const
76 {
77  return name_;
78 }
79 
81 {
82  if (HasProperty(prop.GetName()))
83  {
84  WARNING("Property '" << prop.GetName() << "' already added - overriding.");
85  SetProperty(prop.GetName(), prop.Get());
86  }
87  else
88  {
89  properties_.emplace(prop.GetName(), prop);
90  }
91 }
92 
93 bool Initializer::HasProperty(const std::string& name) const
94 {
95  return properties_.find(name) != properties_.end();
96 }
97 
98 boost::any Initializer::GetProperty(const std::string& name) const
99 {
100  return properties_.at(name).Get();
101 }
102 
103 void Initializer::SetProperty(const std::string& name, boost::any value)
104 {
105  properties_.at(name).Set(value);
106 }
107 
108 void Initializer::SetName(const std::string& name)
109 {
110  name_ = name;
111 }
112 
113 std::vector<std::string> Initializer::GetPropertyNames() const
114 {
115  return GetKeys(properties_);
116 }
117 } // namespace exotica
boost::any value_
Definition: property.h:65
std::vector< std::string > GetKeys(std::map< std::string, T > map)
Definition: tools.h:106
bool IsStringType() const
Definition: property.cpp:46
boost::any GetProperty(const std::string &name) const
Definition: property.cpp:98
bool IsSet() const
Definition: property.cpp:45
void SetProperty(const std::string &name, boost::any)
Definition: property.cpp:103
bool IsInitializerVectorType() const
Definition: property.cpp:47
#define ThrowPretty(m)
Definition: exception.h:36
const std::string & GetName() const
Definition: property.cpp:75
boost::any Get() const
Definition: property.cpp:40
bool IsRequired() const
Definition: property.cpp:44
void SetName(const std::string &name)
Definition: property.cpp:108
Property(const std::string &prop_name)
Definition: property.cpp:41
std::string GetType() const
Definition: property.cpp:49
std::string name_
Definition: property.h:85
std::map< std::string, Property > properties_
Definition: property.h:84
bool HasProperty(const std::string &name) const
Definition: property.cpp:93
void AddProperty(const Property &prop)
Definition: property.cpp:80
const std::string & GetName() const
Definition: property.cpp:48
std::vector< std::string > GetPropertyNames() const
Definition: property.cpp:113
std::string GetTypeName(const std::type_info &type)
Definition: tools.cpp:133
std::string name_
Definition: property.h:67
#define WARNING(x)
With endline.
Definition: printable.h:56


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49