source: NonGTP/Boost/boost/test/utils/runtime/env/variable.hpp @ 857

Revision 857, 6.6 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  (C) Copyright Gennadiy Rozental 2005.
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: variable.hpp,v $
9//
10//  Version     : $Revision: 1.2 $
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// 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
24#include <boost/test/utils/runtime/env/fwd.hpp>
25
26// Boost
27#include <boost/optional.hpp>
28
29namespace boost {
30
31namespace BOOST_RT_PARAM_NAMESPACE {
32
33namespace environment {
34
35// ************************************************************************** //
36// **************      runtime::environment::variable_data     ************** //
37// ************************************************************************** //
38
39namespace rt_env_detail {
40
41struct variable_data : public runtime::parameter {
42    cstring         m_var_name;
43    dstring         m_global_id;
44    argument_ptr    m_value;
45};
46
47} // namespace rt_env_detail
48
49// ************************************************************************** //
50// **************     runtime::environment::variable_base      ************** //
51// ************************************************************************** //
52
53class variable_base {
54public:
55    explicit    variable_base( rt_env_detail::variable_data& data ) : m_data( &data ) {}
56
57    // arguments access
58    template<typename T>
59    T const&    value() const
60    {
61        return arg_value<T>( *m_data->m_value );
62    }
63
64    template<typename T>
65    void        value( boost::optional<T>& res ) const
66    {
67        if( has_value() )
68            res = arg_value<T>( *m_data->m_value );
69        else
70            res.reset();
71    }
72
73    bool        has_value() const   { return m_data->m_value; }
74    cstring     name() const        { return m_data->m_var_name; }
75
76protected:
77    // Data members
78    rt_env_detail::variable_data*  m_data;
79} ;
80
81// ************************************************************************** //
82// **************        runtime::environment::variable        ************** //
83// ************************************************************************** //
84
85template<typename T = cstring>
86class variable : public variable_base {
87public:
88    // Constructors
89    explicit    variable( cstring var_name );
90
91    template<typename Modifiers>
92    explicit    variable( cstring var_name, Modifiers const& m );
93
94    explicit    variable( rt_env_detail::variable_data& data )
95    : variable_base( data )                                 {}
96
97    // other variable assignment
98    void        operator=( variable const& v )              { m_data = v.m_data; }
99
100    // access methods
101    T const&    value() const                               { return variable_base::value<T>(); }
102
103    using       variable_base::value;
104
105    // Value assignment
106    template<typename V>
107    void        operator=( V const& v )
108    {
109        if( !has_value() )
110            m_data->m_value.reset( new typed_argument<T>( *m_data ) );
111
112        arg_value<T>( *m_data->m_value ) = v;
113
114        rt_env_detail::sys_write_var( m_data->m_var_name, format_stream().ref() << value() );
115    }
116}; // class variable
117
118//____________________________________________________________________________//
119
120template<typename CharT, typename Tr,typename T>
121inline std::basic_ostream<CharT,Tr>&
122operator<<( std::basic_ostream<CharT,Tr>& os, variable<T> const& v )
123{
124    os << v.name() << '=';
125
126    if( v.has_value() )
127        os << v.value();
128
129    return os;
130}
131
132//____________________________________________________________________________//
133
134template<typename T, typename V>
135inline bool
136operator==( variable<T> ev, V const& v )
137{
138    return ev.has_value() && ev.value() == v;
139}
140
141//____________________________________________________________________________//
142
143template<typename T, typename V>
144inline bool
145operator==( V const& v, variable<T> ev )
146{
147    return ev.has_value() && ev.value() == v;
148}
149
150//____________________________________________________________________________//
151
152template<typename T, typename V>
153inline bool
154operator!=( variable<T> ev, V const& v )
155{
156    return !ev.has_value() || ev.value() != v;
157}
158
159//____________________________________________________________________________//
160
161template<typename T, typename V>
162inline bool
163operator!=( V const& v, variable<T> ev )
164{
165    return !ev.has_value() || ev.value() != v;
166}
167
168//____________________________________________________________________________//
169
170} // namespace environment
171
172} // namespace BOOST_RT_PARAM_NAMESPACE
173
174} // namespace boost
175
176// ************************************************************************** //
177// ************************************************************************** //
178// Implementation
179
180#include <boost/test/utils/runtime/env/environment.hpp>
181
182// ************************************************************************** //
183// **************        runtime::environment::variable        ************** //
184// ************************************************************************** //
185
186namespace boost {
187
188namespace BOOST_RT_PARAM_NAMESPACE {
189
190namespace environment {
191
192template<typename T>
193variable<T>::variable( cstring var_name )
194: variable_base( environment::var<T>( var_name ) )
195{}
196
197//____________________________________________________________________________//
198
199template<typename T>
200template<typename Modifiers>
201variable<T>::variable( cstring var_name, Modifiers const& m )
202: variable_base( environment::var<T>( var_name, m ) )
203{}
204
205//____________________________________________________________________________//
206
207} // namespace environment
208
209} // namespace BOOST_RT_PARAM_NAMESPACE
210
211} // namespace boost
212
213// ************************************************************************** //
214//   Revision History:
215//
216//   $Log: variable.hpp,v $
217//   Revision 1.2  2005/05/05 05:55:45  rogeeff
218//   portability fixes
219//
220//   Revision 1.1  2005/04/12 06:42:43  rogeeff
221//   Runtime.Param library initial commit
222//
223// ************************************************************************** //
224
225#endif // BOOST_RT_ENV_VARIABLE_HPP_062604GER
Note: See TracBrowser for help on using the repository browser.