我正在创建具有以下设计的信号和插槽系统:
有信号和插槽类
struct timeDoubleEvent
{
public:
timeDoubleEvent(const std::string& dataName)
:
dataName(dataName)
{}
const std::string& getDataName() const { return dataName; }
Event<hydraPtr<DataHandler::timeDouble>> update;
void fire(const hydraPtr<DataHandler::timeDouble>& timeDouble) const { update(timeDouble); }
private:
const std::string dataName;
};
class timeDoubleSlot
{
public:
timeDoubleSlot(const std::string& dataName)
:
dataName(dataName)
{}
const std::string& getDataName() const { return dataName; }
virtual void onEvent(const hydraPtr<DataHandler::timeDouble>& timeDouble) = 0;
private:
const std::string dataName;
};
在不同情况下,插槽最终会有所不同,因此我正在创建嵌套在某些内部的派生类:
class BaseClass
{
// forward declaration
class timePriceSlot;
public:
BaseClass(const std::string& name,
const std::vector<hydraPtr<EventHandler::timeDoubleEvent>>& dataEventVector,
const size_t& warmUpLength)
:
name(name),
dataSlotVector(initializeDataSlotVector(dataEventVector)),
warmUpLength(warmUpLength),
dataStorage(initializeDataStorage(dataEventVector)),
timeStorage(initializeTimeStorage(dataEventVector))
{}
private:
const std::vector<hydraPtr<timePriceSlot>> dataSlotVector;
const std::vector<hydraPtr<timePriceSlot>> initializeDataSlotVector(const std::vector<hydraPtr<EventHandler::timeDoubleEvent>>&);
const bool& checkAllDataReceived(const std::string& dataName);
class timePriceSlot : public EventHandler::timeDoubleSlot
{
public:
timePriceSlot(const std::string& dataName,
BaseClass& parent)
:
timeDoubleSlot(dataName),
parent(parent)
{}
void onEvent(const hydraPtr<DataHandler::timeDouble>& timeDouble);
BaseClass& getParent() const { return parent; }
private:
BaseClass& parent;
};
};
(仅显示相关位)特别是,信号槽连接是这样完成的:
const std::vector<hydraPtr<BaseClass::timePriceSlot>> BaseClass::initializeDataSlotVector(
const std::vector<hydraPtr<EventHandler::timeDoubleEvent>>& dataEventVector)
{
std::vector<hydraPtr<BaseClass::timePriceSlot>> output(dataEventVector.size());
for (size_t i = 0; i < dataEventVector.size(); ++i)
{
EventHandler::timeDoubleEvent thisTimeDoubleEvent = (*dataEventVector[i]);
std::shared_ptr<BaseClass::timePriceSlot> thisTimePriceSlot = std::make_shared<BaseClass::timePriceSlot>(dataEventVector[i]->getDataName(), *this);
output[i] = thisTimePriceSlot;
thisTimeDoubleEvent.update.connect([&](hydraPtr<DataHandler::timeDouble>& timeDouble) { (*thisTimePriceSlot).onEvent(timeDouble); });
}
return output;
}
我正在使用这样的客户端程序调用该函数:
const std::string stockName = "BBH";
EventHandler::timeDoubleEvent event1(stockName);
std::vector<hydraPtr<EventHandler::timeDoubleEvent>> eventVector(1);
eventVector[0] = std::make_shared<EventHandler::timeDoubleEvent>(stockName);
const hydraPtr<Signal::DerivedClass> myMA = std::make_shared<Signal::DerivedClass>(stockName + "_MA", eventVector, 10);
const std::vector<hydraPtr<DataHandler::PriceTimeSeriesDataStruct>> myData = getAggregatedData();
const std::vector<double> priceVectorCopy = myData[0]->getPriceVector();
std::vector<double>::const_iterator it_d;
const std::vector<std::string> timeVectorCopy = myData[0]->getDateTimeVector();
std::vector<std::string>::const_iterator it_s;
for (it_d = priceVectorCopy.begin(), it_s = timeVectorCopy.begin();
it_d != priceVectorCopy.end(); it_d++, it_s++)
{
const hydraPtr<DataHandler::timeDouble> timeDoubleTemp = std::make_shared<DataHandler::timeDouble>(stockName, (*it_s), (*it_d));
event1.fire(timeDoubleTemp);
}
(DerivedClass派生自BaseClass,此处具有一个不相关的函数的不同实现)但是,我发现即使没有构建或运行时错误,事件触发时也不会发生任何事情。实际上,永远不会访问onEvent函数。我做错什么了吗?预先感谢。
答案 0 :(得分:0)
问题出在这一行
thisTimeDoubleEvent.update.connect([&](hydraPtr<DataHandler::timeDouble>& timeDouble) { (*thisTimePriceSlot).onEvent(timeDouble); });
事件和插槽都应该是指针,而不是取消引用的值。