Exotica
property.h
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 #ifndef EXOTICA_CORE_PROPERTY_H_
31 #define EXOTICA_CORE_PROPERTY_H_
32 
33 #include <map>
34 #include <memory>
35 #include <string>
36 
37 #include <boost/any.hpp>
38 #include <initializer_list>
39 
41 
42 namespace exotica
43 {
44 class Property
45 {
46 public:
47  boost::any Get() const;
48  template <typename C>
49  void Set(const C val)
50  {
51  value_ = val;
52  }
53  Property(const std::string& prop_name);
54  Property(const std::string& prop_name, bool is_required);
55  Property(const std::string& prop_name, bool is_required, boost::any val);
56  Property(std::initializer_list<boost::any> val);
57  bool IsRequired() const;
58  bool IsSet() const;
59  bool IsStringType() const;
60  bool IsInitializerVectorType() const;
61  const std::string& GetName() const;
62  std::string GetType() const;
63 
64 private:
65  boost::any value_;
66  bool required_;
67  std::string name_;
68 };
69 
71 {
72 public:
73  Initializer();
74  Initializer(const std::string& name);
75  Initializer(const std::string& name, const std::map<std::string, boost::any>& properties);
76  const std::string& GetName() const;
77  void SetName(const std::string& name);
78  void AddProperty(const Property& prop);
79  boost::any GetProperty(const std::string& name) const;
80  void SetProperty(const std::string& name, boost::any);
81  bool HasProperty(const std::string& name) const;
82  std::vector<std::string> GetPropertyNames() const;
83 
84  std::map<std::string, Property> properties_;
85  std::string name_;
86 };
87 
89 {
90 public:
91  InitializerBase() = default;
92  virtual ~InitializerBase() = default;
93  virtual void Check(const Initializer& other) const = 0;
94  virtual Initializer GetTemplate() const = 0;
95  virtual std::vector<Initializer> GetAllTemplates() const = 0;
96 };
97 
99 {
100 public:
101  InstantiableBase() = default;
102  virtual ~InstantiableBase() = default;
103  virtual Initializer GetInitializerTemplate() = 0;
104  virtual void InstantiateInternal(const Initializer& init) = 0;
105  virtual void InstantiateBase(const Initializer& init) {}
106  virtual std::vector<Initializer> GetAllTemplates() const = 0;
107 };
108 
109 template <class C, typename = typename std::enable_if<std::is_base_of<InitializerBase, C>::value, C>::type>
110 class Instantiable : public virtual InstantiableBase
111 {
112 public:
113  void InstantiateInternal(const Initializer& init) override
114  {
115  InstantiateBase(init);
116  const C tmp(init);
117  tmp.Check(init);
118  Instantiate(tmp);
119  }
120 
122  {
123  return C().GetTemplate();
124  }
125 
126  std::vector<Initializer> GetAllTemplates() const override
127  {
128  return C().GetAllTemplates();
129  }
130 
131  virtual void Instantiate(const C& init)
132  {
133  parameters_ = init;
134  }
135 
136  const C& GetParameters() const { return parameters_; }
137 
138 protected:
140 };
141 } // namespace exotica
142 
143 #endif // EXOTICA_CORE_PROPERTY_H_
exotica::Initializer::Initializer
Initializer()
exotica::Initializer::SetName
void SetName(const std::string &name)
exotica::Instantiable::parameters_
C parameters_
Definition: property.h:139
exotica::InstantiableBase::InstantiateBase
virtual void InstantiateBase(const Initializer &init)
Definition: property.h:105
exotica::InstantiableBase::InstantiateInternal
virtual void InstantiateInternal(const Initializer &init)=0
exotica::Initializer::GetProperty
boost::any GetProperty(const std::string &name) const
exotica::Initializer::GetPropertyNames
std::vector< std::string > GetPropertyNames() const
exotica::InitializerBase::InitializerBase
InitializerBase()=default
exotica::Property::IsInitializerVectorType
bool IsInitializerVectorType() const
exotica::Property::Property
Property(const std::string &prop_name)
exotica::Instantiable::Instantiate
virtual void Instantiate(const C &init)
Definition: property.h:131
exotica::Instantiable::GetParameters
const C & GetParameters() const
Definition: property.h:136
exotica::Instantiable::GetAllTemplates
std::vector< Initializer > GetAllTemplates() const override
Definition: property.h:126
exotica::Property::IsSet
bool IsSet() const
exotica::Property::Get
boost::any Get() const
exotica::InstantiableBase
Definition: property.h:98
exotica::InstantiableBase::~InstantiableBase
virtual ~InstantiableBase()=default
exotica::Instantiable
Definition: property.h:110
exotica
Definition: cartpole_dynamics_solver.h:38
conversions.h
exotica::InitializerBase::GetAllTemplates
virtual std::vector< Initializer > GetAllTemplates() const =0
exotica::Property::value_
boost::any value_
Definition: property.h:65
exotica::InstantiableBase::GetInitializerTemplate
virtual Initializer GetInitializerTemplate()=0
exotica::Property::IsRequired
bool IsRequired() const
exotica::InitializerBase
Definition: property.h:88
exotica::Initializer::properties_
std::map< std::string, Property > properties_
Definition: property.h:84
exotica::Instantiable::InstantiateInternal
void InstantiateInternal(const Initializer &init) override
Definition: property.h:113
exotica::Initializer::HasProperty
bool HasProperty(const std::string &name) const
exotica::Property::name_
std::string name_
Definition: property.h:67
exotica::Initializer::AddProperty
void AddProperty(const Property &prop)
exotica::Property::GetName
const std::string & GetName() const
exotica::Initializer::SetProperty
void SetProperty(const std::string &name, boost::any)
exotica::InstantiableBase::GetAllTemplates
virtual std::vector< Initializer > GetAllTemplates() const =0
exotica::Initializer
Definition: property.h:70
exotica::Initializer::name_
std::string name_
Definition: property.h:85
exotica::Instantiable::GetInitializerTemplate
Initializer GetInitializerTemplate() override
Definition: property.h:121
exotica::InstantiableBase::InstantiableBase
InstantiableBase()=default
exotica::InitializerBase::Check
virtual void Check(const Initializer &other) const =0
exotica::Property::required_
bool required_
Definition: property.h:66
exotica::Property
Definition: property.h:44
exotica::Initializer::GetName
const std::string & GetName() const
exotica::Property::Set
void Set(const C val)
Definition: property.h:49
exotica::InitializerBase::~InitializerBase
virtual ~InitializerBase()=default
exotica::InitializerBase::GetTemplate
virtual Initializer GetTemplate() const =0
exotica::Property::GetType
std::string GetType() const
exotica::Property::IsStringType
bool IsStringType() const