The MAUVE Toolchain
HasProperty.hpp
1 /*
2  * Copyright 2017 ONERA
3  *
4  * This file is part of the MAUVE Runtime project.
5  *
6  * MAUVE Runtime is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 3 as
8  * published by the Free Software Foundation.
9  *
10  * MAUVE Runtime is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with MAUVE. If not, see <https://www.gnu.org/licenses/lgpl-3.0>.
17  */
18 #ifndef MAUVE_RUNTIME_HAS_PROPERTY_HPP
19 #define MAUVE_RUNTIME_HAS_PROPERTY_HPP
20 
21 #include "mauve/runtime/Configurable.hpp"
22 #include <string>
23 #include <vector>
24 
25 namespace mauve {
26  namespace runtime {
27 
28  class AbstractProperty;
29 
30  // ------------------------- Property type -------------------------
31 
32  template<typename T> class OtherProperty;
33  template<typename T> class IntegralProperty;
34  template<typename T> class FloatingPointProperty;
35  class StringProperty;
36 
37  template<bool I, bool F, bool S, typename T>
38  struct property_t { typedef OtherProperty<T> type; };
39 
40  template<bool F, bool S, typename T>
41  struct property_t<true, F, S, T> { typedef IntegralProperty<T> type; };
42 
43  template<bool I, bool S, typename T>
44  struct property_t<I, true, S, T> { typedef FloatingPointProperty<T> type; };
45 
46  template<bool I, bool F, typename T>
47  struct property_t<I, F, true, T> { typedef StringProperty type; };
48 
49  template<typename T>
50  using Property = typename property_t<std::is_integral<T>::value, std::is_floating_point<T>::value, std::is_same<T, std::string>::value, T>::type;
51 
52 
53  class HasProperty : virtual public Configurable {
54  public:
55  HasProperty();
56  virtual ~HasProperty() noexcept;
57 
62  const std::vector<AbstractProperty*> get_properties() const;
63 
64  std::size_t get_properties_size() const { return properties.size(); }
65 
66  AbstractProperty* get_property(std::string const & name) const;
67 
68  AbstractProperty* get_property(int index) const;
69 
70  protected:
78  template <typename T>
79  Property<T> & mk_property(std::string const & name, T init_value);
80 
81  private:
82  std::vector<AbstractProperty *> properties;
83  };
84 
85  // -------------------- Exceptions --------------------
86 
88  struct AlreadyDefinedProperty: public std::exception {
89  AlreadyDefinedProperty(std::string const & name) throw() : name(name) {}
91  const std::string name;
93  virtual const char* what() const throw() {
94  std::string message = "Already Defined Property " + name;
95  return message.c_str();
96  }
97  };
98 
99  }
100 }
101 
102 #include "mauve/runtime/ipp/HasProperty.ipp"
103 
104 #endif
Definition: HasProperty.hpp:34
virtual const char * what() const
Exception explanation.
Definition: HasProperty.hpp:93
Exception for Already Defined Properties.
Definition: HasProperty.hpp:88
Definition: HasProperty.hpp:32
Definition: HasProperty.hpp:53
Definition: HasProperty.hpp:33
The MAUVE namespace.
Definition: tracing.hpp:24
Configurable trait.
Definition: Configurable.hpp:25
const std::string name
Exception name.
Definition: HasProperty.hpp:91
Abstract property class.
Definition: Property.hpp:32
Definition: Property.hpp:147
Definition: HasProperty.hpp:38