c++ - C++ 中的多类型容器

标签 c++ generics configuration

我正在寻找一个 C++ 类来存储多类型参数以用作全局配置机制。

以下近似值存储了 param_name - param_value 对。其中参数值是多类型的。

有谁知道用于此目的的任何库或替代方案吗?

以下代码的任何替代方案?

#ifndef _GLOBAL_CONFIG_HPP_
#define _GLOBAL_CONFIG_HPP_

#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional/optional.hpp>

#include <map>
#include <string>
#include <ostream>
#include <typeinfo>

namespace GC
{

  class Global_Config
  {

  public:

    typedef std::string label_t;
    typedef std::map< label_t, boost::any > data_m_t;

  private:

    data_m_t data_m_;

  public:

    template< class T >
    void get( boost::optional< T >& data, const label_t& label )
      throw (  boost::bad_any_cast, std::bad_alloc )
    {
      data_m_t :: iterator it = data_m_.find( label );
      data = ( it == data_m_.end( ) ) ?
             boost::optional< T >( ) : boost::any_cast< T >( it->second );
    }

    template< class T >
    void set( const label_t& label, const T& data )
    {
      data_m_[ label ] = data;
    }

    friend std::ostream& operator<< ( std::ostream& os, const Global_Config& gc );
  };


  class ex_chk_type_ok : public std::exception { };

  // WARNING: throwing ex when type found for performance purposes!!!!
  template< class T >
  void ostream_over_global_config_chk_type( std::ostream& os,
                        const boost::any& value)
  {
    if( value.type( ) == typeid( T ) )
    {
     os << boost::any_cast< T > ( value );
     throw ex_chk_type_ok( ); 
    }
  }


  std::ostream& operator<<( std::ostream& os, const Global_Config& gc )
  {

    for( Global_Config::data_m_t:: const_iterator it( gc.data_m_.begin( ) );
     it != gc.data_m_.end( ); ++it )
     {
      os << it->first <<  ("=");
      try 
      {
        ostream_over_global_config_chk_type< bool >              ( os, it->second );
        ostream_over_global_config_chk_type< char >              ( os, it->second );
        ostream_over_global_config_chk_type< short >             ( os, it->second );
        ostream_over_global_config_chk_type< unsigned short>     ( os, it->second );
        ostream_over_global_config_chk_type< int >               ( os, it->second );
        ostream_over_global_config_chk_type< unsigned int>       ( os, it->second );
        ostream_over_global_config_chk_type< float >             ( os, it->second );
        ostream_over_global_config_chk_type< long>               ( os, it->second );
        ostream_over_global_config_chk_type< unsigned long >     ( os, it->second );
        ostream_over_global_config_chk_type< long long >         ( os, it->second );
        ostream_over_global_config_chk_type< unsigned long long >( os, it->second );
        ostream_over_global_config_chk_type< double >            ( os, it->second );
        ostream_over_global_config_chk_type< long double >       ( os, it->second );
        ostream_over_global_config_chk_type< wchar_t >           ( os, it->second );
        ostream_over_global_config_chk_type< std::string >       ( os, it->second );
      } catch(  ex_chk_type_ok &ex )
      {
        //Do nothing, ex to break checkings for performance purposes...
      }
      os << "(" << it->second.type( ).name( ) << ")";
      if( it != gc.data_m_.end( ) ) os << ";";

    }
    return os;  
  }

}; // ns GC
#endif

最佳答案

最动态的配置方式是在 Poco::DynamicAny 行中.

这就像任何一样,但在检索值时不需要强制转换。给定一个 DynamicAny 对象,您可以:

DynamicAny d{100.2};
//Note that there is no need for casting
double my_double = d;

配置的另一种选择是 Boost.Program_Options , 我认为这主要用于命令行,但也可用于读取 如果我没记错的话。 Boost.PropertyTree . 主要针对分层配置文件。

关于c++ - C++ 中的多类型容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703281/

相关文章:

generics - 在 Kotlin 中实例化泛型数组

configuration - Play 2 - 在生产模式下无法加载外部配置文件

configuration - Glassfish 3.1.2-default-config和server-config之间的区别

c++ - 将一系列 h264 帧写入多个文件

具有静态成员的 C++ 模板类 - 对所有类型的类都相同

Swift:将基本协议(protocol)类型转换为子协议(protocol)的泛型

java - 上界通配符(扩展)不起作用;数组列表<? extends SuperType> 不允许子类型的实例

linux - Setfacl - 不支持

c++ - cppcheck 空指针取消引用 : m_buffer - otherwise it is redundant to check it against null

c++ - 显式传递模板参数时,函数模板参数丢失常量?