$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 #ifndef CLOE_TRIGGER_SET_ACTION_HPP_
26 #define CLOE_TRIGGER_SET_ACTION_HPP_
27 
28 #include <memory> // for unique_ptr<>
29 #include <string> // for string
30 #include <utility> // for move
31 #include <vector> // for vector<>
32 
33 #include <cloe/trigger.hpp> // for Action, ActionFactory
34 #include <cloe/trigger/helper_macros.hpp> // for _X_FACTORY, _X_CALLBACK
35 
36 namespace cloe {
37 namespace actions {
38 
39 template <typename T>
40 T from_string(const std::string& s);
41 
42 template <>
43 double from_string<double>(const std::string& s) {
44  return std::stod(s);
45 }
46 
47 template <>
48 int from_string<int>(const std::string& s) {
49  return std::stoi(s);
50 }
51 
52 template <>
53 bool from_string<bool>(const std::string& s) {
54  if (s == "true") {
55  return true;
56  } else if (s == "false") {
57  return false;
58  } else {
59  throw std::out_of_range("cannot parse into boolean: " + s);
60  }
61 }
62 
63 // Set configuration attributes via triggers.
64 template <typename T>
65 class SetVariableAction : public Action {
66  public:
67  explicit SetVariableAction(const std::string& action_name, const std::string& data_name,
68  T* data_ptr, T value)
69  : Action(action_name), data_name_(data_name), data_ptr_(data_ptr), value_(value) {}
70  ActionPtr clone() const override {
71  return std::make_unique<SetVariableAction>(name(), data_name_, data_ptr_, value_);
72  }
73  void operator()(const Sync&, TriggerRegistrar&) override { *data_ptr_ = value_; }
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 actions
111 } // namespace cloe
112 
143 #define DEFINE_SET_STATE_ACTION(xName, xname, xdescription, xState, xOperatorBlock) \
144  class xName : public ::cloe::Action { \
145  public: \
146  xName(const std::string& name, xState* ptr) : ::cloe::Action(name), ptr_(ptr) {} \
147  ::cloe::ActionPtr clone() const override { return std::make_unique<xName>(name(), ptr_); } \
148  void operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { xOperatorBlock } \
149  void to_json(::cloe::Json&) const override {} \
150  \
151  private: \
152  xState* ptr_; \
153  }; \
154  \
155  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
156  public: \
157  using ActionType = xName; \
158  \
159  _X_FACTORY(xName)(xState * ptr) : ::cloe::ActionFactory(xname, xdescription), ptr_(ptr) {} \
160  \
161  ::cloe::ActionPtr make(const ::cloe::Conf&) const override { \
162  return std::make_unique<xName>(name(), ptr_); \
163  } \
164  \
165  ::cloe::ActionPtr make(const std::string&) const override { \
166  return std::make_unique<xName>(name(), ptr_); \
167  } \
168  \
169  private: \
170  xState* ptr_; \
171  };
172 
208 #define DEFINE_SET_DATA_ACTION(xName, xActionName, xActionDesc, xDataType, xAttributeName, \
209  xAttributeType, xOperatorBlock) \
210  class xName : public ::cloe::Action { \
211  public: \
212  xName(const std::string& action_name, xDataType* ptr, const std::string& attribute_name, \
213  const xAttributeType attribute_value) \
214  : ::cloe::Action(action_name) \
215  , ptr_(ptr) \
216  , name_(attribute_name) \
217  , value_(attribute_value) {} \
218  ::cloe::ActionPtr clone() const override { \
219  return std::make_unique<xName>(name(), ptr_, name_, value_); \
220  } \
221  void operator()(const ::cloe::Sync&, ::cloe::TriggerRegistrar&) override { xOperatorBlock } \
222  bool is_significant() const override { return false; } \
223  void to_json(::cloe::Json& j) const override { \
224  j = ::fable::Json{ \
225  {name_, value_}, \
226  }; \
227  } \
228  \
229  private: \
230  xDataType* ptr_; \
231  std::string name_; \
232  xAttributeType value_; \
233  }; \
234  \
235  class _X_FACTORY(xName) : public ::cloe::ActionFactory { \
236  public: \
237  using ActionType = xName; \
238  _X_FACTORY(xName) \
239  (xDataType * ptr) : ::cloe::ActionFactory(xActionName, xActionDesc), ptr_(ptr) {} \
240  \
241  ::cloe::ActionPtr make(const ::cloe::Conf& c) const override { \
242  auto value = c.get<xAttributeType>(xAttributeName); \
243  return std::make_unique<xName>(name(), ptr_, xAttributeName, value); \
244  } \
245  \
246  ::cloe::ActionPtr make(const std::string& s) const override { \
247  auto value = ::cloe::actions::from_string<xAttributeType>(s); \
248  return make(::fable::Conf{::fable::Json{ \
249  {xAttributeName, value}, \
250  }}); \
251  } \
252  \
253  private: \
254  xDataType* ptr_; \
255  };
256 
257 #endif // CLOE_TRIGGER_SET_ACTION_HPP_
Definition: conf.hpp:74
Definition: set_action.hpp:88
T get() const
Definition: conf.hpp:164
Definition: trigger.hpp:442
ActionPtr clone() const override
Definition: set_action.hpp:70
void operator()(const Sync &, TriggerRegistrar &) override
Definition: set_action.hpp:73
Definition: sync.hpp:35
Definition: trigger.hpp:616
Definition: coordinator.hpp:36
void to_json(Json &j) const override
Definition: set_action.hpp:75
bool is_significant() const override
Definition: set_action.hpp:74
ActionPtr make(const std::string &s) const override
Definition: set_action.hpp:98
Definition: trigger.hpp:300
std::string name() const
Definition: entity.hpp:68
ActionPtr make(const Conf &c) const override
Definition: set_action.hpp:94
Definition: set_action.hpp:65