C ++运行时数据类型识别&避免开关情况 [英] C++ runtime datatype identification & avoiding switch-case

查看:82
本文介绍了C ++运行时数据类型识别&避免开关情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我希望简化和最小化重复。

I have the following code that I hope to simplify and minimize duplication.

// Gather.cpp
void GetParam()
{
    ProParameter * param; // 3rd party struct
    ProParamvalue proValue; // 3rd party struct
    ProParameterValueGet( param, & proValue ); // this is a call to a 3rd Party API
    shared_ptr< CPartParam > pParam; // CPartParam is my class

    /* The .type in switch() statement is an enum with following definition:
    typedef enum  param_value_types  {
       PRO_PARAM_DOUBLE  = 50, /* use d_val from ProParamvalueValue to set value *
       PRO_PARAM_STRING  = 51, /* use s_val from ProParamvalueValue to set value *
    }  ProParamvalueType;
    */
    // WUD LOVE TO ELIMINATE THE SWITCH-CASE
    switch( proValue.type ) // .type is enum as above
    {
        case PRO_PARAM_DOUBLE: // 3rd party enum value
            pParam->SetValue( proValue.value.d_val );
            break;
        case PRO_PARAM_STRING: // 3rd party enum value
            pParam->SetValue( proValue.value.s_val );
            break;
        default:
            break;
     }
}

// PartData.h
class CPartParam
{
public:
    enum ValueType
    {
    DOUBLE,
    STRING
    };

    ValueType m_eValueType;
    double m_dVal;
    wstring m_sVal;
    bool SetValue( const double & dVal );
    bool SetValue( const wstring & sVal );
};

// PartData.cpp
// There is one overload for each data-type. WUD LOVE TO CONDENSE TO A SINGLE METHOD/TEMPLATE FUNCTION THAT CAN SET THE VALUE IRRESPECTIVE OF THE DATA-TYPE.
void CPartParam::SetValue( const double & dVal )
{
    m_eValueType = DOUBLE;
    m_dVal = dVal;
}

bool CPartParam::SetValue( const wstring & sVal )
{
    m_eValueType = STRING;
    m_sVal = sVal;
}

可以看出,proValue.type的数据类型运行时,这迫使我写重复代码:CPartParam :: SetValue()重载(每个数据类型一个)。我会喜欢它,如果我可以避免在GetParam()中的开关情况循环,如果可能的话。

As can be seen the datatype of the "proValue.type" is determined at runtime, which forces me to write repetitive code: CPartParam::SetValue() overloads (one for each data-type). I would LOVE it if I can avoid the switch-case loop in GetParam(), if at all possible.

第二个开关情况下有更多的重复代码存储在CPartParam shared_ptr中的数据必须检索(即CPartParam :: GetValue)。

There is more repetitive code with a 2nd switch-case when the data stored in CPartParam shared_ptr has to retrieved (i.e. CPartParam::GetValue).

我已经显示了示例代码(可能不一定编译)

I have shown sample code (may not necessarily compile), but will fix if anyone wants to compile.

我只显示两种数据类型(PRO_PARAM_DOUBLE,PRO_PARAM_STRING),但还有一些。

I have shown only two data-types (PRO_PARAM_DOUBLE, PRO_PARAM_STRING) but there are a handful more.

我唯一的限制是,我们的开发团队仍然使用Boost 1.36.0,Visual Studio 2005(主要原因是由于第三方库)。我们需要使用boost :: serialization,所以建议使用任何新的数据类型有限制,新的数据类型必须使用boost :: serialization序列化boost 1.36.0。

The only restrictions I have are that our dev team still uses Boost 1.36.0, Visual Studio 2005 (primary reason is due to the 3rd party library). We need to use boost::serialization, so recommendations to use any new data-types has the restriction that that new data-type must be serializable using boost::serialization in boost version 1.36.0.

推荐答案

查看Boost.Variant(在Boost 1.36.0中可用)。这提供了封装了区别联合(代替了 CPartParam 类型)的变体类型, c $ c> static_visitor idiom代替切换。

Have a look at Boost.Variant (which is available in Boost 1.36.0). This provides a variant type that encapsulates the discriminated union (taking the place of your CPartParam type), and a static_visitor idiom that takes the place of the switch.

这篇关于C ++运行时数据类型识别&amp;避免开关情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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