我正在研究增强的序列化图书馆,支持序列化的侵扰性方式是界定具有签名(简化)的成员职能:
class ToBeSerialized {
public:
//Define this to support serialization
//Notice not virtual function!
template<class Archive>
void serialize(Archive & ar)
{.....}
};
此外,支持 derived强类基点清洗的一种方式是使用一种类型的宏观方法:
//No mention to the base class(es) from which Derived_class inherits
BOOST_CLASS_EXPORT_GUID(Derived_class, "derived_class")
在Derived_class是一些从基类继承的阶层,即基地。 由于这种宏观因素,有可能通过点站将“Derived”型级的序列序号定为“基地”级。
The question is: I am used in C++ to write abstract factories implemented through a map from std::string to (pointer to) functions which return objects of the desired type (and eveything is fine thanks to covariant types).
我看不出我如何利用上述非虚拟序列模版成员功能,在不了解其类型的情况下适当将物体(即构造)降为一种物体(但假设这种类型的信息是由序列器储存的,例如在座标)。
我想做的是(保持与上述相同的名声),例如:
XmlArchive xmlArchive; //A type or archive
xmlArchive.open("C:/ser.txt"); //Contains type information for the serialized class
Base_class* basePtr = Factory<Base_class>::create("derived_class",xmlArchive);
与在高射线一侧制造高压蒸气球(通过违约构造),这是我知道如何解决的那部分内容,并称XmlArchive的序列功能(我 st!) i.e. 做的是:
Base_class* Factory<Base_class>::create("derived_class",xmlArchive)
{
Base_class* basePtr = new Base_class; //OK, doable, usual map string to pointer to function
static_cast<Derived_class*>( basePtr )->serialize( xmlArchive ); //De-serialization, how?????
return basePtr;
}
I am sure this can be done (boost serialize does it but its code is impenetrable! :P), but I fail to figure out how. The key problem is that the serialize function is a template function. So I cannot have a pointer to a generic templated function. As the point in writing the templated serialize function is to make the code generic (i.e. not having to re-write the serialize function for different Archivers), it does not make sense then to have to register all the derived classes for all possible archive types, like:
MY_CLASS_REGISTER(Derived_class, XmlArchive);
MY_CLASS_REGISTER(Derived_class, TxtArchive);
...
事实上,在我的法典中,我依靠超负荷工作来纠正正确的行为:
void serialize( XmlArchive& archive, Derived_class& derived );
void serialize( TxtArchive& archive, Derived_class& derived );
...
牢记的关键点是,档案类型总是已知的,即: 我从未在档案类别中使用过长的多吗?
任何帮助我的建议?
非常感谢你们。
卡车