$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::actions {
35 
36 template <typename T>
37 T from_string(const std::string& s);
38 
39 template <>
40 inline double from_string<double>(const std::string& s) {
41  return std::stod(s);
42 }
43 
44 template <>
45 inline int from_string<int>(const std::string& s) {
46  return std::stoi(s);
47 }
48 
49 template <>
50 inline bool from_string<bool>(const std::string& s) {
51  if (s == "true") {
52  return true;
53  } else if (s == "false") {
54  return false;
55  } else {
56  throw std::out_of_range("cannot parse into boolean: " + s);
57  }
58 }
59 
60 // Set configuration attributes via triggers.
61 template <typename T>
62 class SetVariableAction : public Action {
63  public:
64  explicit SetVariableAction(const std::string& action_name, const std::string& data_name,
65  T* data_ptr, T value)
66  : Action(action_name), data_name_(data_name), data_ptr_(data_ptr), value_(value) {}
67  ActionPtr clone() const override {
68  return std::make_unique<SetVariableAction>(name(), data_name_, data_ptr_, value_);
69  }
71  *data_ptr_ = value_;
72  return CallbackResult::Ok;
73  }
74  bool is_significant() const override { return false; }
75  void to_json(Json& j) const override {
76  j = Json{
77  {data_name_, value_},
78  };
79  }
80 
81  private:
82  std::string data_name_;
83  T* data_ptr_;
84  T value_;
85 };
86 
87 template <typename T>
89  public:
91  explicit SetVariableActionFactory(const std::string& action_name, const std::string& action_desc,
92  const std::string& data_name, T* data_ptr)
93  : ActionFactory(action_name, action_desc), data_name_(data_name), data_ptr_(data_ptr) {}
94  ActionPtr make(const Conf& c) const override {
95  auto value = c.get<T>(data_name_);
96  return std::make_unique<SetVariableAction<T>>(name(), data_name_, data_ptr_, value);
97  }
98  ActionPtr make(const std::string& s) const override {
99  auto value = from_string<T>(s);
100  return make(Conf{Json{
101  {data_name_, value},
102  }});
103  }
104 
105  private:
106  std::string data_name_;
107  T* data_ptr_;
108 };
109 
110 } // namespace cloe::actions
111 
142 #define DEFINE_SET_STATE_ACTION(xName, xname, xdescription, xState, xOperatorBlock) \
143  class xName : public ::cloe::Action { \
144  public: \
145  xName(const std::string& name, xState* ptr) : ::cloe::Action(name), ptr_(ptr) {} \
146  ::cloe::ActionPtr clone() const override { return std::make_unique<xName>(name(), ptr_); } \
147  ::cloe::CallbackResult operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { \
148  xOperatorBlock; \
149  return ::cloe::CallbackResult::Ok; \
150  } \
151  void to_json(::cloe::Json&) const override {} \
152  \
153  private: \
154  xState* ptr_; \
155  }; \
156  \
157  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
158  public: \
159  using ActionType = xName; \
160  \
161  _X_FACTORY(xName)(xState * ptr) : ::cloe::ActionFactory(xname, xdescription), ptr_(ptr) {} \
162  \
163  ::cloe::ActionPtr make(const ::cloe::Conf&) const override { \
164  return std::make_unique<xName>(name(), ptr_); \
165  } \
166  \
167  ::cloe::ActionPtr make(const std::string&) const override { \
168  return std::make_unique<xName>(name(), ptr_); \
169  } \
170  \
171  private: \
172  xState* ptr_; \
173  };
174 
210 #define DEFINE_SET_DATA_ACTION(xName, xActionName, xActionDesc, xDataType, xAttributeName, \
211  xAttributeType, xOperatorBlock) \
212  class xName : public ::cloe::Action { \
213  public: \
214  xName(const std::string& action_name, xDataType* ptr, const std::string& attribute_name, \
215  const xAttributeType attribute_value) \
216  : ::cloe::Action(action_name) \
217  , ptr_(ptr) \
218  , name_(attribute_name) \
219  , value_(attribute_value) {} \
220  ::cloe::ActionPtr clone() const override { \
221  return std::make_unique<xName>(name(), ptr_, name_, value_); \
222  } \
223  ::cloe::CallbackResult operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { \
224  xOperatorBlock; \
225  return ::cloe::CallbackResult::Ok; \
226  } \
227  bool is_significant() const override { return false; } \
228  void to_json(::cloe::Json& j) const override { \
229  j = ::fable::Json{ \
230  {name_, value_}, \
231  }; \
232  } \
233  \
234  private: \
235  xDataType* ptr_; \
236  std::string name_; \
237  xAttributeType value_; \
238  }; \
239  \
240  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
241  public: \
242  using ActionType = xName; \
243  _X_FACTORY(xName) \
244  (xDataType * ptr) : ::cloe::ActionFactory(xActionName, xActionDesc), ptr_(ptr) {} \
245  \
246  ::cloe::ActionPtr make(const ::cloe::Conf& c) const override { \
247  auto value = c.get<xAttributeType>(xAttributeName); \
248  return std::make_unique<xName>(name(), ptr_, xAttributeName, value); \
249  } \
250  \
251  ::cloe::ActionPtr make(const std::string& s) const override { \
252  auto value = ::cloe::actions::from_string<xAttributeType>(s); \
253  return make(::fable::Conf{::fable::Json{ \
254  {xAttributeName, value}, \
255  }}); \
256  } \
257  \
258  private: \
259  xDataType* ptr_; \
260  };
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:88
ActionPtr make(const std::string &s) const override
Definition: set_action.hpp:98
ActionPtr make(const Conf &c) const override
Definition: set_action.hpp:94
Definition: set_action.hpp:62
ActionPtr clone() const override
Definition: set_action.hpp:67
bool is_significant() const override
Definition: set_action.hpp:74
CallbackResult operator()(const Sync &, TriggerRegistrar &) override
Definition: set_action.hpp:70
Definition: conf.hpp:81
T get() const
Definition: conf.hpp:297
CallbackResult
Definition: trigger.hpp:514