如何避免基于数据类型的运行时切换/案例代码? [英] How to avoid runtime switch/case code based on data type?

查看:71
本文介绍了如何避免基于数据类型的运行时切换/案例代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小组,我有一个问题,我找不到一个好的解决方案。我是使用netCDF格式的科学数据文件的
。 netCDF数据的

属性之一是在运行时只知道实际的数据类型

。因此很多基于模板的技巧都不太有用。

考虑


数据文件

浮动x(3)3.5 ,2.5,8.9

double y(3)2.7,-2.3,1.2

int z(3)5,2,3


假设在我的代码中,我需要写一个求和函数并返回

结果,我可以做


模板< class T> < br $>
T sum(T * data,size_t n);


这一切都很好,但是在调用子程序中我被卡住了

因为在编译时我没有实际数据类型的先验知识

包含在数据文件中,因此我必须依赖基于开关/案例代码的

在运行时包含在数据文件中。考虑:


void sum_caller(){

mytype type = datafile.getline(i).type

switch(type) {

case float_type:

float ret = sum(datafile.getline(i).data);

do_something(ret);

case double_type:

float ret = sum(datafile.getline(i).data);

...

....

}


我不能声明''模板< class T> void sum_caller();''因为sum_caller中只有

,所以类型信息可用。 (你可以争辩说

我可以声明sum_caller模板,但这不是重点。

某些点,类型信息变得可用且适当

可采取行动)。是否有设计模式或实施

策略可以帮助我解决这个难题,从而避免交换机/案例基于
的代码?谢谢!

Hi Group, I''ve got a problem I couldn''t find a good solution. I am
working with scientific data files in netCDF format. One of the
properties of netCDF data is that the actual type of data is only known
at run time. Therefore a lot of template based trick isn''t too useful.
Considering

datafile
float x(3) 3.5, 2.5, 8.9
double y(3) 2.7, -2.3, 1.2
int z(3) 5, 2, 3

Suppose in my code, I need to write a summation function and return the
result, I could do

template <class T>
T sum(T * data, size_t n);

This is all good and dandy, but in the calling subroutine I am stuck
because at compile I have no priori knowledge of actual data type
contained in the data file, thus I must rely on switch/case code based
on what may be contained in the data file during run time. Consider:

void sum_caller(){
mytype type = datafile.getline(i).type
switch(type){
case float_type:
float ret = sum(datafile.getline(i).data);
do_something(ret);
case double_type:
float ret = sum(datafile.getline(i).data);
...
....
}

I cannot declare ''template <class T> void sum_caller();'' because only
within sum_caller, the type information is available. (you could argue
I can declare sum_caller template based, but that''s not the point. At
some point, the type information becomes available and appropriate
action can be taken). Is there a design pattern or implementation
strategy that can help me with this dilemma and thus avoid switch/case
based code altogether? Thanks!

推荐答案

刘飞写道:
嗨小组,我有问题我找不到一个好的解决方案。我正在使用netCDF格式的科学数据文件。 netCDF数据的一个属性是实际的数据类型只在运行时才知道。因此很多基于模板的技巧都不太有用。
考虑数据文件
浮动x(3)3.5,2.5,8.9
双y(3 )2.7,-2.3,1.2
int z(3)5,2,3

假设在我的代码中,我需要编写一个求和函数并返回
结果,我可以做

模板< class T>
T sum(T * data,size_t n);

这一切都很好,花花公子,但在调用子程序我被卡住了
因为在编译时我对数据文件中包含的实际数据类型没有先验知识,因此我必须依赖基于开关/案例代码的
来处理可能包含的内容在运行时的数据文件中。考虑:

void sum_caller(){
mytype type = datafile.getline(i).type
switch(type){
case float_type:
float ret = sum(datafile.getline(i).data);
do_something(ret);
case double_type:
float ret = sum(datafile.getline(i).data);
...
...


我无法声明''template< class T> void sum_caller();''因为只有sum_caller中的
,所以类型信息可用。 (你可以争辩说
我可以声明sum_caller模板,但这不是重点。在某些时候,类型信息变得可用并且可以采取适当的行动)。是否有一种设计模式或实施策略可以帮助我解决这个难题,从而完全避免基于开关/案例的代码?谢谢!
Hi Group, I''ve got a problem I couldn''t find a good solution. I am
working with scientific data files in netCDF format. One of the
properties of netCDF data is that the actual type of data is only known
at run time. Therefore a lot of template based trick isn''t too useful.
Considering

datafile
float x(3) 3.5, 2.5, 8.9
double y(3) 2.7, -2.3, 1.2
int z(3) 5, 2, 3

Suppose in my code, I need to write a summation function and return the
result, I could do

template <class T>
T sum(T * data, size_t n);

This is all good and dandy, but in the calling subroutine I am stuck
because at compile I have no priori knowledge of actual data type
contained in the data file, thus I must rely on switch/case code based
on what may be contained in the data file during run time. Consider:

void sum_caller(){
mytype type = datafile.getline(i).type
switch(type){
case float_type:
float ret = sum(datafile.getline(i).data);
do_something(ret);
case double_type:
float ret = sum(datafile.getline(i).data);
...
...
}

I cannot declare ''template <class T> void sum_caller();'' because only
within sum_caller, the type information is available. (you could argue
I can declare sum_caller template based, but that''s not the point. At
some point, the type information becomes available and appropriate
action can be taken). Is there a design pattern or implementation
strategy that can help me with this dilemma and thus avoid switch/case
based code altogether? Thanks!




查看Andrei Alexandrescu撰写的_Modern C ++ Design_的第8章。一个

Loki工厂的目的是为了填补这样的空白,并且很可能会为您提供所需的产品(好吧,只需付出一点努力)。您可以从


的最新源代码.href =http://sourceforge.net/projects/loki-lib/ target =_ blank> http://sourceforge.net/projects/loki-lib/



干杯! --M



Check out chapter 8 of _Modern C++ Design_ by Andrei Alexandrescu. A
Loki factory is intended to fill just such a gap and will likely do
what you need (well, with a little effort on your part). You can get
the latest source code for it from

http://sourceforge.net/projects/loki-lib/

Cheers! --M


2006-03-03,刘飞< fe ***** @ gmail.com>写道:
On 2006-03-03, Fei Liu <fe*****@gmail.com> wrote:
嗨组,我有一个问题,我找不到一个好的解决方案。我正在使用netCDF格式的科学数据文件。 netCDF数据的一个属性是实际的数据类型仅在运行时已知。因此,很多基于模板的技巧都不是很有用。考虑数据文件
浮点x(3)3.5,2.5,8.9
double y(3)2.7,-2.3,1.2
int z(3)5, 2,3,
Hi Group, I''ve got a problem I couldn''t find a good solution. I am
working with scientific data files in netCDF format. One of the
properties of netCDF data is that the actual type of data is only
known at run time. Therefore a lot of template based trick isn''t too
useful. Considering

datafile
float x(3) 3.5, 2.5, 8.9
double y(3) 2.7, -2.3, 1.2
int z(3) 5, 2, 3




看起来你需要一个解析器,而且它已经写了

。访问NCO的CVS存储库似乎是最好的开始。


-

Neil Cerutti

我从发动机罩下面冒出一股烟雾。

我意识到这辆车着火所以带走了我的狗并闷死它

用毯子。 --Insurance Claim Blooper



It looks like you need a parser, rather, and it''s already been
written. A visit to the CVS repository for NCO seems like the best
place to start.

--
Neil Cerutti
I pulled into a lay-by with smoke coming from under the bonnet.
I realized the car was on fire so took my dog and smothered it
with a blanket. --Insurance Claim Blooper




Neil Cerutti写道:

Neil Cerutti wrote:
2006-03-03,刘飞< ; FE ***** @ gmail.com>写道:
On 2006-03-03, Fei Liu <fe*****@gmail.com> wrote:
嗨组,我有一个问题,我找不到一个好的解决方案。我正在使用netCDF格式的科学数据文件。 netCDF数据的一个属性是实际的数据类型仅在运行时已知。因此,很多基于模板的技巧都不是很有用。考虑数据文件
浮点x(3)3.5,2.5,8.9
double y(3)2.7,-2.3,1.2
int z(3)5, 2,3,
Hi Group, I''ve got a problem I couldn''t find a good solution. I am
working with scientific data files in netCDF format. One of the
properties of netCDF data is that the actual type of data is only
known at run time. Therefore a lot of template based trick isn''t too
useful. Considering

datafile
float x(3) 3.5, 2.5, 8.9
double y(3) 2.7, -2.3, 1.2
int z(3) 5, 2, 3



看起来你需要一个解析器,而且它已经被编写了。参观NCO的CVS资料库似乎是最好的起点。

-
Neil Cerutti
我拉着烟雾来了从发动机罩下面。
我意识到汽车着火了,所以带着我的狗用毯子闷死它。 --Insurance Claim Blooper



It looks like you need a parser, rather, and it''s already been
written. A visit to the CVS repository for NCO seems like the best
place to start.

--
Neil Cerutti
I pulled into a lay-by with smoke coming from under the bonnet.
I realized the car was on fire so took my dog and smothered it
with a blanket. --Insurance Claim Blooper




解析器如何解决我的问题?



How does a parser solve my problem?


这篇关于如何避免基于数据类型的运行时切换/案例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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