如何做一个类型化参数封装类?
我需要在一个类中打包固定数量的任意类型的值。然后我需要能够根据每个参数的类型通过开关传递每个参数。参数的类型是基本的 C 类型和指向东西的指针(特定且有限数量的“东西”),所以没什么复杂的。
该“参数”类需要轻量(在空间和处理方面)。
这是我需要如何使用它的示例:
void MyFunc( const Parameters &Params )
{
// for loop
switch( Params(0).GetType() ) {
case MY_INT_TYPE: int ValInt = Params(0).Get<int>(); ...
case MY_PTR_TO_MY_STUFF1: MyStuff1 *ValS1 = Params(0).Get<MyStuff1*>(); ...
...
}
}
Parameters MyParams(2);
MyParams.Set<int>(0, 123);
MyParams.Set<MyStuff1*>(1, &SomeClassInstance);
MyFunc( MyParams );
...
MyParams.Set<float>(0, 123.456); // The same variable in the same scope
MyParams.Set<int*>(1, &Val);
MyFunc( MyParams );
当然,我可以手动专门化所有类型并将其存储在联合中,这就是强力方法。我一直在想有一种更简单的方法可以做到这一点,但无法弄清楚。我可以使用类型特征来存储类型信息,但我对值感到困惑。并且铸造价值不是一个选择。
有指针(比喻指针)吗?
I need to package a fixed number of values of arbitrary types in a class. Then I need to be able to pass each parameter through a switch according the their type. The types of the parameters are basic C types and pointers to stuff (a specific and limited number of "stuff"), so nothing complicated.
That "Parameter" class needs to be light (in space as well as in processing).
This is an example of how I need to use it:
void MyFunc( const Parameters &Params )
{
// for loop
switch( Params(0).GetType() ) {
case MY_INT_TYPE: int ValInt = Params(0).Get<int>(); ...
case MY_PTR_TO_MY_STUFF1: MyStuff1 *ValS1 = Params(0).Get<MyStuff1*>(); ...
...
}
}
Parameters MyParams(2);
MyParams.Set<int>(0, 123);
MyParams.Set<MyStuff1*>(1, &SomeClassInstance);
MyFunc( MyParams );
...
MyParams.Set<float>(0, 123.456); // The same variable in the same scope
MyParams.Set<int*>(1, &Val);
MyFunc( MyParams );
Of course I can specialize for all types manually and store in a union, that is the brute force approach. I keep thinking there is an easier way to do this, but can't figure it out. I can use type traits to store the type info, but I am stuck for the value. And casting the value is not an option.
Any pointer (figurative pointers that is)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于您最后的评论,我给您下一个答案:)
如果每个重载都能自行处理,那当然更好。
我真心希望我的回答能够满足您的需求。
Regarding last your comment I give you next answer :)
Sure better if each overload will process itself.
I am really hope that my answers will cover your needs.
无论如何,如果您不使用运行时多态性,就会立即出现一个切换。如果您将使用静态多态性,那么您需要专门化或切换。
使用 type2enum 和 enum2type 映射的好方法:
type2enum:
和向后 enum2type:
以及代码示例:
我们有 Type1、Type2、Type3 和 enum: ,
而不是将其用于将类型映射到 enum:
或将常量枚举映射到 type:
以及运行时枚举类型你使用类似:
Anyway there will be at once one switch if you will not use run-time polymorphism. If you will use static polymorphism than you need either specialization or switch.
Its good approach to use type2enum and enum2type mapping:
type2enum:
and backward enum2type:
and the sample of code:
we have Type1, Type2, Type3 and enum:
than use it for mapping type to enum:
or constant enum to type:
and for runtime enum to type you use something like:
允许摆脱 switch 的另一种方法是使用枚举和函子数组:
这允许您用简单的构造替换侵入式缝合:
恭喜!
如果您是疯子,那么将讨论下一种方法。
创建静态装饰器(mixin)来自动执行以下操作怎么样:
Another way that allows to get rid of switch is use enum and array of functors:
And that allows you substitute intrusive stitch with simple construct:
Congratulaions!
If you are maniac than will discuss next approach.
What about creating static decorator (mixin) for automaticially do following:
理想的实现将使用由有效参数类型集参数化的变体类型(例如boost::variant)。以下是如何使用变体类型来解决问题的示例:
如果您无法使用
boost
,这里是如何实现变体类型的示例。此实现旨在作为示例,仅支持两种类型 - 扩展以支持更多类型相对容易。您可能希望使用模板参数包和移动构造函数来实现最干净、最高效的实现。
An ideal implementation would use a variant type (e.g.
boost::variant
) parameterised by the valid set of parameter types. Here's an example of how to use a variant type to solve the problem:And here's an example of how to implement a variant type, if you can't use
boost
.This implementation is intended as an example and only supports two types - it would be relatively easy to extend to support more. You would want to use template-parameter-packs and move-constructors to achieve the cleanest and most efficient implementation.