environment.ipp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: 57992 $
  10. //
  11. // Description : implements model of program environment
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER
  14. #define BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER
  15. // Boost.Runtime.Parameter
  16. #include <boost/test/utils/runtime/config.hpp>
  17. #include <boost/test/utils/runtime/validation.hpp>
  18. #include <boost/test/utils/runtime/env/variable.hpp>
  19. // Boost.Test
  20. #include <boost/test/utils/basic_cstring/compare.hpp>
  21. #include <boost/test/utils/basic_cstring/io.hpp>
  22. // STL
  23. #include <map>
  24. #include <list>
  25. namespace boost {
  26. namespace BOOST_RT_PARAM_NAMESPACE {
  27. namespace environment {
  28. // ************************************************************************** //
  29. // ************** runtime::environment ************** //
  30. // ************************************************************************** //
  31. namespace rt_env_detail {
  32. typedef std::map<cstring,rt_env_detail::variable_data> registry;
  33. typedef std::list<dstring> keys;
  34. BOOST_RT_PARAM_INLINE registry& s_registry() { static registry instance; return instance; }
  35. BOOST_RT_PARAM_INLINE keys& s_keys() { static keys instance; return instance; }
  36. BOOST_RT_PARAM_INLINE variable_data&
  37. new_var_record( cstring var_name )
  38. {
  39. // save the name in list of keys
  40. s_keys().push_back( dstring() );
  41. dstring& key = s_keys().back();
  42. assign_op( key, var_name, 0 );
  43. // create and return new record
  44. variable_data& new_var_data = s_registry()[key];
  45. new_var_data.m_var_name = key;
  46. return new_var_data;
  47. }
  48. //____________________________________________________________________________//
  49. BOOST_RT_PARAM_INLINE variable_data*
  50. find_var_record( cstring var_name )
  51. {
  52. registry::iterator it = s_registry().find( var_name );
  53. return it == s_registry().end() ? 0 : &(it->second);
  54. }
  55. //____________________________________________________________________________//
  56. #ifdef BOOST_MSVC
  57. #pragma warning(push)
  58. #pragma warning(disable:4996) // getenv
  59. #endif
  60. BOOST_RT_PARAM_INLINE cstring
  61. sys_read_var( cstring var_name )
  62. {
  63. using namespace std;
  64. return BOOST_RT_PARAM_GETENV( var_name.begin() );
  65. }
  66. #ifdef BOOST_MSVC
  67. #pragma warning(pop)
  68. #endif
  69. //____________________________________________________________________________//
  70. BOOST_RT_PARAM_INLINE void
  71. sys_write_var( cstring var_name, format_stream& var_value )
  72. {
  73. BOOST_RT_PARAM_PUTENV( var_name, cstring( var_value.str() ) );
  74. }
  75. //____________________________________________________________________________//
  76. } // namespace rt_env_detail
  77. BOOST_RT_PARAM_INLINE variable_base
  78. var( cstring var_name )
  79. {
  80. rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name );
  81. BOOST_RT_PARAM_VALIDATE_LOGIC( !!vd,
  82. BOOST_RT_PARAM_LITERAL( "First access to the environment variable " )
  83. << var_name << BOOST_RT_PARAM_LITERAL( " should be typed" ) );
  84. return variable_base( *vd );
  85. }
  86. //____________________________________________________________________________//
  87. } // namespace environment
  88. } // namespace BOOST_RT_PARAM_NAMESPACE
  89. } // namespace boost
  90. #endif // BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER