state_nested_array.h
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_NESTED_ARRAY_H
34 #define STATE_NESTED_ARRAY_H
35 
36 #include <float.h>
37 #include <memory>
38 
40 #include <tuw_control/utils.h>
41 
42 namespace tuw
43 {
49 template <typename SubState, size_t N>
51 template <typename SubState, size_t N>
52 using StateNestedArraySPtr = std::shared_ptr<StateNestedArray<SubState, N>>;
53 template <typename SubState, size_t N>
54 using StateNestedArrayConstSPtr = std::shared_ptr<StateNestedArray<SubState, N> const>;
55 template <typename SubState, size_t N>
56 using StateNestedArrayUPtr = std::unique_ptr<StateNestedArray<SubState, N>>;
57 template <typename SubState, size_t N>
58 using StateNestedArrayConstUPtr = std::unique_ptr<StateNestedArray<SubState, N> const>;
59 template <typename SubState, size_t N>
60 class StateNestedArray : public State
61 {
62 public:
63  StateNestedArray(State* _parent) : State(_parent)
64  {
65  for (size_t i = 0; i < N; ++i)
66  {
67  states_[i] = std::shared_ptr<SubState>(new SubState(this));
68  statesBase_[i] = states_[i];
69  }
70  this->callRootUpdateSize();
71  }
72 
73 public:
75  {
76  for (size_t i = 0; i < N; ++i)
77  {
78  states_[i] = std::shared_ptr<SubState>(new SubState(this));
79  statesBase_[i] = states_[i];
80  }
81  this->callRootUpdateSize();
82  }
83 
84 public:
85  virtual ~StateNestedArray() = default;
86 
87 public:
88  StateNestedArray(const StateNestedArray&) = default;
89 
90 public:
91  StateNestedArray& operator=(const StateNestedArray&) = default;
92 
93 public:
95 
96 public:
98 
99 public:
100  virtual StateSPtr cloneState() const override
101  {
102  return std::shared_ptr<StateNestedArray<SubState, N>>(new StateNestedArray<SubState, N>(*this));
103  }
104 
105 public:
106  size_t stateSize() const override
107  {
108  return N;
109  }
110 
111 public:
112  size_t valueSize() const override
113  {
114  return valueSize_;
115  }
116 
117 public:
118  double& value(const std::size_t& _i) override
119  {
120  return *values_[_i];
121  }
122 
123 public:
124  const double& value(const std::size_t& _i) const override
125  {
126  return *values_[_i];
127  }
128 
129 public:
130  StateSPtr& state(const std::size_t& _i) override
131  {
132  return statesBase_[_i];
133  }
134 
135 public:
136  std::shared_ptr<SubState>& stateScoped(const size_t& _i)
137  {
138  return this->states_[_i];
139  }
140 
141 public:
142  void updateSize() override
143  {
144  valueSize_ = 0;
145  for (auto& stateI : states_)
146  {
147  stateI->updateSize();
148  valueSize_ += stateI->valueSize();
149  }
150  values_.resize(valueSize_);
151  size_t valueSizeI, valueSizeSum = 0;
152  for (auto& stateI : states_)
153  {
154  valueSizeI = stateI->valueSize();
155  for (size_t i = 0; i < valueSizeI; ++i)
156  {
157  values_[valueSizeSum + i] = &(stateI->value(i));
158  }
159  valueSizeSum += valueSizeI;
160  }
161  }
162 
163 protected:
164  size_t valueSize_;
165 
166 protected:
167  size_t statesSize_;
168 
169 protected:
170  std::array<std::shared_ptr<SubState>, N> states_;
171 
172 protected:
173  std::array<StateSPtr, N> statesBase_;
174 
175 protected:
176  std::vector<double*> values_;
177 };
178 
185 template <typename EnumStateVals, typename SubState>
186 class StateNestedArrayScoped : public StateNestedArray<SubState, asInt(EnumStateVals::ENUM_SIZE)>
187 {
188  // special class member functions
189 public:
190  StateNestedArrayScoped(State* _parent) : StateNestedArray<SubState, asInt(EnumStateVals::ENUM_SIZE)>(_parent)
191  {
192  }
193 
194 public:
195  StateNestedArrayScoped() : StateNestedArray<SubState, asInt(EnumStateVals::ENUM_SIZE)>()
196  {
197  }
198 
199 public:
200  virtual ~StateNestedArrayScoped() = default;
201 
202 public:
204 
205 public:
207 
208 public:
210 
211 public:
213 
214  // implementation of virtual functions
215 public:
216  virtual StateSPtr cloneState() const override
217  {
218  return std::shared_ptr<StateNestedArrayScoped<EnumStateVals, SubState>>(new StateNestedArrayScoped<EnumStateVals, SubState>(*this));
219  }
221 public:
222  std::shared_ptr<StateNestedArrayScoped<EnumStateVals, SubState>> cloneStateExt() const
223  {
224  return std::shared_ptr<StateNestedArrayScoped<EnumStateVals, SubState>>(new StateNestedArrayScoped<EnumStateVals, SubState>(*this));
225  }
226  // public : template<EnumStateVals _i> double& value () { return StateNestedArray<SubState,
227  // asInt(EnumStateVals::ENUM_SIZE)>::value(asInt(_i)); }
228  // public : template<EnumStateVals _i> const double& value () const { return StateNestedArray<SubState,
229  // asInt(EnumStateVals::ENUM_SIZE)>::value(asInt(_i)); }
231 public:
232  template <EnumStateVals _i>
233  typename std::shared_ptr<SubState>& state()
234  {
235  return this->states_[asInt(_i)];
236  }
237 
238 public:
240 
241 public:
243 
244  template <typename... NestedStates1>
245  friend class StateNestedSet;
246  template <typename EnumStateVals1, typename... NestedStates1>
247  friend class StateNestedSetScoped;
248  template <typename SubState1>
249  friend class StateNestedVector;
250 };
251 }
252 
253 #endif // STATE_NESTED_ARRAY_H
std::array< StateSPtr, N > statesBase_
const double & value(const std::size_t &_i) const override
Const access state variable based on index _i.
std::shared_ptr< State > StateSPtr
Definition: state.h:129
double & value(const std::size_t &_i) override
Access state variable based on index _i.
size_t stateSize() const override
Size of the sub-states.
std::vector< double * > values_
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Definition: utils.h:87
std::shared_ptr< SubState > & stateScoped(const size_t &_i)
StateNestedArray(State *_parent)
size_t valueSize() const override
Size of the state variables.
Extension of StateNestedSet providing sub-state access based on a scoped enumeration (compile-time)...
StateNestedArrayScoped(State *_parent)
std::shared_ptr< StateNestedArray< SubState, N >> StateNestedArraySPtr
Implementation of State being formed by tuple of substates.
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
Definition: state.h:135
void updateSize() override
Performs internal manipulation when any of the underlying arrays are being resized.
std::unique_ptr< StateNestedArray< SubState, N >> StateNestedArrayUPtr
std::shared_ptr< SubState > & state()
Scoped access (compile-time) to the sub-states of the state object.
std::array< std::shared_ptr< SubState >, N > states_
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
std::unique_ptr< StateNestedArray< SubState, N > const > StateNestedArrayConstUPtr
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
StateSPtr & state(const std::size_t &_i) override
Access sub-state based on index _i.
StateNestedArray & operator=(const StateNestedArray &)=default
void callRootUpdateSize()
Calls (if present) the parent updateSize procedure. Otherwise performs root updateSize.
Definition: state.h:380
Extension of StateNestedArray providing sub-state access based on a scoped enumeration (compile-time)...
std::shared_ptr< StateNestedArrayScoped< EnumStateVals, SubState > > cloneStateExt() const
Clone-to-this-class-ptr function.
Implementation of State being formed by an array of substates.
virtual ~StateNestedArray()=default
std::shared_ptr< StateNestedArray< SubState, N > const > StateNestedArrayConstSPtr


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