environment.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 54633 $
  10. //
  11. // Description : defines and implements inline model of program environment
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_ENV_ENVIRONMENT_HPP_062604GER
  14. #define BOOST_RT_ENV_ENVIRONMENT_HPP_062604GER
  15. #ifdef UNDER_CE
  16. #error Windows CE does not support environment variables.
  17. #endif
  18. // Boost.Runtime.Parameter
  19. #include <boost/test/utils/runtime/config.hpp>
  20. #include <boost/test/utils/runtime/fwd.hpp>
  21. #include <boost/test/utils/runtime/argument.hpp>
  22. #include <boost/test/utils/runtime/interpret_argument_value.hpp>
  23. #include <boost/test/utils/runtime/env/fwd.hpp>
  24. #include <boost/test/utils/runtime/env/modifier.hpp>
  25. #include <boost/test/utils/runtime/env/variable.hpp>
  26. // Boost.Test
  27. #include <boost/test/utils/callback.hpp>
  28. // Boost
  29. #include <boost/optional.hpp>
  30. namespace boost {
  31. namespace BOOST_RT_PARAM_NAMESPACE {
  32. // ************************************************************************** //
  33. // ************** runtime::environment implementation ************** //
  34. // ************************************************************************** //
  35. namespace environment {
  36. namespace rt_env_detail {
  37. template<typename T, typename Modifiers>
  38. variable_data&
  39. init_new_var( cstring var_name, Modifiers m = nfp::no_params )
  40. {
  41. rt_env_detail::variable_data& new_vd = new_var_record( var_name );
  42. cstring str_value = sys_read_var( new_vd.m_var_name );
  43. if( !str_value.is_empty() ) {
  44. try {
  45. boost::optional<T> value;
  46. if( m.has( interpreter ) )
  47. m[interpreter]( str_value, value );
  48. else
  49. interpret_argument_value( str_value, value, 0 );
  50. if( !!value ) {
  51. new_vd.m_value.reset( new typed_argument<T>( new_vd ) );
  52. arg_value<T>( *new_vd.m_value ) = *value;
  53. }
  54. }
  55. catch( ... ) { // !! could we do that
  56. // !! should we report an error?
  57. }
  58. }
  59. if( !new_vd.m_value && m.has( default_value ) ) {
  60. new_vd.m_value.reset( new typed_argument<T>( new_vd ) );
  61. nfp::optionally_assign( arg_value<T>( *new_vd.m_value ), m[default_value] );
  62. }
  63. nfp::optionally_assign( new_vd.m_global_id, m, global_id );
  64. return new_vd;
  65. }
  66. //____________________________________________________________________________//
  67. } // namespace rt_env_detail
  68. } // namespace environment
  69. // ************************************************************************** //
  70. // ************** runtime::environment ************** //
  71. // ************************************************************************** //
  72. namespace environment {
  73. // variable access
  74. variable_base
  75. var( cstring var_name );
  76. //________________________________________________________________________//
  77. template<typename T>
  78. inline variable<T>
  79. var( cstring var_name )
  80. {
  81. rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name );
  82. return environment::variable<T>( !vd ? rt_env_detail::init_new_var<T>( var_name, nfp::no_params ) : *vd );
  83. }
  84. //________________________________________________________________________//
  85. template<typename T, typename Modifiers>
  86. inline variable<T>
  87. var( cstring var_name, Modifiers const& m )
  88. {
  89. rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name );
  90. return environment::variable<T>( !vd ? rt_env_detail::init_new_var<T>( var_name, m ) : *vd );
  91. }
  92. //________________________________________________________________________//
  93. // direct variable value access
  94. inline cstring
  95. get( cstring var_name )
  96. {
  97. return environment::var<cstring>( var_name ).value();
  98. }
  99. //________________________________________________________________________//
  100. template<typename T>
  101. inline T const&
  102. get( cstring var_name )
  103. {
  104. return environment::var<T>( var_name ).value();
  105. }
  106. //________________________________________________________________________//
  107. template<typename T>
  108. inline void
  109. get( cstring var_name, boost::optional<T>& res )
  110. {
  111. variable<T> const& v = environment::var<T>( var_name );
  112. v.value( res );
  113. }
  114. //________________________________________________________________________//
  115. } // namespace environment
  116. namespace env = environment;
  117. } // namespace BOOST_RT_PARAM_NAMESPACE
  118. } // namespace boost
  119. #ifndef BOOST_RT_PARAM_OFFLINE
  120. #define BOOST_RT_PARAM_INLINE inline
  121. #include <boost/test/utils/runtime/env/environment.ipp>
  122. #endif
  123. #endif // BOOST_RT_ENV_ENVIRONMENT_HPP_062604GER