Skip to content

Commit 0d9509c

Browse files
committed
all: Improve DataBroker interface for common types
1 parent a29afa6 commit 0d9509c

File tree

7 files changed

+111
-78
lines changed

7 files changed

+111
-78
lines changed

engine/src/registrar.hpp

+33-16
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
#pragma once
2323

24-
#include <memory> // for unique_ptr<>, shared_ptr<>
25-
#include <string> // for string
24+
#include <memory> // for unique_ptr<>, shared_ptr<>
25+
#include <string> // for string
26+
#include <string_view> // for string_view
2627

27-
#include <cloe/registrar.hpp> // for cloe::Registrar
28+
#include <cloe/core/logger.hpp> // for logger::get
29+
#include <cloe/registrar.hpp> // for cloe::Registrar
2830

31+
#include "config.hpp" // for CLOE_TRIGGER_PATH_DELIMITER, ...
2932
#include "coordinator.hpp" // for Coordinator
3033
#include "server.hpp" // for Server, ServerRegistrar
3134

@@ -59,55 +62,69 @@ class Registrar : public cloe::Registrar {
5962
server_registrar_->register_api_handler(endpoint, t, h);
6063
}
6164

62-
std::unique_ptr<cloe::Registrar> clone() const {
65+
[[nodiscard]] std::unique_ptr<cloe::Registrar> clone() const {
6366
return std::make_unique<Registrar>(*this, "", "", "");
6467
}
6568

6669
std::unique_ptr<cloe::Registrar> with_static_prefix(const std::string& prefix) const override {
67-
assert(prefix.size() > 0);
70+
assert(!prefix.empty());
6871
return std::make_unique<Registrar>(*this, "", prefix, "");
6972
}
7073

7174
std::unique_ptr<cloe::Registrar> with_api_prefix(const std::string& prefix) const override {
72-
assert(prefix.size() > 0);
75+
assert(!prefix.empty());
7376
return std::make_unique<Registrar>(*this, "", "", prefix);
7477
}
7578

7679
std::unique_ptr<cloe::Registrar> with_trigger_prefix(const std::string& prefix) const override {
77-
assert(prefix.size() > 0 && prefix[0] != '_');
80+
assert(!prefix.empty() && prefix[0] != '_');
7881
return std::make_unique<Registrar>(*this, prefix, "", "");
7982
}
8083

81-
std::string trigger_key(const std::string& name) {
82-
assert(name.size() != 0);
84+
[[nodiscard]] std::string make_prefix(std::string_view name, std::string_view delim) const {
85+
assert(!name.empty());
8386

84-
if (trigger_prefix_.size() == 0) {
87+
if (trigger_prefix_.empty()) {
8588
// This only works for Cloe internal triggers.
86-
return name;
89+
return std::string(name);
8790
}
91+
92+
std::string prefix = trigger_prefix_;
8893
if (name == "_") {
8994
// Special case: "_" means we can actually use just trigger_prefix_.
9095
// This might cause a problem if we name a plugin the same as one
9196
// of the internal Cloe triggers...
92-
return trigger_prefix_;
97+
return prefix;
9398
}
94-
return trigger_prefix_ + "/" + name;
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;
95112
}
96113

97114
void register_action(cloe::ActionFactoryPtr&& af) override {
98-
coordinator_->register_action(trigger_key(af->name()), std::move(af));
115+
coordinator_->register_action(make_trigger_name(af->name()), std::move(af));
99116
}
100117

101118
void register_event(
102119
cloe::EventFactoryPtr&& ef, std::shared_ptr<cloe::Callback> storage) override {
103-
coordinator_->register_event(trigger_key(ef->name()), std::move(ef), storage);
120+
coordinator_->register_event(make_trigger_name(ef->name()), std::move(ef), storage);
104121
}
105122

106123
sol::table register_lua_table() override {
107124
return coordinator_->register_lua_table(trigger_prefix_);
108125
}
109126

110-
cloe::DataBroker& data_broker() const override {
127+
[[nodiscard]] cloe::DataBroker& data_broker() const override {
111128
assert(data_broker_ != nullptr);
112129
return *data_broker_;
113130
}

plugins/basic/src/basic.cpp

+3-38
Original file line numberDiff line numberDiff line change
@@ -397,45 +397,10 @@ class BasicController : public Controller {
397397
}
398398

399399
void enroll(Registrar& r) override {
400-
auto& db = r.data_broker();
401400
if (this->veh_) {
402-
auto& vehicle = this->veh_->name();
403-
{
404-
std::string name1 = fmt::format("vehicles.{}.{}.acc", vehicle, name());
405-
auto acc_signal = db.declare<cloe::controller::basic::AccConfiguration>(name1);
406-
acc_signal->set_getter<cloe::controller::basic::AccConfiguration>(
407-
[this]() -> const cloe::controller::basic::AccConfiguration& {
408-
return this->acc_.config;
409-
});
410-
acc_signal->set_setter<cloe::controller::basic::AccConfiguration>(
411-
[this](const cloe::controller::basic::AccConfiguration& value) {
412-
this->acc_.config = value;
413-
});
414-
}
415-
{
416-
std::string name1 = fmt::format("vehicles.{}.{}.aeb", vehicle, name());
417-
auto aeb_signal = db.declare<cloe::controller::basic::AebConfiguration>(name1);
418-
aeb_signal->set_getter<cloe::controller::basic::AebConfiguration>(
419-
[this]() -> const cloe::controller::basic::AebConfiguration& {
420-
return this->aeb_.config;
421-
});
422-
aeb_signal->set_setter<cloe::controller::basic::AebConfiguration>(
423-
[this](const cloe::controller::basic::AebConfiguration& value) {
424-
this->aeb_.config = value;
425-
});
426-
}
427-
{
428-
std::string name1 = fmt::format("vehicles.{}.{}.lka", vehicle, name());
429-
auto lka_signal = db.declare<cloe::controller::basic::LkaConfiguration>(name1);
430-
lka_signal->set_getter<cloe::controller::basic::LkaConfiguration>(
431-
[this]() -> const cloe::controller::basic::LkaConfiguration& {
432-
return this->lka_.config;
433-
});
434-
lka_signal->set_setter<cloe::controller::basic::LkaConfiguration>(
435-
[this](const cloe::controller::basic::LkaConfiguration& value) {
436-
this->lka_.config = value;
437-
});
438-
}
401+
r.declare_signal("acc", &acc_.config);
402+
r.declare_signal("aeb", &aeb_.config);
403+
r.declare_signal("lka", &lka_.config);
439404
}
440405

441406
auto lua = r.register_lua_table();

plugins/speedometer/src/speedometer.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,19 @@ struct SpeedometerConf : public fable::Confable {
3838

3939
class Speedometer : public cloe::Component {
4040
public:
41-
Speedometer(const std::string& name, const SpeedometerConf&, std::shared_ptr<cloe::EgoSensor> ego)
41+
Speedometer(const std::string& name, const SpeedometerConf& /*conf*/,
42+
std::shared_ptr<cloe::EgoSensor> ego)
4243
: Component(name, "provides an event trigger to evaluate speed in km/h"), sensor_(ego) {}
4344

44-
virtual ~Speedometer() noexcept = default;
45+
~Speedometer() noexcept override = default;
4546

4647
void enroll(cloe::Registrar& r) override {
4748
callback_kmph_ =
4849
r.register_event<cloe::events::EvaluateFactory, double>("kmph", "vehicle speed in km/h");
4950

50-
auto& db = r.data_broker();
51-
{
52-
std::string signal_name = fmt::format("components.{}.kmph", name());
53-
auto signal = db.declare<double>(signal_name);
54-
signal->set_getter<double>(
55-
[this]() -> double { return cloe::utility::EgoSensorCanon(sensor_).velocity_as_kmph(); });
56-
}
51+
auto kmph_signal = r.declare_signal<double>("kmph");
52+
kmph_signal->set_getter<double>(
53+
[this]() -> double { return cloe::utility::EgoSensorCanon(sensor_).velocity_as_kmph(); });
5754
}
5855

5956
cloe::Duration process(const cloe::Sync& sync) override {
@@ -63,7 +60,7 @@ class Speedometer : public cloe::Component {
6360
}
6461

6562
fable::Json active_state() const override {
66-
return fable::Json{{"kmph", utility::EgoSensorCanon(sensor_).velocity_as_kmph()}};
63+
return fable::Json{{"kmph", cloe::utility::EgoSensorCanon(sensor_).velocity_as_kmph()}};
6764
}
6865

6966
private:
@@ -75,7 +72,7 @@ class Speedometer : public cloe::Component {
7572
DEFINE_COMPONENT_FACTORY(SpeedometerFactory, SpeedometerConf, "speedometer",
7673
"provide an event trigger to evaluate speed in km/h")
7774

78-
DEFINE_COMPONENT_FACTORY_MAKE(SpeedometerFactory, Speedometer, EgoSensor)
75+
DEFINE_COMPONENT_FACTORY_MAKE(SpeedometerFactory, Speedometer, cloe::EgoSensor)
7976

8077
// Register factory as plugin entrypoint
8178
EXPORT_CLOE_PLUGIN(SpeedometerFactory)

runtime/include/cloe/data_broker.hpp

+35-2
Original file line numberDiff line numberDiff line change
@@ -1574,14 +1574,47 @@ class DataBroker {
15741574
* \return Pointer to the specified signal
15751575
*/
15761576
template <typename T>
1577-
SignalPtr declare(std::string_view new_name) {
1577+
SignalPtr declare(std::string_view name) {
15781578
assert_static_type<T>();
15791579
using compatible_type = databroker::compatible_base_t<T>;
15801580

15811581
declare<compatible_type>();
15821582

15831583
SignalPtr signal = Signal::make<compatible_type>();
1584-
alias(signal, new_name);
1584+
alias(signal, name);
1585+
return signal;
1586+
}
1587+
1588+
/**
1589+
* Declare a new signal and auto-implement getter and setter.
1590+
*
1591+
* \tparam T type of the signal value
1592+
* \param name name of the signal
1593+
* \param value_ptr pointer to signal value
1594+
* \return pointer to specified signal
1595+
*/
1596+
template <typename T>
1597+
SignalPtr declare(std::string_view name, T* value_ptr) {
1598+
assert(value_ptr != nullptr);
1599+
auto signal = this->declare<T>(name);
1600+
signal->template set_getter<T>([value_ptr]() -> const T& {
1601+
return *value_ptr;
1602+
});
1603+
signal->template set_setter<T>([value_ptr](const T& value) {
1604+
*value_ptr = value;
1605+
});
1606+
return signal;
1607+
}
1608+
1609+
template <typename T>
1610+
SignalPtr declare(std::string_view name, std::function<T()> getter, std::function<void(T)> setter) {
1611+
auto signal = this->declare<T>(name);
1612+
if (getter) {
1613+
signal->template set_getter<T>(getter);
1614+
}
1615+
if (setter) {
1616+
signal->template set_setter<T>(setter);
1617+
}
15851618
return signal;
15861619
}
15871620

runtime/include/cloe/registrar.hpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
#include <string> // for string
2727
#include <utility> // for move
2828

29+
#include <cloe/data_broker.hpp> // for DataBroker
30+
#include <cloe/handler.hpp> // for Handler
31+
#include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
32+
#include <fable/json.hpp> // for Json
2933
#include <sol/table.hpp>
30-
#include <cloe/cloe_fwd.hpp> // for DataBroker
31-
#include <cloe/handler.hpp> // for Handler
32-
#include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
33-
#include <fable/json.hpp> // for Json
3434

3535
namespace cloe {
3636

@@ -159,19 +159,37 @@ class Registrar {
159159
virtual std::unique_ptr<Registrar> with_static_prefix(const std::string& prefix) const = 0;
160160

161161
/**
162-
* Return a new Registrar with the given trigger prefix.
162+
* Return a new Registrar with the given trigger and data broker prefix.
163163
*
164164
* The returned object should remain valid even if the object creating it
165165
* is destroyed.
166166
*/
167167
virtual std::unique_ptr<Registrar> with_trigger_prefix(const std::string& prefix) const = 0;
168168

169+
[[nodiscard]] virtual std::string make_signal_name(std::string_view name) const = 0;
170+
169171
/**
170172
* Register an ActionFactory.
171173
*/
172174
virtual void register_action(std::unique_ptr<ActionFactory>&&) = 0;
173175

174-
virtual DataBroker& data_broker() const = 0;
176+
template <typename T>
177+
SignalPtr declare_signal(std::string_view name) {
178+
return data_broker().declare<T>(make_signal_name(name));
179+
}
180+
181+
template <typename T>
182+
SignalPtr declare_signal(std::string_view name, T&& value_ptr) {
183+
return data_broker().declare(make_signal_name(name), std::forward<T>(value_ptr));
184+
}
185+
186+
template <typename T, typename GetterFunc, typename SetterFunc>
187+
SignalPtr declare_signal(std::string_view name, GetterFunc&& getter, SetterFunc&& setter) {
188+
return data_broker().declare(make_signal_name(name), std::forward<GetterFunc>(getter),
189+
std::forward<SetterFunc>(setter));
190+
}
191+
192+
[[nodiscard]] virtual DataBroker& data_broker() const = 0;
175193

176194
/**
177195
* Construct and register an ActionFactory.

runtime/include/cloe/trigger/nil_event.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string> // for string
2626

2727
#include <cloe/core.hpp> // for Conf, Json
28+
#include <cloe/registrar.hpp> // for DirectCallback
2829
#include <cloe/trigger.hpp> // for Event, EventFactory, Action, ActionFactory
2930
#include <cloe/trigger/helper_macros.hpp> // for _X_FACTORY, _X_CALLBACK
3031

tests/test_lua04_schedule_test.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ cloe.schedule({
1818
end,
1919
})
2020

21-
local signals = { "vehicles.default.basic.acc" }
22-
cloe.require_signals(signals)
23-
cloe.record_signals(signals)
21+
local Sig = {
22+
VehAcc = "basic/acc"
23+
}
24+
cloe.require_signals_enum(Sig)
25+
cloe.record_signals(Sig)
2426
cloe.record_signals( {
2527
["acc_config.limit_acceleration"] = function()
26-
return cloe.signal("vehicles.default.basic.acc").limit_acceleration
28+
return cloe.signal(Sig.VehAcc).limit_acceleration
2729
end,
2830
["acc_config.limit_deceleration"] = function()
29-
return cloe.signal("vehicles.default.basic.acc").limit_deceleration
31+
return cloe.signal(Sig.VehAcc).limit_deceleration
3032
end,
3133
})
3234

0 commit comments

Comments
 (0)