一.数据容器的定义:
DataContainer.h:
#ifndef DATA_CONTAINER_H
#define DATA_CONTAINER_H
#include
#include
class DataContainer{
public:
void push(Poco::Any any);
Poco::Any get(int index);
size_t size();
private:
std::vector
};
inline void DataContainer::push(Poco::Any any){
_vector.push_back(any);
}
inline Poco::Any DataContainer::get(int index){
if(index < size()){
return _vector[index];
}else{
return 0;
}
}
inline size_t DataContainer::size(){
return _vector.size();
}
#endif
二.数据解析容器的定义:
DataAuxiliary.h:
#ifndef DATA_AUXILIARY_H
#define DATA_AUXILIARY_H
#include "DataContainer.h"
#include
using namespace Poco;
class DataAuxiliary{
public:
static bool getBool(DataContainer& container,int index);
static int getInt(DataContainer& container,int index);
static UInt64 getUInt64(DataContainer& container,int index);
static UInt32 getUInt32(DataContainer& container,int index);
static UInt16 getUInt16(DataContainer& container,int index);
static DataContainer getContainer(DataContainer& container,int index);
static std::string getString(DataContainer& container,int index);
private:
DataAuxiliary(){}
};
#endif
三.数据解析容器的实现:
DataAuxiliary.cpp:
#include "DataAuxiliary.h"
bool DataAuxiliary::getBool(DataContainer& container,int index){
return AnyCast
}
int DataAuxiliary::getInt(DataContainer& container,int index){
return static_cast
}
Poco::UInt32 DataAuxiliary::getUInt32(DataContainer& container,int index){
return static_cast
}
Poco::UInt16 DataAuxiliary::getUInt16(DataContainer& container,int index){
return static_cast
}
std::string DataAuxiliary::getString(DataContainer& container,int index){
return AnyCast
}
DataContainer DataAuxiliary::getContainer(DataContainer& container,int index){
return AnyCast
}
四.Main函数的实现:
Main.cpp:
#include "DataContainer.h"
#include "DataAuxiliary.h"
#include
class Data{
public:
Data(int arg1,double arg2,std::string arg3)
:_arg1(arg1),_arg2(arg2),_arg3(arg3){
}
void toString(){
std::cout<<"_arg1->"<<_arg1<
std::cout<<"_arg2->"<<_arg2<
std::cout<<"_arg3->"<<_arg3<
}
private:
int _arg1;
double _arg2;
std::string _arg3;
};
int main(int argc,char** argv){
DataContainer container;
container.push(Poco::Any(Poco::UInt32(32)));//压入基本数据类型
container.push(Poco::Any(std::string("helloworld")));
container.push(Poco::Any(false));
container.push(Poco::Any(Data(25,99,"linyanwen")));//压入构造数据类型
std::cout<
Poco::UInt32 dt = DataAuxiliary::getUInt32(container,0);//解析数据
std::string str = DataAuxiliary::getString(container,1);
bool flag = DataAuxiliary::getBool(container,2);
Data data = AnyCast(container.get(3));
std::cout<<"dt->"<
std::cout<<"str->"<
if(!flag){
std::cout<<"flag is false"<
}else{
std::cout<<"flag is true"<
}
data.toString();
return 0;
}
PS:初写文章,文笔生涩之处,各位请见谅,若有疑问或者交流的,可加本人YY号:301558660
转载请注明出处:山水间博客, http://blog.csdn.net/linyanwen99/article/details/8029530