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