不带switch语句的序列化. [英] Serialization without switch statements.

查看:47
本文介绍了不带switch语句的序列化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该问一个问题.

刚刚向我指出,在C ++语言中,完全不需要使用switch语句.完全没有理由再使用它.

作为恐龙,我仍然使用它.并发现它在具有多年经验的CAD/CAM/CNC编程中很有用.

让我们在CAD世界中举一个常见的例子. DXF文件.

在这种格式下,每个原始类型都由一个数字表示.

假设您遇到以下要实例化的数字.
100
201
300
305

现在,您需要从这些实例化不同的对象类型类型.可能是字符串,备注文本,字体,线,折线,圆等...我确定...不用担心获取与数据类型完全匹配的数字.数字不一定遵循逻辑模式.序列中的许多跳跃是显而易见的.

有人可以提供一个简洁的代码示例,说明如何从这些数字中实例化不同种类的对象而无需使用switch语句,函数映射或其他树''?

谢谢.

奖励:
在DSTV格式中,对象类型由字符串文字表示.类似的解决方案将很有趣.

My turn to ask a question.

It was just pointed out to me that the switch statement is completely unnecessary in the C++ language. There is no reason at all to use it anymore.

Being a dinosaur I still use it. And find it useful in CAD/CAM/CNC programming that have years of experience in.

Let''s take a common example in the CAD world. The DXF File.

In this format, every primitive type is denoted by a number.

Say, you encounter the following numbers to instantiate from.
100
201
300
305

Now you need to instantiate different object type types from these. It could be Strings, memo text, fonts, lines, polylines, circles, etc... You get the idea I''m sure... Don''t worry about getting the exact number matched with a data type. The numbers don''t necessarily follow a logical pattern. Many skips in the sequences are evident.

Could some provide a concise code example of how to instantiate different kinds of objects from these numbers without using switch statements, function mapping, or if else trees''?

Thank you.

Bonus:
In the DSTV format, object types are denoted by string literals. A similar solution for these would be interesting to see.

推荐答案

嗯...大开关"还有其他选择,但它们的方便性视具体情况而定. .

考虑不同的动作"(创建此"/创建该")作为不同的函数,并考虑一个表,该表具有作为键提及的数字和作为值的函数指针


well ... there are alternatives to "big switches" but the fact they are convenient depends case by case.

Think to different "actions" ("create this" / "create that") as different functions, and think to a table having the numbers you mention as key and function pointer as values

with an
std::map<unsigned, creatable* (createfn*)()> yourmap;

进行了适当的初始化,您可以将对象(支持从shape派生)创建为

initialized appropriately, you can just create your objects (suppsed to derive from shape) as

creatable* newobject= yourmap[id]();



同样,每次必须根据ID区分操作时,都可以使用类似的调度表或-存在公共接口的地方-虚拟函数.

在C ++ 11中,您甚至可以声明



Similarly, every time you have to differentiate an action based on an ID you can use similar dispatch tables or -where a common interface exist- virtual functions.

In C++11 you can even declare

std::map<unsigned, std::function<creatable*> >

当时具有使用任何 functor 类型(实现()运算符的类,甚至是lambda表达式的类型)的能力,返回creatable* ...

请注意,您可以使用std::string替换我以前使用的unsigned (指数字),以使用文本标识符

having the capability -at that point- to use whatever functor type (classes implementing the () operator, or even lambda expressions) returning a creatable*...

Note that you can replace what I used to be unsigned (referring to numbers) with std::string to use textual identifiers


这是无开关解决方案中的一个突破. />
创建某种协调器类,该类允许多个不同的对象工厂类在协调器内部注册自己.每个不同的对象工厂类都知道如何创建一种特定类型的对象,这些对象大概都是基于对象的某个通用抽象基类(例如,通用" CAD对象),并且知道如何接受或拒绝创建对象的邀请.基于定义原始代码编号的参数.

要创建对象,请向协调员提供基本元素的代码流(例如100、201、305等).对于每个工厂,协调员都会遍历其已注册工厂的列表,并依次询问每个工厂是否要成为创建对象的工厂.协调器将继续浏览其列表,直到创建对象为止,或者每个注册工厂都拒绝了创建对象的邀请.

请注意,该体系结构很容易升级,可以为新类型的基元添加新类型的工厂,而无需触摸协调器类的代码或任何其他现有工厂的代码.

迈克
Here''s a stab at switch-less solution.

Create some sort of coordinator class that allows multiple different object factory classes to register themselves inside of the coordinator. Each different object factory class knows how to create one particular kind of object, all presumably based on some common abstract base class of object (e.g., a "generic" CAD object), and knows how to accept or reject an invitation to create an object based on a parameter that defines the code number for a primitive.

To create an object, feed the coordinator a stream of code numbers for the primitives (e.g., 100, 201, 305, etc). For each one, the coordinator goes through its list of registered factories, and asks each in turn if it wants to be the one to create the object. The coordinator continues through its list until the object is created, or every registered factory has declined the invitation to create the object.

Note that the architecture is easily upgraded, to add new types of factories for new types of primitives, without a need to touch the code for the coordinator class or the code for any of the other existing factories.

Mike


这篇关于不带switch语句的序列化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆