state_nested_set.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_SET_H
34 #define STATE_NESTED_SET_H
35 
36 #include <float.h>
37 #include <memory>
38 
40 #include <tuw_control/utils.h>
41 
42 namespace tuw
43 {
48 template <typename... NestedStates>
50 template <typename... NestedStates>
51 using StateNestedSetSPtr = std::shared_ptr<StateNestedSet<NestedStates...> >;
52 template <typename... NestedStates>
53 using StateNestedSetConstSPtr = std::shared_ptr<StateNestedSet<NestedStates...> const>;
54 template <typename... NestedStates>
55 using StateNestedSetUPtr = std::unique_ptr<StateNestedSet<NestedStates...> >;
56 template <typename... NestedStates>
57 using StateNestedSetConstUPtr = std::unique_ptr<StateNestedSet<NestedStates...> const>;
58 template <typename... NestedStates>
59 class StateNestedSet : public State
60 {
61  // special class member functions
62 public:
63  StateNestedSet(State* _parent) : State(_parent), isInit_(false)
64  {
65  states_ = std::make_tuple(std::shared_ptr<NestedStates>(new NestedStates(this))...);
66  statesBase_ = std::initializer_list<StateSPtr>{ std::shared_ptr<NestedStates>(new NestedStates(this))... };
67  isInit_ = true;
69  }
70 
71 public:
73  {
74  states_ = std::make_tuple(std::shared_ptr<NestedStates>(new NestedStates(this))...);
75  statesBase_ = std::initializer_list<StateSPtr>{ std::shared_ptr<NestedStates>(new NestedStates(this))... };
76  isInit_ = true;
78  }
79 
80 public:
81  virtual ~StateNestedSet() = default;
82 
83 public:
84  StateNestedSet(const StateNestedSet&) = default;
85 
86 public:
87  StateNestedSet& operator=(const StateNestedSet&) = default;
88 
89 public:
90  StateNestedSet(StateNestedSet&&) = default;
91 
92 public:
94 
95 public:
96  virtual StateSPtr cloneState() const override
97  {
98  return std::shared_ptr<StateNestedSet<NestedStates...> >( new StateNestedSet<NestedStates...>(*this));
99  }
100 
101 public:
102  size_t stateSize() const override
103  {
104  return statesSize_;
105  }
106 
107 public:
108  size_t valueSize() const override
109  {
110  return valueSize_;
111  }
112 
113 public:
114  double& value(const std::size_t& _i) override
115  {
116  return *values_[_i];
117  }
118 
119 public:
120  const double& value(const std::size_t& _i) const override
121  {
122  return *values_[_i];
123  }
124 
125 public:
126  StateSPtr& state(const std::size_t& _i) override
127  {
128  return statesBase_[_i];
129  }
130 
131 public:
132  void updateSize() override
133  {
134  if (!isInit_)
135  {
136  return;
137  }
138  valueSize_ = 0;
139  size_t _i = 0;
140  for_each_tuple(states_, [this, &_i](StateSPtr stateI)
141  {
142  statesBase_[_i] = stateI;
143  _i++;
144  stateI->updateSize();
145  valueSize_ += stateI->valueSize();
146  });
147  values_.resize(valueSize_);
148  size_t valSizeSum = 0;
149  auto funcBindVals = [this, &valSizeSum](StateSPtr stateI)
150  {
151  size_t valSizeI = stateI->valueSize();
152  for (size_t i = 0; i < valSizeI; ++i)
153  {
154  values_[valSizeSum + i] = &stateI->value(i);
155  }
156  valSizeSum += valSizeI;
157  };
158  for_each_tuple(states_, funcBindVals);
159  }
160 
161 protected:
162  size_t valueSize_;
163 
164 protected:
165  static constexpr const size_t statesSize_ = sizeof...(NestedStates);
166 
167 protected:
168  std::tuple<std::shared_ptr<NestedStates>...> states_;
169 
170 protected:
171  std::vector<StateSPtr> statesBase_;
172 
173 protected:
174  std::vector<double*> values_;
175 
176 protected:
177  bool isInit_;
178 };
179 
186 template <typename EnumStateVals, typename... NestedStates>
187 class StateNestedSetScoped : public StateNestedSet<NestedStates...>
188 {
189  // special class member functions
190 public:
191  StateNestedSetScoped(State* _parent) : StateNestedSet<NestedStates...>(_parent)
192  {
193  }
194 
195 public:
196  StateNestedSetScoped() : StateNestedSet<NestedStates...>()
197  {
198  }
199 
200 public:
201  virtual ~StateNestedSetScoped() = default;
202 
203 public:
204  StateNestedSetScoped(const StateNestedSetScoped&) = default;
205 
206 public:
208 
209 public:
211 
212 public:
214 
215 public:
216  using StateNestedSet<NestedStates...>::value;
217 
218 public:
219  using StateNestedSet<NestedStates...>::state;
220  // public : template<EnumStateVals _i> double& value () { return *this->values_[asInt(_i)]; }
221  // public : template<EnumStateVals _i> const double& value () const { return *this->values_[asInt(_i)]; }
223 public:
224  template <EnumStateVals _i>
225  typename std::tuple_element<asInt(_i), std::tuple<std::shared_ptr<NestedStates>...> >::type& state()
226  {
227  return std::get<asInt(_i)>(this->states_);
228  }
229 };
230 }
231 
232 #endif // STATE_NESTED_SET_H
std::shared_ptr< State > StateSPtr
Definition: state.h:129
StateNestedSet(State *_parent)
std::vector< double * > values_
StateSPtr & state(const std::size_t &_i) override
Access sub-state based on index _i.
std::unique_ptr< StateNestedSet< NestedStates... > const > StateNestedSetConstUPtr
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
std::shared_ptr< StateNestedSet< NestedStates... > const > StateNestedSetConstSPtr
StateNestedSetScoped(State *_parent)
static constexpr const size_t statesSize_
virtual ~StateNestedSet()=default
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple(std::tuple< Tp... > &, FuncT)
Definition: utils.h:101
const double & value(const std::size_t &_i) const override
Const access state variable based on index _i.
std::shared_ptr< StateNestedSet< NestedStates... > > StateNestedSetSPtr
Extension of StateNestedSet providing sub-state access based on a scoped enumeration (compile-time)...
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
size_t stateSize() const override
Size of the sub-states.
double & value(const std::size_t &_i) override
Access state variable based on index _i.
std::vector< StateSPtr > statesBase_
size_t valueSize() const override
Size of the state variables.
std::tuple_element< asInt(_i), std::tuple< std::shared_ptr< NestedStates >... > >::type & state()
Scoped access (compile-time) to the sub-states of the state object.
std::tuple< std::shared_ptr< NestedStates >... > states_
std::unique_ptr< StateNestedSet< NestedStates... > > StateNestedSetUPtr
void callRootUpdateSize()
Calls (if present) the parent updateSize procedure. Otherwise performs root updateSize.
Definition: state.h:380
void updateSize() override
Performs internal manipulation when any of the underlying arrays are being resized.
StateNestedSet & operator=(const StateNestedSet &)=default


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