Skip to main content

AVxcelerate Simulation Framework 2024 R2 SP02

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 
12 namespace simulation_framework
13 {
14 namespace core
15 {
16 
17 using TopicId = std::string;
18 
32 template <typename TopicMsgType>
33 class Topic : public ITopic
34 {
35  public:
36  using PublisherCallback = std::function<TopicMsgType()>;
37  using SubscriberMessageCallback = std::function<void(const TopicMsgType&)>;
38  using TopicMessageType = TopicMsgType;
39 
40  Topic(const TopicId& topic_id, const TopicType topic_type = TopicType::kRTIDDS)
41  : ITopic{}, topic_id_(topic_id), topic_type_(topic_type)
42  {
43  }
44 
45  TopicId GetId() const override { return topic_id_; }
46  TopicType GetType() const override { return topic_type_; }
47 
48  void AddPublisher(std::unique_ptr<IPublisher> pub_ptr) override
49  {
50  if (publishers_.size() == 1)
51  {
52  throw std::range_error("Already a Publisher for topic " + topic_id_ +
53  " has been set. Multiple publishers for same topic is NOT allowed! ");
54  }
55  publishers_.push_back(std::move(pub_ptr));
56  }
57  void AddSubscriber(std::unique_ptr<ISubscriber> sub_ptr) override { subscribers_.push_back(std::move(sub_ptr)); }
58 
59  std::size_t SubscriberCount() const override { return subscribers_.size(); }
60  std::size_t PublisherCount() const override { return publishers_.size(); }
61 
62  void ClearPubSub() override
63  {
64  publishers_.clear();
65  subscribers_.clear();
66  }
67 
68  const Subscribers& GetSubscribers() const override { return subscribers_; }
69  const Publishers& GetPublishers() const override { return publishers_; }
70 
71  private:
72  TopicId topic_id_{};
73  Subscribers subscribers_{};
74  Publishers publishers_{};
75 
76  const core::TopicType topic_type_{core::TopicType::kRTIDDS};
77 };
78 
79 } // namespace core
80 } // namespace simulation_framework

Connect with Ansys