我正在尝试在 OpenSplice 中构建一个 C++ 程序,该程序允许用户指定应加载的类型。通过使用 OpenSplice 的 IDL 预处理器 (IDLPP) 生成的 C++ 文件不共享一个公共文件,这会稍微复杂一些接口或基类——而不是选择生成所有/大部分代码(我假设减少不必要的依赖)。
我为这个问题创建的解决方案是首先使用 python 脚本生成一个头文件,然后可以由主程序调用。这将允许生成所有包含语句,并将 IDLPP 创建的类型添加为定义。与此有关的问题是稍后引用这些定义。我想通过将定义的对象存储在容器中然后简单地按数字引用它们来解决这个问题。
这里的问题是,由于它们不共享一个公共基类,C++ 没有提供一种简单的方法将它们存储在同一个容器中。作为参考,OpenSplice 对象通常是这样创建的(类似于 STL 中的元组或向量):
dds::topic::Topic<moduleName::classType> variableName(params);
我想将这些不同的对象类型存储在一个容器中。重复和“丑陋”的代码在这里不是什么大问题,因为无论如何我都会生成这些文件。这是我目前尝试的实现:
#include <tuple>
template<class T, class U>
class TopicHolder{
public:
std::tuple<dds::topic::Topic<T>, dds::topic::Topic<U>> tuple_;
//empty constructor (error: no matching function for call to 'std::tuple<dds::topic::Topic<chat::NameServiceType, dds::topic::detail::Topic>,
// dds::topic::Topic<chat::ChatMessageType, dds::topic::detail::Topic>>::tuple()')
TopicHolder(){}
//or leave out the default constructor altogether (error: use of deleted function + note: implicity deleted because the default definition would be ill formed)
//also previous error
//put the template in the constructor? (see main below for issues)
TopicHolder(T, U){
dds::domain::DomainParticipant dp(0);
dds::topic::Topic<T> top1(dp, "top1");
dds::topic::Topic<U> top2(dp, "top2");
tuple_ = std::make_tuple(top1, top2);
}
void storeTopics(std::tuple<dds::topic::Topic<T>, dds::topic::Topic<U>> topics){
tuple_ = topics;
}
};
int main(){
//call templated constructor?
TopicHolder<chat::NameServiceType, chat::ChatMessageType> topicHolder();
//all good, now try to pull tuple out...
auto outTopic = std::get<0>(topicHolder.tuple_);
//error: request for member 'tuple_' in 'topicHolder', which is of non-class type 'TopicHolder<chat::NameServiceType, chat::ChatMessageType>()'
//leave out brackets in TopicHolder constructor: error: no matching function for call to 'TopicHolder<chat::NameServiceType, chat::ChatMessageType>::Topic()'
return 0
}
正如您从评论中看到的那样,根据我尝试修改的部分,这会产生各种不同的错误。似乎最接近的方法是使用TopicHolder(T, U)
构造函数,但是当我尝试实际读取元组时失败了