gpt4 book ai didi

c++ - C++ 接口(interface)中的静态反序列化工厂

转载 作者:行者123 更新时间:2023-11-30 03:38:57 24 4
gpt4 key购买 nike

如下所示在接口(interface)类中使用静态反序列化方法是不好的做法吗?

class IBase
{
~IBase();
virtual void Foo()=0;

virtual const char* Serialize()=0;
static std::unique_ptr<IBase> Deserialize(const char* data); // <-- This
};

IBase 将跨越 DLL 边界,因此一个有效的序列化方法是让每个对象序列化自身,因此 IBase::Serialize 函数。

然后当我们有原始数据并且我们想要反序列化回一个对象时,调用 IBase::Deserialize 似乎是合乎逻辑的,因此 Serialize 和 Deserialize 函数紧密耦合。

boost::serialization 做了一些巧妙的自动操作,因此 Deserialize 工厂函数看起来像 data >> pIBase;,因此无需为新的派生类型手动更新工厂函数。

我认为另一种方法是创建一个 IBaseDeserializer 类来保存反序列化函数:

class IBase
{
~IBase();
virtual void Foo()=0;

virtual const char* Serialize()=0;
};

class IBaseDeserializer
{
static std::unique_ptr<IBase> Deserialize(const char* data);
};

大多数关于 SO 的类似问题都声称应该避免第一种方法,因为它违反了单一职责原则,基接口(interface)也是工厂。但是,我觉得这是一个特殊情况,因为它是基本接口(interface)的 Serialize 函数的对应函数,恰好是一个工厂。但是,派生对象具有反序列化函数或接口(interface)具有实现(尽管是静态的)没有多大意义。

写完这篇文章后,我更倾向于第二种方法,但我仍然看到第一种方法的值(value)。这里的最佳做法是什么?还有哪些其他优点和缺点,或者我是否完全偏离了基础(没有双关语意)并且应该做其他事情?

最佳答案

你会遇到更多的问题 passing those std::shared_ptr's across DLL boundaries除非您为所有 DLL 及其各自的客户端使用了完全相同的编译器和标准库,否则您真的很幸运。 (std::unique_ptr 也是如此。这与您已经用 std::string 确定的问题相同)。

您对静态解串器的使用没有好坏之分,它只是一个任意的设计选择。保持一致,这是重要的部分。

但你真的应该找到一种方法来返回 IBase * 并将任何共享指针内容隔离到客户端,或者考虑推出你自己的 COM 样式引用计数方法,或者直接使用COM (跨 DLL 边界处理共享对象是 COM 旨在解决的问题之一)。

而且不仅仅是返回类型。甚至例如std::shared_ptr(或 std::string)DLL header 中的成员变量将有问题,因为它们的大小/成员布局可能因编译器而异,您必须将类似的东西隐藏在所谓的 pimpl pattern 后面.与跨 DLL 边界抛出 std::exception 的处理相同。


用一个人为的例子来解决你的评论(我保持这个非常简单,我希望它具有代表性,即使它没有使用任何模板东西,模板化并不是真正的问题):

考虑一些从 DLL 导出的东西:

// In a header you have no control over, e.g. a standard library header:
struct Example {
int x;
int y;
};

// Exported from your DLL:
class IBase {
public:
static Example constructExample (int x, int y);
static void incrementX (Example *ex);
};

// And its implementation:
Example IBase::constructExample (int x, int y) {
Example ex;
ex.x = x;
ex.y = y;
return ex;
}

void IBase::incrementX (Example *ex) {
ex.x ++;
}

现在,您评论说“...不会跨越 DLL 边界,因为它是一个静态函数 - 即编译到调用它们的任何 DLL 中”。但它会的。例如,在使用该 DLL 的客户端中:

// In a SLIGHTLY DIFFERENT VERSION of that header you have no control over, 
// e.g. a standard library header:
struct Example {
int y; // note x and y are swapped from the version of this header
int x; // that the DLL was compiled with.
};

// In your client code somewhere:
Example ex = IBase::constructExample(1, 2);
assert(ex.x == 1); // <- will fail, our broken ex.x is actually 2.
assert(ex.y == 2); // <- will fail, our broken ex.y is actually 1.

// And also:
Example ex;
ex.x = 0;
ex.y = 0;
IBase::incrementX(&ex);
assert(ex.x == 1); // <- will fail, because our broken ex.y was incremented
assert(ex.y == 0); // <- will fail, because our broken ex.y was incremented

如您所见,它是 static 的事实在这里没有任何区别。

关于c++ - C++ 接口(interface)中的静态反序列化工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39156608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com