Ticket #7726: variable.hpp

File variable.hpp, 6.5 KB (added by p.brockamp@…, 10 years ago)

Test

Line 
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
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision: 54633 $
11//
12// Description : defines model of program environment variable
13// ***************************************************************************
14
15#ifndef BOOST_RT_ENV_VARIABLE_HPP_062604GER
16#define BOOST_RT_ENV_VARIABLE_HPP_062604GER
17
18#ifdef UNDER_CE
19#error Windows CE does not support environment variables.
20#endif
21
22// Boost.Runtime.Parameter
23#include <boost/test/utils/runtime/config.hpp>
24#include <boost/test/utils/runtime/fwd.hpp>
25#include <boost/test/utils/runtime/parameter.hpp>
26#include <boost/test/utils/runtime/argument.hpp>
27
28#include <boost/test/utils/runtime/env/fwd.hpp>
29
30// Boost
31#include <boost/optional.hpp>
32
33namespace boost {
34
35namespace BOOST_RT_PARAM_NAMESPACE {
36
37namespace environment {
38
39// ************************************************************************** //
40// ************** runtime::environment::variable_data ************** //
41// ************************************************************************** //
42
43namespace rt_env_detail {
44
45struct variable_data : public runtime::parameter {
46 cstring m_var_name;
47 dstring m_global_id;
48 argument_ptr m_value;
49};
50
51} // namespace rt_env_detail
52
53// ************************************************************************** //
54// ************** runtime::environment::variable_base ************** //
55// ************************************************************************** //
56
57class variable_base {
58public:
59 explicit variable_base( rt_env_detail::variable_data& data ) : m_data( &data ) {}
60
61 // arguments access
62 template<typename T>
63 T const& value() const
64 {
65 return arg_value<T>( *m_data->m_value );
66 }
67
68 template<typename T>
69 void value( boost::optional<T>& res ) const
70 {
71 if( has_value() )
72 res = arg_value<T>( *m_data->m_value );
73 else
74 res.reset();
75 }
76
77 bool has_value() const { return m_data->m_value; }
78 cstring name() const { return m_data->m_var_name; }
79
80protected:
81 // Data members
82 rt_env_detail::variable_data* m_data;
83} ;
84
85// ************************************************************************** //
86// ************** runtime::environment::variable ************** //
87// ************************************************************************** //
88
89template<typename T = cstring>
90class variable : public variable_base {
91public:
92 // Constructors
93 explicit variable( cstring var_name );
94
95 template<typename Modifiers>
96 explicit variable( cstring var_name, Modifiers const& m );
97
98 explicit variable( rt_env_detail::variable_data& data )
99 : variable_base( data ) {}
100
101 // other variable assignment
102 void operator=( variable const& v ) { m_data = v.m_data; }
103
104 // access methods
105 T const& value() const { return variable_base::value<T>(); }
106
107#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) || \
108 BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0593))
109 template<typename T>
110 void value( boost::optional<T>& res ) const { variable_base::value( res ); }
111#else
112 using variable_base::value;
113#endif
114
115 // Value assignment
116 template<typename V>
117 void operator=( V const& v )
118 {
119 if( !has_value() )
120 m_data->m_value.reset( new typed_argument<T>( *m_data ) );
121
122 arg_value<T>( *m_data->m_value ) = v;
123
124 rt_env_detail::sys_write_var( m_data->m_var_name, format_stream().ref() << value() );
125 }
126}; // class variable
127
128//____________________________________________________________________________//
129
130template<typename CharT, typename Tr,typename T>
131inline std::basic_ostream<CharT,Tr>&
132operator<<( std::basic_ostream<CharT,Tr>& os, variable<T> const& v )
133{
134 os << v.name() << '=';
135
136 if( v.has_value() )
137 os << v.value();
138
139 return os;
140}
141
142//____________________________________________________________________________//
143
144template<typename T, typename V>
145inline bool
146operator==( variable<T> ev, V const& v )
147{
148 return ev.has_value() && ev.value() == v;
149}
150
151//____________________________________________________________________________//
152
153template<typename T, typename V>
154inline bool
155operator==( V const& v, variable<T> ev )
156{
157 return ev.has_value() && ev.value() == v;
158}
159
160//____________________________________________________________________________//
161
162template<typename T, typename V>
163inline bool
164operator!=( variable<T> ev, V const& v )
165{
166 return !ev.has_value() || ev.value() != v;
167}
168
169//____________________________________________________________________________//
170
171template<typename T, typename V>
172inline bool
173operator!=( V const& v, variable<T> ev )
174{
175 return !ev.has_value() || ev.value() != v;
176}
177
178//____________________________________________________________________________//
179
180} // namespace environment
181
182} // namespace BOOST_RT_PARAM_NAMESPACE
183
184} // namespace boost
185
186// ************************************************************************** //
187// ************************************************************************** //
188// Implementation
189
190#include <boost/test/utils/runtime/env/environment.hpp>
191
192// ************************************************************************** //
193// ************** runtime::environment::variable ************** //
194// ************************************************************************** //
195
196namespace boost {
197
198namespace BOOST_RT_PARAM_NAMESPACE {
199
200namespace environment {
201
202template<typename T>
203variable<T>::variable( cstring var_name )
204: variable_base( environment::var( var_name ) )
205{}
206
207//____________________________________________________________________________//
208
209template<typename T>
210template<typename Modifiers>
211variable<T>::variable( cstring var_name, Modifiers const& m )
212: variable_base( environment::var( var_name, m ) )
213{}
214
215//____________________________________________________________________________//
216
217} // namespace environment
218
219} // namespace BOOST_RT_PARAM_NAMESPACE
220
221} // namespace boost
222
223#endif // BOOST_RT_ENV_VARIABLE_HPP_062604GER