> ## Documentation Index
> Fetch the complete documentation index at: https://developer.synopsys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# topic.h

```cpp theme={null}
#pragma once

#include "core/communication/i_topic.h"
#include <functional>
#include <stdexcept>

namespace simulation_framework
{
namespace core
{

using TopicId = std::string;

template <typename TopicMsgType>
class Topic : public ITopic
{
 public:
 using PublisherCallback = std::function<TopicMsgType()>;
 using SubscriberMessageCallback = std::function<void(const TopicMsgType&)>;
 using TopicMessageType = TopicMsgType;

 Topic(const TopicId& topic_id, const TopicType topic_type = TopicType::kRTIDDS)
        : ITopic{}, topic_id_(topic_id), topic_type_(topic_type)
    {
    }

    TopicId GetId() const override { return topic_id_; }
    TopicType GetType() const override { return topic_type_; }

 void AddPublisher(std::unique_ptr<IPublisher> pub_ptr) override
 {
 if (publishers_.size() == 1)
        {
 throw std::range_error("Already a Publisher for topic " + topic_id_ +
 " has been set. Multiple publishers for same topic is NOT allowed! ");
        }
        publishers_.push_back(std::move(pub_ptr));
    }
 void AddSubscriber(std::unique_ptr<ISubscriber> sub_ptr) override { subscribers_.push_back(std::move(sub_ptr)); }

    std::size_t SubscriberCount() const override { return subscribers_.size(); }
    std::size_t PublisherCount() const override { return publishers_.size(); }

 void ClearPubSub() override
 {
        publishers_.clear();
        subscribers_.clear();
    }

 const Subscribers& GetSubscribers() const override { return subscribers_; }
 const Publishers& GetPublishers() const override { return publishers_; }

 private:
    TopicId topic_id_{};
    Subscribers subscribers_{};
    Publishers publishers_{};

 const core::TopicType topic_type_{core::TopicType::kRTIDDS};
};

}  // namespace core
}  // namespace simulation_framework
```
