2016-03-15 57 views
0

我有大量不受我控制的枚举类型。模板化延迟初始化以内部模板化的typedef作为参数的单例模式

enum ColorType { 
    RED, 
    GREEN, 
    BLUE }; 

我期待让C++客户端绑定他们希望枚举类型的个体值双向任何的std :: string(但独特之处:例如(枚举几个数百1)办法)。我得到这个使用升压bimap的(V1.60)工作 - 忽略边界检查等在这里:

template <typename L, typename R> boost::bimap<L, R> 
     makeBimap(std::initializer_list<typename boost::bimap<L, R>::value_type> list) 
     { 
      return boost::bimap<L, R>(list.begin(), list.end()); 
     }; 

template<typename E> class EnumWithString 
{ 
private: 
    typedef typename boost::bimap<E, std::string > BMEnumType; 
    const BMEnumType _biMap; 

    EnumWithString(const EnumWithString<E>&) = delete; 
    EnumWithString& operator=(const EnumWithString<E>&) = delete; 
public: 
    EnumWithString(const BMEnumType& biMap) : _biMap(biMap) {}; 
    const std::string& getString(const E& e) const { 
     return this->_biMap.left.at(e); 
    } 
    const E& getEnum(const std::string& s) const { 
     return this->_biMap.right.at(s); 
    } 
}; 

使得例如客户端代码(随意想象可读性类型定义):

EnumWithString<ColorType> stringBoundEnum(makeBimap<ColorType, std::string>({ { ColorType::RED, "Rouge" }, 
{ ColorType::BLUE, "Bleu" },                     
{ ColorType::GREEN, "Vert" } })); 
    cout << stringBoundEnum.getString(ColorType::GREEN) << endl; 
    cout << stringBoundEnum.getEnum("Bleu") << endl; 

这给出了正确的输出。该粗糙的一点是,我需要这些创建映射(如stringBoundEnum)是单身,也就是我正在寻找类似的接口(再想象类型定义):

EnumWithStringSingleton<ColorType>::setInstance(makeBimap<ColorType, std::string>({ { ColorType::RED, "Rouge" }, 
                       { ColorType::BLUE, "Bleu" }, 
                       { ColorType::GREEN, "Vert" } })); // called only once by client 

cout << EnumWithStringSingleton<ColorType>::getInstance().getString(ColorType::GREEN) << endl; 

理想的情况下,我找的东西,用模板辛格尔顿工作采取单CTOR参数,如:

template <typename T, typename CTORArgument> class LazyInitSingleton 
{ 
public: 
    static T& getInstance(const CTORArgument& arg) 
    { 
     static T& theInstance(arg); 
     return theInstance; 
    }  
private: 
}; 

的问题是,我的情况下,CTORArgument是一个模板类中定义的模板化的typedef。我很想知道人们是如何解决这个问题的(SFINAE也许?)。

回答

1

简单的解决办法就是让你的单身为EnumWithString需要更具体:

template <typename EnumWithStringType> class LazyInitSingleton 
{ 
public: 
    // You have to make BMEnumType public 
    static EnumWithStringType& getInstance(const typename EnumWithStringType::BMEnumType& arg) 
    { 
     static EnumWithStringType theInstance(arg); 
     return theInstance; 
    } 
}; 

LazyInitSingleton<EnumWithStringType<ColorType>>::getInstance(...); 

甚至

template <typename EnumType> class LazyInitSingleton 
{ 
public: 
    static EnumWithString<EnumType>& getInstance(const EnumWithString<EnumType>::BMEnumType& arg) 
    { 
     static EnumWithString<EnumType> theInstance(arg); 
     return theInstance; 
    } 
}; 

LazyInitSingleton<ColorType>::getInstance(...); 
+0

是的,这个伟大的工程。我认为我的想法太过于保持Singleton类的通用性,从而使结构过于复杂。为了让它编译,我添加了typename,以便它识别'BMEnumType',但是正确的想法在那里:typedef typename boost :: bimap BMEnumType;'和static EnumWithString &getInstance(const BMEnumType& arg)'签名。谢谢! –

+0

@HansRoggeman没问题,很高兴帮助。 –