- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如下所示在接口(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/
我目前正在对一个 mmorpg 的二进制网络协议(protocol)进行逆向工程。我正在用 java 实现该协议(protocol)。 对于每个数据包类型,我将创建一个表示二进制数据的类。 例如,聊天
我正在尝试围绕现有类编写半透明包装器,我希望它能够模仿其他类的序列化。 例如,给定以下类: class Foo { [JsonConverter(CustomConverter)] s
是否有使用 Jackson 序列化和反序列化枚举集的简单方法? private enum Type { YES, NO } @JacksonXmlProperty(localName = "t
我很想知道当我们反序列化一个对象时会发生什么。 例如,如果我的类对象由许多其他对象组成,对象创建过程如何在反序列化过程中发生 最佳答案 对象是用默认的初始化字段创建的,然后用从串行流中获取的属性值填充
我正在尝试序列化和反序列化(使用 QDataStream 但这与这里无关)一个 enum class变量: enum class Type : char { Trivial, Comp
我不确定这到底有什么问题...它不会为我编译,我将它从 c 翻译成 C++(或尝试)...是的,我是初学者。谢谢! #include #include using namespace std; i
我遇到的问题与此处描述的问题非常相似:Combining type and field serializers case class(id: Option[UUID], otherValue:Stri
我们知道base中的apply()可以对数组的边距应用一个函数,边距应该是行或列。我想将边距扩大到“对角线” 和“反对角线”。结构看起来像 diagApply <- function(x, FUN,
我找到了 JSON serialization and deserialization to objects in Flutter 的例子但是如何使用像这样的人员列表来做到这一点: [ {
我有一个相当大的terms聚合结果,这些结果被加载到下拉列表中以提供filter功能。 可以说,我的下拉列表中有4000多种动物。我的另一个下拉列表有4种动物颜色。 例, animal --> ["d
我需要将 C# (.NET Framework 4.5.2) 中的一个类与 XML 序列化(反序列化),该类具有 string 的字典属性。键和 string[]数组值。我正在使用 Serializa
[已解决]应用给定的解决方案,效果很好! 程序的目的:在用户打开和关闭程序时保存/重新加载以前的数据。 我曾经用一个对象(obj)成功(反)序列化,现在我有两个不同类的不同对象。 我试图通过查看其他帖
问题 假设我有一个代表某事或其他的枚举: public enum ResultState { Found, Deleted, NotFound } 在我的序列化 json 中,
是否有取消 JSON 字符串的功能?我猜它不会内置到 JQuery 中,但它可以通过编写一个操纵字符串的脚本来实现吗?我在下面遇到了这个问题。 我正在使用 NYTimes API,但它不支持 JSON
对于这个问题,假设当对象完全写入流并成功读出时,或者当对象部分写入流并且读回对象时发生异常时,序列化/反序列化是原子的。假设写操作可能无法成功完成,例如因为停电了。 在Serializable的描述中
有谁知道时序检查是否仍在检测虚拟环境?我尝试使用 rdtsc 指令来获取 cpu 周期并比较真实 linux 机器和在 virtualbox 上运行的 linux 之间的结果。但结果似乎不稳定。有时,
我正在对一个(外部给定的)XML 文件进行操作,该文件具有以下形式的元素 10 20 30 40 50 60 70 80 我知道如何将属性作为属性处理(通过使用 [XmlAttri
我有一个通用的序列化器和反序列化器,用于通过网络连接发送的消息: public static async Task SerializeObject(Object obj) {
我正在考虑将当前基于 WCF 的应用程序迁移到 protobuf-net.Grpc。这似乎是可行的,但是我无法在不包含所有具有 [ProtoInclude] 属性的派生类的情况下使(DTO 类)基类的
我正在尝试将一些数据保存到文件中,但文件保存到的目录不正确。 using (StreamWriter sw = new StreamWriter(dir + "\\temp" + x + ".txt"
我是一名优秀的程序员,十分优秀!