variable.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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: 81913 $
  10. //
  11. // Description : defines model of program environment variable
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_ENV_VARIABLE_HPP_062604GER
  14. #define BOOST_RT_ENV_VARIABLE_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/parameter.hpp>
  22. #include <boost/test/utils/runtime/argument.hpp>
  23. #include <boost/test/utils/runtime/env/fwd.hpp>
  24. // Boost
  25. #include <boost/optional.hpp>
  26. namespace boost {
  27. namespace BOOST_RT_PARAM_NAMESPACE {
  28. namespace environment {
  29. // ************************************************************************** //
  30. // ************** runtime::environment::variable_data ************** //
  31. // ************************************************************************** //
  32. namespace rt_env_detail {
  33. struct variable_data : public runtime::parameter {
  34. cstring m_var_name;
  35. dstring m_global_id;
  36. argument_ptr m_value;
  37. };
  38. } // namespace rt_env_detail
  39. // ************************************************************************** //
  40. // ************** runtime::environment::variable_base ************** //
  41. // ************************************************************************** //
  42. class variable_base {
  43. public:
  44. explicit variable_base( rt_env_detail::variable_data& data ) : m_data( &data ) {}
  45. // arguments access
  46. template<typename T>
  47. T const& value() const
  48. {
  49. return arg_value<T>( *m_data->m_value );
  50. }
  51. template<typename T>
  52. void value( boost::optional<T>& res ) const
  53. {
  54. if( has_value() )
  55. res = arg_value<T>( *m_data->m_value );
  56. else
  57. res.reset();
  58. }
  59. bool has_value() const { return m_data->m_value!=0; }
  60. cstring name() const { return m_data->m_var_name; }
  61. protected:
  62. // Data members
  63. rt_env_detail::variable_data* m_data;
  64. } ;
  65. // ************************************************************************** //
  66. // ************** runtime::environment::variable ************** //
  67. // ************************************************************************** //
  68. template<typename T = cstring>
  69. class variable : public variable_base {
  70. public:
  71. // Constructors
  72. explicit variable( cstring var_name );
  73. template<typename Modifiers>
  74. explicit variable( cstring var_name, Modifiers const& m );
  75. explicit variable( rt_env_detail::variable_data& data )
  76. : variable_base( data ) {}
  77. // other variable assignment
  78. void operator=( variable const& v ) { m_data = v.m_data; }
  79. // access methods
  80. T const& value() const { return variable_base::value<T>(); }
  81. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) || \
  82. BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0593))
  83. template<typename T>
  84. void value( boost::optional<T>& res ) const { variable_base::value( res ); }
  85. #else
  86. using variable_base::value;
  87. #endif
  88. // Value assignment
  89. template<typename V>
  90. void operator=( V const& v )
  91. {
  92. if( !has_value() )
  93. m_data->m_value.reset( new typed_argument<T>( *m_data ) );
  94. arg_value<T>( *m_data->m_value ) = v;
  95. rt_env_detail::sys_write_var( m_data->m_var_name, format_stream().ref() << value() );
  96. }
  97. }; // class variable
  98. //____________________________________________________________________________//
  99. template<typename CharT, typename Tr,typename T>
  100. inline std::basic_ostream<CharT,Tr>&
  101. operator<<( std::basic_ostream<CharT,Tr>& os, variable<T> const& v )
  102. {
  103. os << v.name() << '=';
  104. if( v.has_value() )
  105. os << v.value();
  106. return os;
  107. }
  108. //____________________________________________________________________________//
  109. template<typename T, typename V>
  110. inline bool
  111. operator==( variable<T> ev, V const& v )
  112. {
  113. return ev.has_value() && ev.value() == v;
  114. }
  115. //____________________________________________________________________________//
  116. template<typename T, typename V>
  117. inline bool
  118. operator==( V const& v, variable<T> ev )
  119. {
  120. return ev.has_value() && ev.value() == v;
  121. }
  122. //____________________________________________________________________________//
  123. template<typename T, typename V>
  124. inline bool
  125. operator!=( variable<T> ev, V const& v )
  126. {
  127. return !ev.has_value() || ev.value() != v;
  128. }
  129. //____________________________________________________________________________//
  130. template<typename T, typename V>
  131. inline bool
  132. operator!=( V const& v, variable<T> ev )
  133. {
  134. return !ev.has_value() || ev.value() != v;
  135. }
  136. //____________________________________________________________________________//
  137. } // namespace environment
  138. } // namespace BOOST_RT_PARAM_NAMESPACE
  139. } // namespace boost
  140. // ************************************************************************** //
  141. // ************************************************************************** //
  142. // Implementation
  143. #include <boost/test/utils/runtime/env/environment.hpp>
  144. // ************************************************************************** //
  145. // ************** runtime::environment::variable ************** //
  146. // ************************************************************************** //
  147. namespace boost {
  148. namespace BOOST_RT_PARAM_NAMESPACE {
  149. namespace environment {
  150. template<typename T>
  151. variable<T>::variable( cstring var_name )
  152. : variable_base( environment::var<T>( var_name ) )
  153. {}
  154. //____________________________________________________________________________//
  155. template<typename T>
  156. template<typename Modifiers>
  157. variable<T>::variable( cstring var_name, Modifiers const& m )
  158. : variable_base( environment::var<T>( var_name, m ) )
  159. {}
  160. //____________________________________________________________________________//
  161. } // namespace environment
  162. } // namespace BOOST_RT_PARAM_NAMESPACE
  163. } // namespace boost
  164. #endif // BOOST_RT_ENV_VARIABLE_HPP_062604GER