0

グローバル構成メカニズムとして使用される複数型パラメーターを格納する C++ クラスを探しています。

次の概算では、param_name - param_value のペアが格納されています。param 値は multi-type です。

この目的のためのライブラリまたは代替手段を知っている人はいますか?

次のコードに代わるものはありますか?

#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
4

1 に答える 1

1

構成のための最も動的な方法は、Poco::DynamicAnyの行にあります。

これは他のものと同じですが、値を取得するときにキャストは必要ありません。オブジェクトを指定すると、次のDynamicAnyことができます。

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

構成の別の代替手段はBoost.Program_Optionsです。主にコマンドライン用ですが、間違って思い出さなければファイルからの読み取りにも使用できると思います。 Boost.PropertyTree。主に階層構成ファイル用です。

于 2013-10-31T10:51:47.967 に答える