问题与需求:
请读者先看这篇文章,【C++模版之旅】项目中一次活用C++模板(traits)的经历。 对于此篇文章提出的问题,我给出一个新的思路。
talking is cheap,show me the code.最终版见文章结尾
代码:
class ExportData
{
union
{
string * sp;
long* lp;
double* dp;
void* vp;
};
enum my_type {SP,LP,DP} types;
static unordered_map<type_index,my_type> typeMap;
public:
template <typename T> ExportData(T t)
{
if(typeMap.find(typeid(t))==typeMap.end())
assert(false);
vp=new T(t);
types= typeMap[typeid(T)];
}
template <typename T> void setData(T t)
{
if(typeMap.find(typeid(t))==typeMap.end())
assert(false);
switch(types)
{
case SP:
delete sp;
break;
case DP:
delete dp;
break;
case LP:
delete lp;
break;
}
vp=new T(t);
types=typeMap[typeid(T)];
}
template <typename T> void getData(T& t)
{
if(typeMap[typeid(T)]!=types) assert(false);
t=*(static_cast<T*>(vp));
}
};
unordered_map<type_index,ExportData::my_type> ExportData::typeMap
{
{typeid(string),ExportData::my_type::SP},
{typeid(long),ExportData::my_type::LP},
{typeid(double),ExportData::my_type::DP},
};
重复一下,四点需求:
1. ExportData需要仅支持整型(long),浮点型(double),字符串(string)以及二进制(void*, size)4种类型的操作。(我并没有考虑二进制)
2. ExportData需要考虑结构的尺寸,尽量减少空间冗余(我使用联合体,保存各种类型数据的指针)
3. 即使对以上4种不同数据类型进行操作,还是希望在从ExportData中Get或Set真实数据时,使用的方法能统一(方法显然是统一的,因为使用的是模版)
4. 当调用者尝试使用了以上4种类型以外的数据类型时,能通过返回错误让调用方知道类型不匹配(为了方便演示,试图使用其他类型都会导致断言失败,终止运行)
如果你也讨厌代码中存在swtich,可以再次使用表驱动法。代码如下所示:
这段代码使用static_assert
如果你也讨厌代码中存在swtich,可以再次使用表驱动法。代码如下所示:
class DeleteLong
{
public:
void operator()(void *p)
{
delete static_cast<long*>(p);
}
};
class DeleteString
{
public:
void operator()(void *p)
{
delete static_cast<string*>(p);
}
};
class DeleteDouble
{
public:
void operator()(void *p)
{
delete static_cast<double*>(p);
}
};
class ExportData
{
union
{
string * sp;
long* lp;
double* dp;
void* vp;
};
enum my_type {SP,LP,DP} types;//change it to object.
static unordered_map<type_index,my_type> typeMap;
static vector<function<void(void*)>> deleters;
public:
template <typename T> ExportData(T t)
{
if(typeMap.find(typeid(t))==typeMap.end())
assert(false);
vp=new T(t);
types= typeMap[typeid(T)];
}
template <typename T> void setData(T t)
{
if(typeMap.find(typeid(t))==typeMap.end())
assert(false);
(deleters[types])(vp);
vp=new T(t);
types=typeMap[typeid(T)];
}
template <typename T> void getData(T& t)
{
if(typeMap[typeid(T)]!=types) assert(false);
t=*(static_cast<T*>(vp));
}
};
unordered_map<type_index,ExportData::my_type> ExportData::typeMap
{
{typeid(string),ExportData::my_type::SP},
{typeid(long),ExportData::my_type::LP},
{typeid(double),ExportData::my_type::DP},
};
vector<function<void(void*)>> ExportData::deleters {DeleteString(),DeleteLong(),DeleteDouble(),};
这段代码使用static_assert
class DeleteLong
{
public:
void operator()(void *p)
{
delete static_cast<long*>(p);
}
};
class DeleteString
{
public:
void operator()(void *p)
{
delete static_cast<string*>(p);
}
};
class DeleteDouble
{
public:
void operator()(void *p)
{
delete static_cast<double*>(p);
}
};
class ExportData
{
union
{
string * sp;
long* lp;
double* dp;
void* vp;
};
enum my_type {SP,LP,DP} types;//change it to object.
static unordered_map<type_index,my_type> typeMap;
static vector<function<void(void*)>> deleters;
public:
template <typename T> ExportData(T t)
{
static_assert(is_same<T,string>::value||is_same<T,double>::value||is_same<T,long>::value,"never support!");
vp=new T(t);
types= typeMap[typeid(T)];
}
template <typename T> void setData(T t)
{
if(typeMap.find(typeid(t))==typeMap.end())
assert(false);
(deleters[types])(vp);
vp=new T(t);
types=typeMap[typeid(T)];
}
template <typename T> void getData(T& t)
{
static_assert(is_same<T,string>::value||is_same<T,double>::value||is_same<T,long>::value,"never support!");
if(typeMap[typeid(T)]!=types) assert(false);//怎么在编译期搞定这里呢?
t=*(static_cast<T*>(vp));
}
这段代码额外的优点:
1.代码更加的短小紧凑。(代码量减少)2.ExportData对象使用起来更容易。3.ExportData对象仅有两个数据,一个是指针联合体,一个是枚举值。(性能更优)3.我在作者提出4点需求基础上添加了一个额外功能,ExportData可以动态的改变持有数据的类型。(功能更强)3. 类中所有方法如果不使用模版而是使用重载,虽然会导致代码量大增,但好处是我们可以在编译期提示用户ExportData不支持某些类型,也能提高一点运行速度。要不要这么做,可具体问题具体分析。4.因为使用模版,所以可扩展性强,当增加支持类型时,只需改动少量代码。(可扩展性更好)最后,这段代码是示例代码,也许经不起推敲,那么引用原文作者的话,“我想肯定还有更好的解决方法,比如可以尝试在编译时就提示类型不支持而不是在运行时通过返回错误来提示。如果有更好的解决方案,欢迎一起讨论。”,ME TOO。
struct A{~A(){cout<<"delete A..."<<endl;}};
template<typename T>
struct TypeTraits
{
typedef void TYPE;
};
template<>
struct TypeTraits<std::string>
{
typedef std::string TYPE;
};
template<>
struct TypeTraits<long>
{
typedef long TYPE;
};
template<>
struct TypeTraits<A>
{
typedef A TYPE;
};
template<>
struct TypeTraits<double>
{
typedef double TYPE;
};
class DeleteLong
{
public:
void operator()(void *p)
{
delete static_cast<long*>(p);
}
};
class DeleteString
{
public:
void operator()(void *p)
{
delete static_cast<string*>(p);
}
};
class DeleteDouble
{
public:
void operator()(void *p)
{
delete static_cast<double*>(p);
}
};
class DeleteA
{
public:
void operator()(void *p)
{
delete static_cast<A*>(p);
}
};
class ExportData
{
void* vp;
enum my_type {SP,LP,DP,AP} types;
static unordered_map<type_index,my_type> typeMap;
static vector<function<void(void*)>> deleters;
public:
template <typename T> ExportData(const T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
vp=new T(t);
types= typeMap[typeid(T)];
}
template <typename T> void setData(const T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
assert(types==typeMap[typeid(T)]);
*(static_cast<T*>(vp))=t;
}
template <typename T> void getData(T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
assert(types==typeMap[typeid(T)]);
t=*(static_cast<T*>(vp));
}
~ExportData()
{
(deleters[types])(vp);
}
};
unordered_map<type_index,ExportData::my_type> ExportData::typeMap
{
{typeid(string),ExportData::my_type::SP},
{typeid(long),ExportData::my_type::LP},
{typeid(double),ExportData::my_type::DP},
{typeid(A),ExportData::my_type::AP}
};
vector<function<void(void*)>> ExportData::deleters {DeleteString(),DeleteLong(),DeleteDouble(),DeleteA()};
1.删除ExportData对象持有的数据,最好使用标准delete删除数据,谨记使用内存擦除方法无法清除用户自定义类型(当数据持有指针时)
2.static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");静态断言,当用户使用不支持类型时,立即阻止用户编译。
3.assert(types==typeMap[typeid(T)]);运行时断言,当运行时发现类型异常,立即退出程序。
4.void*指针承担擦除类型的重任