| // (C) Copyright Gennadiy Rozental 2005-2008. |
| // Distributed under the Boost Software License, Version 1.0. |
| // (See accompanying file LICENSE_1_0.txt or copy at |
| // http://www.boost.org/LICENSE_1_0.txt) |
| |
| // See http://www.boost.org/libs/test for the library home page. |
| // |
| // File : $RCSfile$ |
| // |
| // Version : $Revision: 54633 $ |
| // |
| // Description : defines and implements inline model of program environment |
| // *************************************************************************** |
| |
| #ifndef NDNBOOST_RT_ENV_ENVIRONMENT_HPP_062604GER |
| #define NDNBOOST_RT_ENV_ENVIRONMENT_HPP_062604GER |
| |
| #ifdef UNDER_CE |
| #error Windows CE does not support environment variables. |
| #endif |
| |
| // Boost.Runtime.Parameter |
| #include <ndnboost/test/utils/runtime/config.hpp> |
| #include <ndnboost/test/utils/runtime/fwd.hpp> |
| #include <ndnboost/test/utils/runtime/argument.hpp> |
| #include <ndnboost/test/utils/runtime/interpret_argument_value.hpp> |
| |
| #include <ndnboost/test/utils/runtime/env/fwd.hpp> |
| #include <ndnboost/test/utils/runtime/env/modifier.hpp> |
| #include <ndnboost/test/utils/runtime/env/variable.hpp> |
| |
| // Boost.Test |
| #include <ndnboost/test/utils/callback.hpp> |
| |
| // Boost |
| #include <ndnboost/optional.hpp> |
| |
| namespace ndnboost { |
| |
| namespace NDNBOOST_RT_PARAM_NAMESPACE { |
| |
| // ************************************************************************** // |
| // ************** runtime::environment implementation ************** // |
| // ************************************************************************** // |
| |
| namespace environment { |
| |
| namespace rt_env_detail { |
| |
| template<typename T, typename Modifiers> |
| variable_data& |
| init_new_var( cstring var_name, Modifiers m = nfp::no_params ) |
| { |
| rt_env_detail::variable_data& new_vd = new_var_record( var_name ); |
| |
| cstring str_value = sys_read_var( new_vd.m_var_name ); |
| |
| if( !str_value.is_empty() ) { |
| try { |
| ndnboost::optional<T> value; |
| |
| if( m.has( interpreter ) ) |
| m[interpreter]( str_value, value ); |
| else |
| interpret_argument_value( str_value, value, 0 ); |
| |
| if( !!value ) { |
| new_vd.m_value.reset( new typed_argument<T>( new_vd ) ); |
| |
| arg_value<T>( *new_vd.m_value ) = *value; |
| } |
| } |
| catch( ... ) { // !! could we do that |
| // !! should we report an error? |
| } |
| } |
| |
| if( !new_vd.m_value && m.has( default_value ) ) { |
| new_vd.m_value.reset( new typed_argument<T>( new_vd ) ); |
| |
| nfp::optionally_assign( arg_value<T>( *new_vd.m_value ), m[default_value] ); |
| } |
| |
| nfp::optionally_assign( new_vd.m_global_id, m, global_id ); |
| |
| return new_vd; |
| } |
| |
| //____________________________________________________________________________// |
| |
| } // namespace rt_env_detail |
| |
| } // namespace environment |
| |
| // ************************************************************************** // |
| // ************** runtime::environment ************** // |
| // ************************************************************************** // |
| |
| namespace environment { |
| |
| // variable access |
| variable_base |
| var( cstring var_name ); |
| |
| //________________________________________________________________________// |
| |
| template<typename T> |
| inline variable<T> |
| var( cstring var_name ) |
| { |
| rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name ); |
| |
| return environment::variable<T>( !vd ? rt_env_detail::init_new_var<T>( var_name, nfp::no_params ) : *vd ); |
| } |
| |
| //________________________________________________________________________// |
| |
| template<typename T, typename Modifiers> |
| inline variable<T> |
| var( cstring var_name, Modifiers const& m ) |
| { |
| rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name ); |
| |
| return environment::variable<T>( !vd ? rt_env_detail::init_new_var<T>( var_name, m ) : *vd ); |
| } |
| |
| //________________________________________________________________________// |
| |
| // direct variable value access |
| inline cstring |
| get( cstring var_name ) |
| { |
| return environment::var<cstring>( var_name ).value(); |
| } |
| |
| //________________________________________________________________________// |
| |
| template<typename T> |
| inline T const& |
| get( cstring var_name ) |
| { |
| return environment::var<T>( var_name ).value(); |
| } |
| |
| //________________________________________________________________________// |
| |
| template<typename T> |
| inline void |
| get( cstring var_name, ndnboost::optional<T>& res ) |
| { |
| variable<T> const& v = environment::var<T>( var_name ); |
| v.value( res ); |
| } |
| |
| //________________________________________________________________________// |
| |
| } // namespace environment |
| |
| namespace env = environment; |
| |
| } // namespace NDNBOOST_RT_PARAM_NAMESPACE |
| |
| } // namespace ndnboost |
| |
| #ifndef NDNBOOST_RT_PARAM_OFFLINE |
| |
| #define NDNBOOST_RT_PARAM_INLINE inline |
| #include <ndnboost/test/utils/runtime/env/environment.ipp> |
| |
| #endif |
| |
| #endif // NDNBOOST_RT_ENV_ENVIRONMENT_HPP_062604GER |