English 中文(简体)
C++ 模版工厂建筑/航空化
原标题:C++ templated factory constructor/de-serialization
  • 时间:2010-05-24 22:21:07
  •  标签:
  • c++
  • boost

我正在研究增强的序列化图书馆,支持序列化的侵扰性方式是界定具有签名(简化)的成员职能:

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 );
...

牢记的关键点是,档案类型总是已知的,即: 我从未在档案类别中使用过长的多吗?

任何帮助我的建议?

非常感谢你们。

卡车

最佳回答

你们都需要在从衍生类型储存信息之前储存某种识别资料。 然后,在阅读时,你首先使用这一识别资料,指示你去一家工厂,然后可以正确解释下一组信息,并生成你衍生的类型。 这可能是推动的:在非常基本的水平实现航空化。 或许如此:


ar >> type;
Base_class* basePtr = Factory<Base_class>::create(type,xmlArchive);

之后,你们有一幅这样的物体地图:



struct reader_base { virtual void load(xmlArchive, base_ptr) = 0; } template < typename T > struct reader : reader_base { virtual void load(xmlArchive, base_ptr) { static_cast<T*>(base_ptr)->serialize(xmlArchive); } };

问题回答

如果总是知道贵国的档案类型,那么由谁来计算贵国的<条码>的编码的功能为何? 有人主张重新使用密码,是的,但如果您的“编码”“火药类别改变其定义或取而代之,你很可能需要重新确定您的一些序列化代码。

如果你坚持:

class ToBeSerialized : public Base_class {
public:
    void serialize(Archive & ar)
    {.....}
};

然后,你可以就您的<代码>星号功能发出警告,并将其约束在工厂。

您还需要对每一类的<代码>create功能进行单独约束,以便当其要求时,能够立即进行正确的类型。 类似:

template <typename T> Base_class* Factory::create(Archive& xmlArchive) {
    T* derivedPtr = new T;
    derivedPtr->serialize( xmlArchive );
    return derivedPtr;
}

然后,工厂将需要一种通用的<代码>create方法,该方法可通向正确的固定参数create<T>:

Base_class* Factory::create(const char* typeString, Archive& xmlArchive) {
    // Pseudocode.
    return m_map.find(typeString)->callCreate(xmlArchive);
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签