Skip to main content

AVxcelerate Simulation Framework 2025 R2

topic

Last update: 16.07.2025
1
5
6#pragma once
7
8#include "core/communication/i_topic.h"
9#include <functional>
10#include <stdexcept>
11
17{
18namespace core
19{
20
21using TopicId = std::string;
22
36template <typename TopicMsgType>
37class Topic : public ITopic
38{
39 public:
40 using PublisherCallback = std::function<TopicMsgType()>;
41 using SubscriberMessageCallback = std::function<void(const TopicMsgType&)>;
42 using TopicMessageType = TopicMsgType;
43
44 Topic(const TopicId& topic_id, const TopicType topic_type = TopicType::kRTIDDS)
45 : ITopic{}, topic_id_(topic_id), topic_type_(topic_type)
46 {
47 }
48
49 TopicId GetId() const override { return topic_id_; }
50 TopicType GetType() const override { return topic_type_; }
51
52 void AddPublisher(std::unique_ptr<IPublisher> pub_ptr) override
53 {
54 if (publishers_.size() == 1)
55 {
56 throw std::range_error("Already a Publisher for topic " + topic_id_ +
57 " has been set. Multiple publishers for same topic is NOT allowed! ");
58 }
59 publishers_.push_back(std::move(pub_ptr));
60 }
61 void AddSubscriber(std::unique_ptr<ISubscriber> sub_ptr) override { subscribers_.push_back(std::move(sub_ptr)); }
62
63 std::size_t SubscriberCount() const override { return subscribers_.size(); }
64 std::size_t PublisherCount() const override { return publishers_.size(); }
65
66 void ClearPubSub() override
67 {
68 publishers_.clear();
69 subscribers_.clear();
70 }
71
72 const Subscribers& GetSubscribers() const override { return subscribers_; }
73 const Publishers& GetPublishers() const override { return publishers_; }
74
75 private:
76 TopicId topic_id_{};
77 Subscribers subscribers_{};
78 Publishers publishers_{};
79
80 const core::TopicType topic_type_{core::TopicType::kRTIDDS};
81};
82
83} // namespace core
84} // namespace simulation_framework
This is the concrete Topic class that can be used for creation of a communication channel with given ...
Definition topic.h:38
void AddSubscriber(std::unique_ptr< ISubscriber > sub_ptr) override
Definition topic.h:61
TopicId GetId() const override
Definition topic.h:49
TopicType GetType() const override
Definition topic.h:50
std::size_t SubscriberCount() const override
Definition topic.h:63
std::size_t PublisherCount() const override
Definition topic.h:64
void AddPublisher(std::unique_ptr< IPublisher > pub_ptr) override
Definition topic.h:52
const Subscribers & GetSubscribers() const override
Definition topic.h:72
const Publishers & GetPublishers() const override
Definition topic.h:73
void ClearPubSub() override
Definition topic.h:66
The namespace for all core functionalities under namespace simulation_framework.
The top namespace for simulation framework.

Connect with Ansys