RTC Toolkit 4.0.2
Loading...
Searching...
No Matches
main.hpp
Go to the documentation of this file.
1
12#ifndef RTCTK_TELSUB_MAIN_HPP
13#define RTCTK_TELSUB_MAIN_HPP
14
15#include <memory>
16#include <memory_resource>
17
23
24namespace rtctk::telSub {
25
45template <class UserTopicType, class DataBlender>
46void Main(const rtctk::componentFramework::Args& args, DataBlender&& blender) noexcept {
47 auto factory =
48 [blender = std::forward<DataBlender>(blender)](
49 std::string const& name,
50 componentFramework::ServiceContainer& services) -> std::unique_ptr<BusinessLogic> {
51 // Operational Logic factory.
52 // This takes ownership of `blender` and instances created from factory will only receive a
53 // reference. (TBD) (this requires a stateless DataBlender).
54 // If DataBlender is stateful with configuration params then this means that new instances
55 // should be created for each new instance of OperationalLogic?
56 auto op_logic_factory =
57 [&, blender = std::move(blender)](OperationalLogicFactoryParams const& params)
58 -> std::unique_ptr<OperationalLogicIf> {
59 // Allocate a block of memory used by Correlator and DdsWaitSet
60 // The resource and memory ownership is then transferred to OperationalLogic which is
61 // responsible for destroying them last.
62 // clang-format off
63 size_t size = (params.dds_params.m_topics.size() + 1u) * (
64 /* AgnosticDataSamples contains two vectors of dds sequences
65 owned by DdsWaitSet */
66 sizeof(DdsSampleSeq) +
67 sizeof(DdsInfoSeq) +
68 /* DataSamplesView contains vector of views owned by Correlator */
69 sizeof(DataSampleView) +
70 /* Vector of indices in Correlator */
71 sizeof(size_t));
72 // clang-format on
73 std::unique_ptr<std::byte[]> mem(new std::byte[size]);
74 // Both DdsWaitSet and Correlator only allocates once so we can use a simple
75 // monotonic_buffer_resource.
76 auto resource = std::make_unique<std::pmr::monotonic_buffer_resource>(mem.get(), size);
77 // Required by OperationalLogic
78 auto& metrics = services.Get<componentFramework::ComponentMetricsIf>();
79 auto& alerts = services.Get<componentFramework::AlertServiceIf>();
80
81 auto dds_subscriber = std::make_unique<DdsWaitSet>(params.dds_params, resource.get());
82 auto correlator = std::make_unique<Correlator>(
83 params.correlator_params, std::move(dds_subscriber), resource.get());
84 auto writer = ipcq::Writer<UserTopicType>(params.shm_params.topic_name.c_str(),
85 params.shm_params.capacity,
86 params.shm_params.mem_policy);
87 auto shm_publisher = MakeShmPublisher<UserTopicType>(std::move(writer), blender);
88 return std::make_unique<OperationalLogic>(params.operational_params,
89 std::move(correlator),
90 std::move(shm_publisher),
91 metrics,
92 alerts,
93 std::move(resource),
94 std::move(mem));
95 };
96
97 return std::make_unique<BusinessLogic>(name, services, std::move(op_logic_factory));
98 };
99 componentFramework::RunAsRtcComponent<BusinessLogic>(args, std::move(factory));
100}
101
102} // namespace rtctk::telSub
103
104#endif // RTCTK_TELSUB_MAIN_HPP
Alert Service interface.
Definition: alertServiceIf.hpp:315
Class used to parse default command line arguments.
Definition: rtcComponentArgs.hpp:34
Component metrics interface.
Definition: componentMetricsIf.hpp:184
Container class that holds services of any type.
Definition: serviceContainer.hpp:39
Declares Correlator.
Declares the DdsWaitSet implementation.
int Main(int argc, char *argv[])
Main function implementation.
Definition: rtcComponentMain.hpp:229
Definition: main.cpp:18
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition: agnosticDataSamples.hpp:30
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition: agnosticDataSamples.hpp:28
Implements the business logic for telSub.
Provides core functionality of an RTC Component.
Declares ShmPublisher.
Agnostic data sample non-owning reference type.
Definition: dataSampleView.hpp:26
Set of all parameters needed when constructing the OperationalLogic object.
Definition: businessLogic.hpp:47