$darkmode
set_action.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 
26 #include <memory> // for unique_ptr<>
27 #include <string> // for string
28 #include <utility> // for move
29 #include <vector> // for vector<>
30 
31 #include <cloe/trigger.hpp> // for Action, ActionFactory
32 #include <cloe/trigger/helper_macros.hpp> // for _X_FACTORY, _X_CALLBACK
33 
34 namespace cloe {
35 namespace actions {
36 
37 template <typename T>
38 T from_string(const std::string& s);
39 
40 template <>
41 double from_string<double>(const std::string& s) {
42  return std::stod(s);
43 }
44 
45 template <>
46 int from_string<int>(const std::string& s) {
47  return std::stoi(s);
48 }
49 
50 template <>
51 bool from_string<bool>(const std::string& s) {
52  if (s == "true") {
53  return true;
54  } else if (s == "false") {
55  return false;
56  } else {
57  throw std::out_of_range("cannot parse into boolean: " + s);
58  }
59 }
60 
61 // Set configuration attributes via triggers.
62 template <typename T>
63 class SetVariableAction : public Action {
64  public:
65  explicit SetVariableAction(const std::string& action_name, const std::string& data_name,
66  T* data_ptr, T value)
67  : Action(action_name), data_name_(data_name), data_ptr_(data_ptr), value_(value) {}
68  ActionPtr clone() const override {
69  return std::make_unique<SetVariableAction>(name(), data_name_, data_ptr_, value_);
70  }
72  *data_ptr_ = value_;
73  return CallbackResult::Ok;
74  }
75  bool is_significant() const override { return false; }
76  void to_json(Json& j) const override {
77  j = Json{
78  {data_name_, value_},
79  };
80  }
81 
82  private:
83  std::string data_name_;
84  T* data_ptr_;
85  T value_;
86 };
87 
88 template <typename T>
90  public:
92  explicit SetVariableActionFactory(const std::string& action_name, const std::string& action_desc,
93  const std::string& data_name, T* data_ptr)
94  : ActionFactory(action_name, action_desc), data_name_(data_name), data_ptr_(data_ptr) {}
95  ActionPtr make(const Conf& c) const override {
96  auto value = c.get<T>(data_name_);
97  return std::make_unique<SetVariableAction<T>>(name(), data_name_, data_ptr_, value);
98  }
99  ActionPtr make(const std::string& s) const override {
100  auto value = from_string<T>(s);
101  return make(Conf{Json{
102  {data_name_, value},
103  }});
104  }
105 
106  private:
107  std::string data_name_;
108  T* data_ptr_;
109 };
110 
111 } // namespace actions
112 } // namespace cloe
113 
144 #define DEFINE_SET_STATE_ACTION(xName, xname, xdescription, xState, xOperatorBlock) \
145  class xName : public ::cloe::Action { \
146  public: \
147  xName(const std::string& name, xState* ptr) : ::cloe::Action(name), ptr_(ptr) {} \
148  ::cloe::ActionPtr clone() const override { return std::make_unique<xName>(name(), ptr_); } \
149  ::cloe::CallbackResult operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { \
150  xOperatorBlock; \
151  return ::cloe::CallbackResult::Ok; \
152  } \
153  void to_json(::cloe::Json&) const override {} \
154  \
155  private: \
156  xState* ptr_; \
157  }; \
158  \
159  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
160  public: \
161  using ActionType = xName; \
162  \
163  _X_FACTORY(xName)(xState * ptr) : ::cloe::ActionFactory(xname, xdescription), ptr_(ptr) {} \
164  \
165  ::cloe::ActionPtr make(const ::cloe::Conf&) const override { \
166  return std::make_unique<xName>(name(), ptr_); \
167  } \
168  \
169  ::cloe::ActionPtr make(const std::string&) const override { \
170  return std::make_unique<xName>(name(), ptr_); \
171  } \
172  \
173  private: \
174  xState* ptr_; \
175  };
176 
212 #define DEFINE_SET_DATA_ACTION(xName, xActionName, xActionDesc, xDataType, xAttributeName, \
213  xAttributeType, xOperatorBlock) \
214  class xName : public ::cloe::Action { \
215  public: \
216  xName(const std::string& action_name, xDataType* ptr, const std::string& attribute_name, \
217  const xAttributeType attribute_value) \
218  : ::cloe::Action(action_name) \
219  , ptr_(ptr) \
220  , name_(attribute_name) \
221  , value_(attribute_value) {} \
222  ::cloe::ActionPtr clone() const override { \
223  return std::make_unique<xName>(name(), ptr_, name_, value_); \
224  } \
225  ::cloe::CallbackResult operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { \
226  xOperatorBlock; \
227  return ::cloe::CallbackResult::Ok; \
228  } \
229  bool is_significant() const override { return false; } \
230  void to_json(::cloe::Json& j) const override { \
231  j = ::fable::Json{ \
232  {name_, value_}, \
233  }; \
234  } \
235  \
236  private: \
237  xDataType* ptr_; \
238  std::string name_; \
239  xAttributeType value_; \
240  }; \
241  \
242  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
243  public: \
244  using ActionType = xName; \
245  _X_FACTORY(xName) \
246  (xDataType * ptr) : ::cloe::ActionFactory(xActionName, xActionDesc), ptr_(ptr) {} \
247  \
248  ::cloe::ActionPtr make(const ::cloe::Conf& c) const override { \
249  auto value = c.get<xAttributeType>(xAttributeName); \
250  return std::make_unique<xName>(name(), ptr_, xAttributeName, value); \
251  } \
252  \
253  ::cloe::ActionPtr make(const std::string& s) const override { \
254  auto value = ::cloe::actions::from_string<xAttributeType>(s); \
255  return make(::fable::Conf{::fable::Json{ \
256  {xAttributeName, value}, \
257  }}); \
258  } \
259  \
260  private: \
261  xDataType* ptr_; \
262  };
Definition: trigger.hpp:619
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:437
Definition: set_action.hpp:89
ActionPtr make(const std::string &s) const override
Definition: set_action.hpp:99
ActionPtr make(const Conf &c) const override
Definition: set_action.hpp:95
Definition: set_action.hpp:63
ActionPtr clone() const override
Definition: set_action.hpp:68
bool is_significant() const override
Definition: set_action.hpp:75
CallbackResult operator()(const Sync &, TriggerRegistrar &) override
Definition: set_action.hpp:71
Definition: conf.hpp:76
T get() const
Definition: conf.hpp:166
CallbackResult
Definition: trigger.hpp:514