use std::fmt::Debug;
use std::sync::{Arc};
use crate::{
dds::{participant::*, typedesc::*, qos::*, traits::dds_entity::DDSEntity},
};
pub use crate::structure::topic_kind::TopicKind;
pub trait TopicDescription {
fn get_participant(&self) -> Option<DomainParticipant>;
fn get_type(&self) -> TypeDesc; fn get_name(&self) -> String;
}
#[derive(Clone)]
pub struct Topic {
inner: Arc<InnerTopic>,
}
impl Topic {
pub(crate) fn new(
my_domainparticipant: &DomainParticipantWeak,
my_name: String,
my_typedesc: TypeDesc,
my_qos_policies: &QosPolicies,
topic_kind: TopicKind,
) -> Topic {
Topic {
inner: Arc::new(InnerTopic::new(my_domainparticipant,my_name, my_typedesc, my_qos_policies, topic_kind) )
}
}
fn get_participant(&self) -> Option<DomainParticipant> {
self.inner.get_participant().clone()
}
fn get_type(&self) -> TypeDesc {
self.inner.get_type().clone()
}
fn get_name(&self) -> String {
self.inner.get_name().to_string()
}
pub fn kind(&self) -> TopicKind {
self.inner.kind()
}
}
impl PartialEq for Topic {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl Debug for Topic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl TopicDescription for Topic {
fn get_participant(&self) -> Option<DomainParticipant> {
self.get_participant()
}
fn get_type(&self) -> TypeDesc {
self.get_type()
}
fn get_name(&self) -> String {
self.get_name()
}
}
impl HasQoSPolicy for Topic {
fn get_qos(&self) -> QosPolicies {
self.inner.get_qos()
}
}
#[derive(Clone)]
pub struct InnerTopic {
my_domainparticipant: DomainParticipantWeak,
my_name: String,
my_typedesc: TypeDesc,
my_qos_policies: QosPolicies,
topic_kind: TopicKind, }
impl InnerTopic {
fn new(
my_domainparticipant: &DomainParticipantWeak,
my_name: String,
my_typedesc: TypeDesc,
my_qos_policies: &QosPolicies,
topic_kind: TopicKind,
) -> InnerTopic {
InnerTopic {
my_domainparticipant: my_domainparticipant.clone(),
my_name,
my_typedesc,
my_qos_policies: my_qos_policies.clone(),
topic_kind,
}
}
fn get_participant(&self) -> Option<DomainParticipant> {
self.my_domainparticipant.clone().upgrade()
}
fn get_type(&self) -> TypeDesc {
self.my_typedesc.clone()
}
fn get_name(&self) -> String {
self.my_name.to_string()
}
pub fn kind(&self) -> TopicKind {
self.topic_kind
}
}
impl PartialEq for InnerTopic {
fn eq(&self, other: &Self) -> bool {
self.get_participant() == other.get_participant()
&& self.get_type() == other.get_type()
&& self.get_name() == other.get_name()
&& self.get_qos() == other.get_qos()
&& self.topic_kind == other.topic_kind
}
}
impl Debug for InnerTopic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:?}", self.get_participant()))?;
f.write_fmt(format_args!("Topic name: {}", self.get_name()))?;
f.write_fmt(format_args!("Topic type: {:?}", self.get_type()))?;
f.write_fmt(format_args!("Topic QoS: {:?} ", self.get_qos()))
}
}
impl TopicDescription for InnerTopic {
fn get_participant(&self) -> Option<DomainParticipant> {
self.get_participant()
}
fn get_type(&self) -> TypeDesc {
self.get_type()
}
fn get_name(&self) -> String {
self.get_name()
}
}
impl HasQoSPolicy for InnerTopic {
fn get_qos(&self) -> QosPolicies {
self.my_qos_policies.clone()
}
}
impl DDSEntity for InnerTopic {}