$darkmode
registrar.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  */
22 #pragma once
23 
24 #include <list> // for list<>
25 #include <memory> // for unique_ptr<>
26 #include <string> // for string
27 #include <utility> // for move
28 
29 #include <sol/table.hpp>
30 #include <cloe/cloe_fwd.hpp> // for DataBroker
31 #include <cloe/handler.hpp> // for Handler
32 #include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
33 #include <fable/json.hpp> // for Json
34 
35 namespace cloe {
36 
45 template <typename E, typename... Ctx>
46 class DirectCallback : public Callback {
47  public:
48  size_t size() const { return triggers_.size(); }
49  bool empty() const { return triggers_.empty(); }
50 
51  void emplace(TriggerPtr&& t, const Sync&) override { triggers_.emplace_back(std::move(t)); }
52  void to_json(fable::Json& j) const override { j = triggers_; }
53 
54  void trigger(const Sync& sync, const Ctx&... args) {
55  auto it = triggers_.begin();
56  while (it != triggers_.end()) {
57  auto& condition = dynamic_cast<E&>((*it)->event());
58  if (condition(sync, args...)) {
59  if ((*it)->is_sticky()) {
60  auto result = this->execute((*it)->clone(), sync);
61  if (result == CallbackResult::Unpin) {
62  it = triggers_.erase(it);
63  }
64  } else {
65  // Remove from trigger list and advance.
66  this->execute(std::move(*it), sync);
67  it = triggers_.erase(it);
68  continue;
69  }
70  }
71  ++it;
72  }
73  }
74 
75  private:
76  std::list<TriggerPtr> triggers_;
77 };
78 
79 template <typename E, typename... Ctx>
80 using DirectCallbackPtr = std::shared_ptr<DirectCallback<E, Ctx...>>;
81 
88 enum class HandlerType {
93  STATIC,
94 
102  DYNAMIC,
103 
112  BUFFERED,
113 };
114 
121 class Registrar {
122  public:
123  virtual ~Registrar() noexcept = default;
124 
133  virtual void register_static_handler(const std::string& endpoint, Handler) = 0;
134 
143  virtual void register_api_handler(const std::string& endpoint, HandlerType, Handler) = 0;
144 
151  virtual std::unique_ptr<Registrar> with_api_prefix(const std::string& prefix) const = 0;
152 
159  virtual std::unique_ptr<Registrar> with_static_prefix(const std::string& prefix) const = 0;
160 
167  virtual std::unique_ptr<Registrar> with_trigger_prefix(const std::string& prefix) const = 0;
168 
172  virtual void register_action(std::unique_ptr<ActionFactory>&&) = 0;
173 
174  virtual DataBroker& data_broker() const = 0;
175 
187  template <typename F, typename... Ctx>
188  void register_action(Ctx&&... ctx) {
189  register_action(std::make_unique<F>(std::forward<Ctx>(ctx)...));
190  }
191 
202  virtual void register_event(std::unique_ptr<EventFactory>&& f, std::shared_ptr<Callback> c) = 0;
203 
207  virtual sol::table register_lua_table() = 0;
208 
213  template <typename F, typename... Ctx>
214  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(std::unique_ptr<F>&& f) {
215  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
216  this->register_event(std::move(f), callback);
217  return callback;
218  }
219 
232  template <typename F, typename... Ctx, typename... Args>
233  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(Args... args) {
234  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
235  this->register_event(std::make_unique<F>(args...), callback);
236  return callback;
237  }
238 };
239 
240 } // namespace cloe
Definition: trigger.hpp:545
CallbackResult execute(TriggerPtr &&t, const Sync &s)
Definition: trigger.cpp:111
Definition: data_broker.hpp:1217
Definition: registrar.hpp:46
void to_json(fable::Json &j) const override
Definition: registrar.hpp:52
void emplace(TriggerPtr &&t, const Sync &) override
Definition: registrar.hpp:51
Definition: registrar.hpp:121
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(Args... args)
Definition: registrar.hpp:233
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(std::unique_ptr< F > &&f)
Definition: registrar.hpp:214
virtual std::unique_ptr< Registrar > with_api_prefix(const std::string &prefix) const =0
virtual void register_api_handler(const std::string &endpoint, HandlerType, Handler)=0
virtual void register_static_handler(const std::string &endpoint, Handler)=0
virtual void register_event(std::unique_ptr< EventFactory > &&f, std::shared_ptr< Callback > c)=0
virtual std::unique_ptr< Registrar > with_static_prefix(const std::string &prefix) const =0
void register_action(Ctx &&... ctx)
Definition: registrar.hpp:188
virtual sol::table register_lua_table()=0
virtual std::unique_ptr< Registrar > with_trigger_prefix(const std::string &prefix) const =0
virtual void register_action(std::unique_ptr< ActionFactory > &&)=0
Definition: sync.hpp:34
std::function< void(const Request &, Response &)> Handler
Definition: cloe_fwd.hpp:65
nlohmann::json Json
Definition: fable_fwd.hpp:35
HandlerType
Definition: registrar.hpp:88