$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 <memory> // for unique_ptr<>, shared_ptr<>
25 #include <string> // for string
26 #include <string_view> // for string_view
27 
28 #include <cloe/core/logger.hpp> // for logger::get
29 #include <cloe/registrar.hpp> // for cloe::Registrar
30 #include <cloe/stack_config.hpp> // for CLOE_TRIGGER_PATH_DELIMITER, ...
31 
32 #include "coordinator.hpp" // for Coordinator
33 #include "server.hpp" // for Server, ServerRegistrar
34 
35 namespace engine {
36 
37 class Registrar : public cloe::Registrar {
38  public:
39  Registrar(std::unique_ptr<ServerRegistrar> r, Coordinator* c, cloe::DataBroker* db)
40  : server_registrar_(std::move(r)), coordinator_(c), data_broker_(db) {}
41 
42  Registrar(const Registrar& ar,
43  const std::string& trigger_prefix,
44  const std::string& static_prefix,
45  const std::string& api_prefix)
46  : coordinator_(ar.coordinator_), data_broker_(ar.data_broker_) {
47  if (trigger_prefix.empty()) {
48  trigger_prefix_ = ar.trigger_prefix_;
49  } else {
50  trigger_prefix_ = ar.trigger_prefix_ + trigger_prefix;
51  }
52  server_registrar_ = ar.server_registrar_->with_prefix(static_prefix, api_prefix);
53  }
54 
55  void register_static_handler(const std::string& endpoint, cloe::Handler h) override {
56  server_registrar_->register_static_handler(endpoint, h);
57  }
58 
59  void register_api_handler(const std::string& endpoint,
61  cloe::Handler h) override {
62  server_registrar_->register_api_handler(endpoint, t, h);
63  }
64 
65  [[nodiscard]] std::unique_ptr<cloe::Registrar> clone() const {
66  return std::make_unique<Registrar>(*this, "", "", "");
67  }
68 
69  std::unique_ptr<cloe::Registrar> with_static_prefix(const std::string& prefix) const override {
70  assert(!prefix.empty());
71  return std::make_unique<Registrar>(*this, "", prefix, "");
72  }
73 
74  std::unique_ptr<cloe::Registrar> with_api_prefix(const std::string& prefix) const override {
75  assert(!prefix.empty());
76  return std::make_unique<Registrar>(*this, "", "", prefix);
77  }
78 
79  std::unique_ptr<cloe::Registrar> with_trigger_prefix(const std::string& prefix) const override {
80  assert(!prefix.empty() && prefix[0] != '_');
81  return std::make_unique<Registrar>(*this, prefix, "", "");
82  }
83 
84  [[nodiscard]] std::string make_prefix(std::string_view name, std::string_view delim) const {
85  assert(!name.empty());
86 
87  if (trigger_prefix_.empty()) {
88  // This only works for Cloe internal triggers.
89  return std::string(name);
90  }
91 
92  std::string prefix = trigger_prefix_;
93  if (name == "_") {
94  // Special case: "_" means we can actually use just trigger_prefix_.
95  // This might cause a problem if we name a plugin the same as one
96  // of the internal Cloe triggers...
97  return prefix;
98  }
99  prefix += delim;
100  prefix += name;
101  return prefix;
102  }
103 
104  [[nodiscard]] std::string make_trigger_name(std::string_view name) const {
105  return make_prefix(name, CLOE_TRIGGER_PATH_DELIMITER);
106  }
107 
108  [[nodiscard]] std::string make_signal_name(std::string_view name) const override {
109  auto sname = make_prefix(name, CLOE_SIGNAL_PATH_DELIMITER);
110  coordinator_->logger()->debug("Register signal: {}", sname);
111  return sname;
112  }
113 
114  void register_action(cloe::ActionFactoryPtr&& af) override {
115  coordinator_->register_action(make_trigger_name(af->name()), std::move(af));
116  }
117 
118  void register_event(
119  cloe::EventFactoryPtr&& ef, std::shared_ptr<cloe::Callback> storage) override {
120  coordinator_->register_event(make_trigger_name(ef->name()), std::move(ef), storage);
121  }
122 
123  sol::table register_lua_table() override {
124  return coordinator_->register_lua_table(trigger_prefix_);
125  }
126 
127  [[nodiscard]] cloe::DataBroker& data_broker() const override {
128  assert(data_broker_ != nullptr);
129  return *data_broker_;
130  }
131 
132  private:
133  std::unique_ptr<ServerRegistrar> server_registrar_;
134  Coordinator* coordinator_; // non-owning
135  cloe::DataBroker* data_broker_; // non-owning
136  std::string trigger_prefix_;
137 };
138 
139 } // namespace engine
Definition: data_broker.hpp:1217
Definition: registrar.hpp:121
Definition: coordinator.hpp:99
Definition: registrar.hpp:37
std::unique_ptr< cloe::Registrar > with_trigger_prefix(const std::string &prefix) const override
Definition: registrar.hpp:79
std::unique_ptr< cloe::Registrar > with_static_prefix(const std::string &prefix) const override
Definition: registrar.hpp:69
void register_api_handler(const std::string &endpoint, cloe::HandlerType t, cloe::Handler h) override
Definition: registrar.hpp:59
std::unique_ptr< cloe::Registrar > with_api_prefix(const std::string &prefix) const override
Definition: registrar.hpp:74
void register_static_handler(const std::string &endpoint, cloe::Handler h) override
Definition: registrar.hpp:55
sol::table register_lua_table() override
Definition: registrar.hpp:123
std::function< void(const Request &, Response &)> Handler
Definition: cloe_fwd.hpp:65
HandlerType
Definition: registrar.hpp:88