$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 <cloe/handler.hpp> // for Handler
30 #include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
31 #include <fable/json.hpp> // for Json
32 
33 namespace cloe {
34 
43 template <typename E, typename... Ctx>
44 class DirectCallback : public Callback {
45  public:
46  size_t size() const { return triggers_.size(); }
47  bool empty() const { return triggers_.empty(); }
48 
49  void emplace(TriggerPtr&& t, const Sync&) override { triggers_.emplace_back(std::move(t)); }
50  void to_json(fable::Json& j) const override { j = triggers_; }
51 
52  void trigger(const Sync& sync, const Ctx&... args) {
53  auto it = triggers_.begin();
54  while (it != triggers_.end()) {
55  auto& condition = dynamic_cast<E&>((*it)->event());
56  if (condition(sync, args...)) {
57  if ((*it)->is_sticky()) {
58  this->execute((*it)->clone(), sync);
59  } else {
60  // Remove from trigger list and advance.
61  this->execute(std::move(*it), sync);
62  it = triggers_.erase(it);
63  continue;
64  }
65  }
66  ++it;
67  }
68  }
69 
70  private:
71  std::list<TriggerPtr> triggers_;
72 };
73 
74 template <typename E, typename... Ctx>
75 using DirectCallbackPtr = std::shared_ptr<DirectCallback<E, Ctx...>>;
76 
83 enum class HandlerType {
88  STATIC,
89 
97  DYNAMIC,
98 
107  BUFFERED,
108 };
109 
116 class Registrar {
117  public:
118  virtual ~Registrar() noexcept = default;
119 
128  virtual void register_static_handler(const std::string& endpoint, Handler) = 0;
129 
138  virtual void register_api_handler(const std::string& endpoint, HandlerType, Handler) = 0;
139 
146  virtual std::unique_ptr<Registrar> with_api_prefix(const std::string& prefix) const = 0;
147 
154  virtual std::unique_ptr<Registrar> with_static_prefix(const std::string& prefix) const = 0;
155 
162  virtual std::unique_ptr<Registrar> with_trigger_prefix(const std::string& prefix) const = 0;
163 
167  virtual void register_action(std::unique_ptr<ActionFactory>&&) = 0;
168 
180  template <typename F, typename... Ctx>
181  void register_action(Ctx&&... ctx) {
182  register_action(std::make_unique<F>(std::forward<Ctx>(ctx)...));
183  }
184 
195  virtual void register_event(std::unique_ptr<EventFactory>&& f, std::shared_ptr<Callback> c) = 0;
196 
201  template <typename F, typename... Ctx>
202  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(std::unique_ptr<F>&& f) {
203  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
204  this->register_event(std::move(f), callback);
205  return callback;
206  }
207 
220  template <typename F, typename... Ctx, typename... Args>
221  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(Args... args) {
222  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
223  this->register_event(std::make_unique<F>(args...), callback);
224  return callback;
225  }
226 };
227 
228 } // namespace cloe
Definition: trigger.hpp:533
void execute(TriggerPtr &&t, const Sync &s)
Definition: trigger.cpp:111
Definition: registrar.hpp:44
void to_json(fable::Json &j) const override
Definition: registrar.hpp:50
void emplace(TriggerPtr &&t, const Sync &) override
Definition: registrar.hpp:49
Definition: registrar.hpp:116
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(Args... args)
Definition: registrar.hpp:221
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(std::unique_ptr< F > &&f)
Definition: registrar.hpp:202
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:181
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:58
nlohmann::json Json
Definition: fable_fwd.hpp:35
HandlerType
Definition: registrar.hpp:83