#include
#include
#include
using namespace std;
struct BaseDevice
{
BaseDevice(const std::string sType, const std::string sApplication) : strType(sType), strApplication(sApplication)
{}
virtual ~BaseDevice();
std::string strType;
std::string strApplication;
};
BaseDevice::~BaseDevice() {}
struct GetDeviceInfor : public BaseDevice
{
GetDeviceInfor(const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication)
{}
virtual ~GetDeviceInfor();
};
GetDeviceInfor::~GetDeviceInfor()
{}
template<class T1>
struct SetDeviceInfor : public BaseDevice
{
SetDeviceInfor(const std::string sType, const std::string sApplication, T1 iValues):BaseDevice(sType, sApplication), iValues(iValues)
{}
~SetDeviceInfor()
{}
T1 iValues;
};
int main(int argc, char** argv)
{
std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
std::make_shared<GetDeviceInfor>("TemputerData", "FirstScreen"),
std::make_shared<GetDeviceInfor>("RealValue", "MeasurmentMode"),
std::make_shared<SetDeviceInfor<std::string>>("MachineValue", "MaintainerMode", "45")
};
for (auto pDeviceInfor : vecDeviceInfor)
{
if (pDeviceInfor)
{
std::shared_ptr<GetDeviceInfor> pGetDeviceTemoInfor = std::dynamic_pointer_cast<GetDeviceInfor>(pDeviceInfor);
if (pGetDeviceTemoInfor != nullptr)
{
std::cout << pGetDeviceTemoInfor->strApplication << std::endl;
}
std::shared_ptr<SetDeviceInfor<std::string>> pSetDeviceTemoInfor = std::dynamic_pointer_cast<SetDeviceInfor<std::string>>(pDeviceInfor);
if (pSetDeviceTemoInfor != nullptr)
{
std::cout << pSetDeviceTemoInfor->strApplication << std::endl;
std::cout << pSetDeviceTemoInfor->iValues << std::endl;
}
}
else
{
std::cout << "empty container!" << std::endl;
}
}
}
派生类增加传入指定函数
#include
#include
#include
#include
using namespace std;
class StorageClass
{
public:
StorageClass();
~StorageClass();
void setData(int iData)
{
iOperateData = iData;
}
void setStringData(std::string sData)
{
std::cout << "setData:" << sData << std::endl;
strData = sData;
}
int getData()
{
return iOperateData;
}
std::string getStringData()
{
return strData;
}
private:
int iOperateData = 1;
std::string strData;
};
StorageClass::StorageClass()
{
}
StorageClass::~StorageClass()
{
}
struct BaseDevice
{
BaseDevice(const std::string sType, const std::string sApplication) : strType(sType), strApplication(sApplication)
{}
virtual ~BaseDevice();
std::string strType;
std::string strApplication;
};
BaseDevice::~BaseDevice() {}
struct GetDeviceInfor : public BaseDevice
{
GetDeviceInfor(std::function<int(std::unique_ptr<StorageClass>&)> cFunction, const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication), cFunction(cFunction)
{}
virtual ~GetDeviceInfor();
std::function<int(std::unique_ptr<StorageClass>&)> cFunction;
};
GetDeviceInfor::~GetDeviceInfor()
{}
template<class T1>
struct SetDeviceInfor : public BaseDevice
{
SetDeviceInfor(std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction, const std::string sType, const std::string sApplication, T1 iValues):
BaseDevice(sType, sApplication),
iValues(iValues),
cFunction(cFunction)
{}
~SetDeviceInfor()
{}
T1 iValues;
std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction;
};
int main(int argc, char** argv)
{
std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
std::make_shared<GetDeviceInfor>(&StorageClass::getData, "TemputerData", "FirstScreen"),
std::make_shared<GetDeviceInfor>(&StorageClass::getData, "RealValue", "MeasurmentMode"),
std::make_shared<SetDeviceInfor<std::string>>(&StorageClass::setStringData, "MachineValue", "MaintainerMode", "45")
};
for (auto pDeviceInfor : vecDeviceInfor)
{
if (pDeviceInfor)
{
std::shared_ptr<GetDeviceInfor> pGetDeviceTemoInfor = std::dynamic_pointer_cast<GetDeviceInfor>(pDeviceInfor);
if (pGetDeviceTemoInfor != nullptr)
{
std::cout << pGetDeviceTemoInfor->strApplication << std::endl;
std::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
int rValue = pGetDeviceTemoInfor->cFunction(puniDevice);
std::cout << "rValue:" << rValue << std::endl;
}
std::shared_ptr<SetDeviceInfor<std::string>> pSetDeviceTemoInfor = std::dynamic_pointer_cast<SetDeviceInfor<std::string>>(pDeviceInfor);
if (pSetDeviceTemoInfor != nullptr)
{
std::cout << pSetDeviceTemoInfor->strApplication << std::endl;
std::cout << pSetDeviceTemoInfor->iValues << std::endl;
std::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
pSetDeviceTemoInfor->cFunction(puniDevice, "123");
std::cout << "GetData:" << puniDevice->getStringData() << std::endl;
}
}
else
{
std::cout << "empty container!" << std::endl;
}
}
}