$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  }
71  void operator()(const Sync&, TriggerRegistrar&) override { *data_ptr_ = value_; }
72  bool is_significant() const override { return false; }
73  void to_json(Json& j) const override {
74  j = Json{
75  {data_name_, value_},
76  };
77  }
78 
79  private:
80  std::string data_name_;
81  T* data_ptr_;
82  T value_;
83 };
84 
85 template <typename T>
87  public:
89  explicit SetVariableActionFactory(const std::string& action_name, const std::string& action_desc,
90  const std::string& data_name, T* data_ptr)
91  : ActionFactory(action_name, action_desc), data_name_(data_name), data_ptr_(data_ptr) {}
92  ActionPtr make(const Conf& c) const override {
93  auto value = c.get<T>(data_name_);
94  return std::make_unique<SetVariableAction<T>>(name(), data_name_, data_ptr_, value);
95  }
96  ActionPtr make(const std::string& s) const override {
97  auto value = from_string<T>(s);
98  return make(Conf{Json{
99  {data_name_, value},
100  }});
101  }
102 
103  private:
104  std::string data_name_;
105  T* data_ptr_;
106 };
107 
108 } // namespace actions
109 } // namespace cloe
110 
141 #define DEFINE_SET_STATE_ACTION(xName, xname, xdescription, xState, xOperatorBlock) \
142  class xName : public ::cloe::Action { \
143  public: \
144  xName(const std::string& name, xState* ptr) : ::cloe::Action(name), ptr_(ptr) {} \
145  ::cloe::ActionPtr clone() const override { return std::make_unique<xName>(name(), ptr_); } \
146  void operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { xOperatorBlock } \
147  void to_json(::cloe::Json&) const override {} \
148  \
149  private: \
150  xState* ptr_; \
151  }; \
152  \
153  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
154  public: \
155  using ActionType = xName; \
156  \
157  _X_FACTORY(xName)(xState * ptr) : ::cloe::ActionFactory(xname, xdescription), ptr_(ptr) {} \
158  \
159  ::cloe::ActionPtr make(const ::cloe::Conf&) const override { \
160  return std::make_unique<xName>(name(), ptr_); \
161  } \
162  \
163  ::cloe::ActionPtr make(const std::string&) const override { \
164  return std::make_unique<xName>(name(), ptr_); \
165  } \
166  \
167  private: \
168  xState* ptr_; \
169  };
170 
206 #define DEFINE_SET_DATA_ACTION(xName, xActionName, xActionDesc, xDataType, xAttributeName, \
207  xAttributeType, xOperatorBlock) \
208  class xName : public ::cloe::Action { \
209  public: \
210  xName(const std::string& action_name, xDataType* ptr, const std::string& attribute_name, \
211  const xAttributeType attribute_value) \
212  : ::cloe::Action(action_name) \
213  , ptr_(ptr) \
214  , name_(attribute_name) \
215  , value_(attribute_value) {} \
216  ::cloe::ActionPtr clone() const override { \
217  return std::make_unique<xName>(name(), ptr_, name_, value_); \
218  } \
219  void operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { xOperatorBlock } \
220  bool is_significant() const override { return false; } \
221  void to_json(::cloe::Json& j) const override { \
222  j = ::fable::Json{ \
223  {name_, value_}, \
224  }; \
225  } \
226  \
227  private: \
228  xDataType* ptr_; \
229  std::string name_; \
230  xAttributeType value_; \
231  }; \
232  \
233  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
234  public: \
235  using ActionType = xName; \
236  _X_FACTORY(xName) \
237  (xDataType * ptr) : ::cloe::ActionFactory(xActionName, xActionDesc), ptr_(ptr) {} \
238  \
239  ::cloe::ActionPtr make(const ::cloe::Conf& c) const override { \
240  auto value = c.get<xAttributeType>(xAttributeName); \
241  return std::make_unique<xName>(name(), ptr_, xAttributeName, value); \
242  } \
243  \
244  ::cloe::ActionPtr make(const std::string& s) const override { \
245  auto value = ::cloe::actions::from_string<xAttributeType>(s); \
246  return make(::fable::Conf{::fable::Json{ \
247  {xAttributeName, value}, \
248  }}); \
249  } \
250  \
251  private: \
252  xDataType* ptr_; \
253  };
Definition: trigger.hpp:607
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:433
Definition: set_action.hpp:86
ActionPtr make(const std::string &s) const override
Definition: set_action.hpp:96
ActionPtr make(const Conf &c) const override
Definition: set_action.hpp:92
Definition: set_action.hpp:63
ActionPtr clone() const override
Definition: set_action.hpp:68
void operator()(const Sync &, TriggerRegistrar &) override
Definition: set_action.hpp:71
bool is_significant() const override
Definition: set_action.hpp:72
Definition: conf.hpp:82
T get() const
Definition: conf.hpp:298