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_
virtual void InstantiateBase(const Initializer &init)
Definition: property.h:105
boost::any value_
Definition: property.h:65
Initializer GetInitializerTemplate() override
Definition: property.h:121
bool IsStringType() const
Definition: property.cpp:46
bool IsSet() const
Definition: property.cpp:45
bool IsInitializerVectorType() const
Definition: property.cpp:47
std::vector< Initializer > GetAllTemplates() const override
Definition: property.h:126
boost::any Get() const
Definition: property.cpp:40
void Set(const C val)
Definition: property.h:49
bool IsRequired() const
Definition: property.cpp:44
const C & GetParameters() const
Definition: property.h:136
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
const std::string & GetName() const
Definition: property.cpp:48
virtual void Instantiate(const C &init)
Definition: property.h:131
std::string name_
Definition: property.h:67
void InstantiateInternal(const Initializer &init) override
Definition: property.h:113


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