state_array.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef STATE_ARRAY_H
34 #define STATE_ARRAY_H
35 
36 #include <float.h>
37 #include <memory>
38 #include <array>
39 
40 #include <tuw_control/utils.h>
42 
43 namespace tuw
44 {
49 template <std::size_t N>
50 class StateArray;
51 template <std::size_t N>
52 using StateArraySPtr = std::shared_ptr<StateArray<N>>;
53 template <std::size_t N>
54 using StateArrayConstSPtr = std::shared_ptr<StateArray<N> const>;
55 template <std::size_t N>
56 using StateArrayUPtr = std::unique_ptr<StateArray<N>>;
57 template <std::size_t N>
58 using StateArrayConstUPtr = std::unique_ptr<StateArray<N> const>;
59 template <std::size_t N>
60 class StateArray : public State
61 {
62  // special class member functions
63 public:
64  StateArray(State* _parent) : State(_parent)
65  {
67  }
68 
69 public:
71  {
73  }
74 
75 public:
76  virtual ~StateArray() = default;
77 
78 public:
79  StateArray(const StateArray&) = default;
80 
81 public:
82  StateArray& operator=(const StateArray&) = default;
83 
84 public:
85  StateArray(StateArray&&) = default;
86 
87 public:
88  StateArray& operator=(StateArray&&) = default;
89 
90  // implementation of virtual functions
91 public:
92  virtual StateSPtr cloneState() const override
93  {
94  return std::shared_ptr<StateArray<N>>(new StateArray<N>(*this));
95  }
96 
97 public:
98  virtual double& value(const std::size_t& _i) override
99  {
100  return values_[_i];
101  }
102 
103 public:
104  virtual const double& value(const std::size_t& _i) const override
105  {
106  return values_[_i];
107  }
108 
109 public:
110  virtual size_t valueSize() const override
111  {
112  return N;
113  }
114 
116 public:
117  std::array<double, N>& valuesArray()
118  {
119  return values_;
120  }
122 public:
123  const std::array<double, N>& valuesArray() const
124  {
125  return values_;
126  }
127 
128 protected:
129  std::array<double, N> values_;
130 };
131 
137 template <typename EnumStateVals>
138 class StateArrayScoped : public StateArray<asInt(EnumStateVals::ENUM_SIZE)>
139 {
140  // special class member functions
141 public:
142  StateArrayScoped(State* _parent) : StateArray<asInt(EnumStateVals::ENUM_SIZE)>(_parent)
143  {
144  }
145 
146 public:
148  {
149  }
150 
151 public:
152  virtual ~StateArrayScoped() = default;
153 
154 public:
155  StateArrayScoped(const StateArrayScoped&) = default;
156 
157 public:
158  StateArrayScoped& operator=(const StateArrayScoped&) = default;
159 
160 public:
161  StateArrayScoped(StateArrayScoped&&) = default;
162 
163 public:
165 
166  // implementation of virtual functions
167 public:
168  virtual StateSPtr cloneState() const override
169  {
170  return std::shared_ptr<StateArrayScoped<EnumStateVals>>(new StateArrayScoped<EnumStateVals>(*this));
171  }
173 public:
174  std::shared_ptr<StateArrayScoped<EnumStateVals>> cloneStateExt() const
175  {
176  return std::shared_ptr<StateArrayScoped<EnumStateVals>>(new StateArrayScoped<EnumStateVals>(*this));
177  }
179 public:
180  template <EnumStateVals _i>
181  double& value()
182  {
184  }
186 public:
187  template <EnumStateVals _i>
188  const double& value() const
189  {
191  }
192 
193 public:
195 
196 public:
198 
199  template <typename... NestedStates1>
200  friend class StateNestedSet;
201  template <typename EnumStateVals1, typename... NestedStates1>
202  friend class StateNestedSetScoped;
203  template <typename SubState>
204  friend class StateNestedVector;
205 };
206 }
207 
208 #endif // STATE_ARRAY_H
virtual ~StateArray()=default
std::shared_ptr< State > StateSPtr
Definition: state.h:129
std::unique_ptr< StateArray< N > const > StateArrayConstUPtr
Definition: state_array.hpp:58
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
StateArray(State *_parent)
Definition: state_array.hpp:64
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
double & value()
Scoped access (compile-time) to the values of the state object.
Extension of StateArray providing value access based on a scoped enumeration (compile-time).
virtual const double & value(const std::size_t &_i) const override
Const access state variable based on index _i.
std::array< double, N > values_
State array container.
virtual size_t valueSize() const override
Size of the state variables.
Extension of StateNestedSet providing sub-state access based on a scoped enumeration (compile-time)...
virtual double & value(const std::size_t &_i) override
Access state variable based on index _i.
Definition: state_array.hpp:98
Implementation of State being formed by tuple of substates.
const double & value() const
Const scoped access (compile-time) to the values of the state object.
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
Definition: state.h:135
std::shared_ptr< StateArray< N > const > StateArrayConstSPtr
Definition: state_array.hpp:54
std::unique_ptr< StateArray< N >> StateArrayUPtr
Definition: state_array.hpp:56
const std::array< double, N > & valuesArray() const
Const reference to the variables array.
std::array< double, N > & valuesArray()
Reference to the state variables array.
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
Definition: state_array.hpp:92
std::shared_ptr< StateArrayScoped< EnumStateVals > > cloneStateExt() const
Clone-to-this-class-ptr function.
StateArrayScoped(State *_parent)
StateArray & operator=(const StateArray &)=default
void callRootUpdateSize()
Calls (if present) the parent updateSize procedure. Otherwise performs root updateSize.
Definition: state.h:380
Implementation of State for a fixed size array of double values.
Definition: state_array.hpp:50
std::shared_ptr< StateArray< N >> StateArraySPtr
Definition: state_array.hpp:52


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21