$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 #ifndef CLOE_REGISTRAR_HPP_
24 #define CLOE_REGISTRAR_HPP_
25 
26 #include <list> // for list<>
27 #include <memory> // for unique_ptr<>
28 #include <string> // for string
29 #include <utility> // for move
30 
31 #include <cloe/handler.hpp> // for Handler
32 #include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
33 
34 namespace cloe {
35 
44 template <typename E, typename... Ctx>
45 class DirectCallback : public Callback {
46  public:
47  size_t size() const { return triggers_.size(); }
48  bool empty() const { return triggers_.empty(); }
49 
50  void emplace(TriggerPtr&& t, const Sync&) override { triggers_.emplace_back(std::move(t)); }
51  void to_json(Json& j) const override { j = triggers_; }
52 
53  void trigger(const Sync& sync, const Ctx&... args) {
54  auto it = triggers_.begin();
55  while (it != triggers_.end()) {
56  auto& condition = dynamic_cast<E&>((*it)->event());
57  if (condition(sync, args...)) {
58  if ((*it)->is_sticky()) {
59  this->execute((*it)->clone(), sync);
60  } else {
61  // Remove from trigger list and advance.
62  this->execute(std::move(*it), sync);
63  it = triggers_.erase(it);
64  continue;
65  }
66  }
67  ++it;
68  }
69  }
70 
71  private:
72  std::list<TriggerPtr> triggers_;
73 };
74 
75 template <typename E, typename... Ctx>
76 using DirectCallbackPtr = std::shared_ptr<DirectCallback<E, Ctx...>>;
77 
84 enum class HandlerType {
89  STATIC,
90 
98  DYNAMIC,
99 
108  BUFFERED,
109 };
110 
117 class Registrar {
118  public:
119  virtual ~Registrar() noexcept = default;
120 
129  virtual void register_static_handler(const std::string& endpoint, Handler) = 0;
130 
139  virtual void register_api_handler(const std::string& endpoint, HandlerType, Handler) = 0;
140 
147  virtual std::unique_ptr<Registrar> with_api_prefix(const std::string& prefix) const = 0;
148 
155  virtual std::unique_ptr<Registrar> with_static_prefix(const std::string& prefix) const = 0;
156 
163  virtual std::unique_ptr<Registrar> with_trigger_prefix(const std::string& prefix) const = 0;
164 
168  virtual void register_action(std::unique_ptr<ActionFactory>&&) = 0;
169 
181  template <typename F, typename... Ctx>
182  void register_action(Ctx&&... ctx) {
183  register_action(std::make_unique<F>(std::forward<Ctx>(ctx)...));
184  }
185 
196  virtual void register_event(std::unique_ptr<EventFactory>&& f, std::shared_ptr<Callback> c) = 0;
197 
202  template <typename F, typename... Ctx>
203  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(std::unique_ptr<F>&& f) {
204  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
205  this->register_event(std::move(f), callback);
206  return callback;
207  }
208 
221  template <typename F, typename... Ctx, typename... Args>
222  DirectCallbackPtr<typename F::EventType, Ctx...> register_event(Args... args) {
223  auto callback = std::make_shared<DirectCallback<typename F::EventType, Ctx...>>();
224  this->register_event(std::make_unique<F>(args...), callback);
225  return callback;
226  }
227 };
228 
229 } // namespace cloe
230 
231 #endif // CLOE_REGISTRAR_HPP_
Definition: registrar.hpp:45
void emplace(TriggerPtr &&t, const Sync &) override
Definition: registrar.hpp:50
void execute(TriggerPtr &&t, const Sync &s)
Definition: trigger.cpp:111
Definition: registrar.hpp:117
HandlerType
Definition: registrar.hpp:84
std::function< void(const Request &, Response &)> Handler
Definition: handler.hpp:348
Definition: sync.hpp:35
Definition: coordinator.hpp:36
void register_action(Ctx &&... ctx)
Definition: registrar.hpp:182
void to_json(Json &j) const override
Definition: registrar.hpp:51
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(std::unique_ptr< F > &&f)
Definition: registrar.hpp:203
Definition: trigger.hpp:542
DirectCallbackPtr< typename F::EventType, Ctx... > register_event(Args... args)
Definition: registrar.hpp:222