woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

使用延后static 替换全局变量的使用方法

struct Gain_Map_String
{
public:
    static string GetStringByCmd(CMD_GAIN cmd)
    {
        const Gain_Map_String* p_static_self = __InitInstance();
        map<CMD_GAIN, string>::const_iterator iter_map;
        if(p_static_self->map_string.end() != (iter_map = p_static_self->map_string.find(cmd)))
            return iter_map->second;
        return string("");
    }
    static CMD_GAIN GetCmdByString(string& str)
    {
        const Gain_Map_String* p_static_self = __InitInstance();
        map<CMD_GAIN, string>::const_iterator iter_map;
        for (iter_map = p_static_self->map_string.begin(); iter_map != p_static_self->map_string.end(); ++iter_map)
        {
            if(str == iter_map->second)
                return iter_map->first;
        }
        return CMD_GAIN_END;
    }
    static CMD_GAIN GetCmdByString(const char* pSZ)
    {
        const Gain_Map_String* p_static_self = __InitInstance();
        map<CMD_GAIN, string>::const_iterator iter_map;
        string str(pSZ);
        for (iter_map = p_static_self->map_string.begin(); iter_map != p_static_self->map_string.end(); ++iter_map)
        {
            if(str == iter_map->second)
                return iter_map->first;
        }
        return CMD_GAIN_END;
    }
private:
    map<CMD_GAIN, string> map_string;
    static const Gain_Map_String* __InitInstance()
    {
        static Gain_Map_String stati_instance;
        return (&stati_instance);
    }
    Gain_Map_String()
    {
        map_string[CMD_GAIN_PlaceOCOOrder] = "CMD_GAIN_PlaceOCOOrder";
        map_string[CMD_GAIN_PlaceOCOOrderResult] = "CMD_GAIN_PlaceOCOOrderResult";
        map_string[CMD_GAIN_PlaceSingleOrder] = "CMD_GAIN_PlaceSingleOrder";
        map_string[CMD_GAIN_PlaceSingleOrderResult] = "CMD_GAIN_PlaceSingleOrderResult";
        map_string[CMD_GAIN_ModifySingleOrder] = "CMD_GAIN_ModifySingleOrder";
        map_string[CMD_GAIN_ModifySingleOrderResult] = "CMD_GAIN_ModifySingleOrderResult";
        map_string[CMD_GAIN_GetPositionBlotter] = "CMD_GAIN_GetPositionBlotter";
        map_string[CMD_GAIN_GetPositionBlotterResult] = "CMD_GAIN_GetPositionBlotterResult";
        map_string[CMD_GAIN_ModifySingleASSPOrder] = "CMD_GAIN_ModifySingleASSPOrder";
        map_string[CMD_GAIN_ModifySingleASSPOrderResult] = "CMD_GAIN_ModifySingleASSPOrderResult";
        map_string[CMD_GAIN_DealRequest] = "CMD_GAIN_DealRequest";
        map_string[CMD_GAIN_DealRequestResult] = "CMD_GAIN_DealRequestResult";
        map_string[CMD_GAIN_GetPositionBlotterDataSet] = "CMD_GAIN_GetPositionBlotterDataSet";
        map_string[CMD_GAIN_GetPositionBlotterDataSetResult] = "CMD_GAIN_GetPositionBlotterDataSetResult";
        map_string[CMD_GAIN_GetDealBlotterWithFilter] = "CMD_GAIN_GetDealBlotterWithFilter";
        map_string[CMD_GAIN_GetDealBlotterWithFilterResult] = "CMD_GAIN_GetDealBlotterWithFilterResult";
        map_string[CMD_GAIN_GetOrderBlotterDataSet] = "CMD_GAIN_GetOrderBlotterDataSet";
        map_string[CMD_GAIN_GetOrderBlotterDataSetResult] = "CMD_GAIN_GetOrderBlotterDataSetResult";
        map_string[CMD_GAIN_GetOrderBlotter] = "CMD_GAIN_GetOrderBlotter";
        map_string[CMD_GAIN_GetOrderBlotterResult] = "CMD_GAIN_GetOrderBlotterResult";
        map_string[CMD_GAIN_GetRatesDataSet] = "CMD_GAIN_GetRatesDataSet";
        map_string[CMD_GAIN_GetRatesDataSetResult] = "CMD_GAIN_GetRatesDataSetResult";
        map_string[CMD_GAIN_GetRatesServerAuth] = "CMD_GAIN_GetRatesServerAuth";
        map_string[CMD_GAIN_GetRatesServerAuthResult] = "CMD_GAIN_GetRatesServerAuthResult";
        map_string[CMD_GAIN_DealRequestByDealId] = "CMD_GAIN_DealRequestByDealId";
        map_string[CMD_GAIN_DealRequestByDealIdResult] = "CMD_GAIN_DealRequestByDealIdResult";
        map_string[CMD_GAIN_GetSymbolBlotter] = "CMD_GAIN_GetSymbolBlotter";
        map_string[CMD_GAIN_GetSymbolBlotterResult] = "CMD_GAIN_GetSymbolBlotterResult";
        map_string[CMD_GAIN_CancelOrder] = "CMD_GAIN_CancelOrder";
        map_string[CMD_GAIN_CancelOrderResult] = "CMD_GAIN_CancelOrderResult";
        map_string[CMD_GAIN_DealRequestAtBest] = "CMD_GAIN_DealRequestAtBest";
        map_string[CMD_GAIN_DealRequestAtBestResult] = "CMD_GAIN_DealRequestAtBestResult";
        map_string[CMD_GAIN_ModifyOCOASSPOrder] = "CMD_GAIN_ModifyOCOASSPOrder";
        map_string[CMD_GAIN_ModifyOCOASSPOrderResult] = "CMD_GAIN_ModifyOCOASSPOrderResult";
        map_string[CMD_GAIN_GetMarginBlotter] = "CMD_GAIN_GetMarginBlotter";
        map_string[CMD_GAIN_GetMarginBlotterResult] = "CMD_GAIN_GetMarginBlotterResult";
        map_string[CMD_GAIN_PlaceSingleASSPOrder] = "CMD_GAIN_PlaceSingleASSPOrder";
        map_string[CMD_GAIN_PlaceSingleASSPOrderResult] = "CMD_GAIN_PlaceSingleASSPOrderResult";
        map_string[CMD_GAIN_GetDealBlotter] = "CMD_GAIN_GetDealBlotter";
        map_string[CMD_GAIN_GetDealBlotterResult] = "CMD_GAIN_GetDealBlotterResult";
        map_string[CMD_GAIN_PlaceOCOASSPOrder] = "CMD_GAIN_PlaceOCOASSPOrder";
        map_string[CMD_GAIN_PlaceOCOASSPOrderResult] = "CMD_GAIN_PlaceOCOASSPOrderResult";
        map_string[CMD_GAIN_GetAccountObject] = "CMD_GAIN_GetAccountObject";
        map_string[CMD_GAIN_GetAccountObjectResult] = "CMD_GAIN_GetAccountObjectResult";
    }
};

===================================================================================================================

使用

string str = Gain_Map_String::GetStringByCmd(CMD_GAIN_PlaceSingleOrder);
UINT cmd = Gain_Map_String::GetCmdByString("CMD_GAIN_PlaceSingleOrder");

===================================================================================================================

优点

1、延时加载,使用时再加载,不使用不会存在;

2、减少全局对象,以及在代码中出现的全局对象的变量名,这种代码耦合也很烦人;

3、容易定位到所有使用了该类的代码中,最好是定义一个宏来得到指针,比如:GET_CLASS_A_PTR

===================================================================================================================

注意事项

1、只适合于生一个一个对象的类;

posted on 2008-06-07 22:00 肥仔 阅读(395) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理