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