summaryrefslogtreecommitdiff
path: root/boost/test/utils/runtime/env/variable.hpp
blob: 8e8b0a0737a9299c548c9eaddca5d81a6ba754f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//  (C) Copyright Gennadiy Rozental 2005-2008.
//  Distributed under the Boost Software License, Version 1.0.
//  (See accompanying file LICENSE_1_0.txt or copy at 
//  http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.
//
//  File        : $RCSfile$
//
//  Version     : $Revision$
//
//  Description : defines model of program environment variable
// ***************************************************************************

#ifndef BOOST_RT_ENV_VARIABLE_HPP_062604GER
#define BOOST_RT_ENV_VARIABLE_HPP_062604GER

#ifdef UNDER_CE
#error Windows CE does not support environment variables.
#endif

// Boost.Runtime.Parameter
#include <boost/test/utils/runtime/config.hpp>
#include <boost/test/utils/runtime/fwd.hpp>
#include <boost/test/utils/runtime/parameter.hpp>
#include <boost/test/utils/runtime/argument.hpp>

#include <boost/test/utils/runtime/env/fwd.hpp>

// Boost
#include <boost/optional.hpp>

namespace boost {

namespace BOOST_RT_PARAM_NAMESPACE {

namespace environment {

// ************************************************************************** //
// **************      runtime::environment::variable_data     ************** //
// ************************************************************************** //

namespace rt_env_detail {

struct variable_data : public runtime::parameter {
    cstring         m_var_name;
    dstring         m_global_id;
    argument_ptr    m_value;
};

} // namespace rt_env_detail

// ************************************************************************** //
// **************     runtime::environment::variable_base      ************** //
// ************************************************************************** //

class variable_base {
public:
    explicit    variable_base( rt_env_detail::variable_data& data ) : m_data( &data ) {}

    // arguments access
    template<typename T>
    T const&    value() const
    {
        return arg_value<T>( *m_data->m_value );
    }

    template<typename T>
    void        value( boost::optional<T>& res ) const
    {
        if( has_value() )
            res = arg_value<T>( *m_data->m_value );
        else
            res.reset();
    }

    bool        has_value() const   { return m_data->m_value!=0; }
    cstring     name() const        { return m_data->m_var_name; }

protected:
    // Data members
    rt_env_detail::variable_data*  m_data;
} ;

// ************************************************************************** //
// **************        runtime::environment::variable        ************** //
// ************************************************************************** //

template<typename T = cstring>
class variable : public variable_base {
public:
    // Constructors
    explicit    variable( cstring var_name );

    template<typename Modifiers>
    explicit    variable( cstring var_name, Modifiers const& m );

    explicit    variable( rt_env_detail::variable_data& data ) 
    : variable_base( data )                                 {}

    // other variable assignment
    void        operator=( variable const& v )              { m_data = v.m_data; }

    // access methods
    T const&    value() const                               { return variable_base::value<T>(); }

#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) || \
    BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0593))
    template<typename T>
    void        value( boost::optional<T>& res ) const      { variable_base::value( res ); }
#else
    using       variable_base::value;
#endif

    // Value assignment
    template<typename V>
    void        operator=( V const& v )
    {
        if( !has_value() )
            m_data->m_value.reset( new typed_argument<T>( *m_data ) );

        arg_value<T>( *m_data->m_value ) = v;

        rt_env_detail::sys_write_var( m_data->m_var_name, format_stream().ref() << value() );
    }
}; // class variable

//____________________________________________________________________________//

template<typename CharT, typename Tr,typename T>
inline std::basic_ostream<CharT,Tr>&
operator<<( std::basic_ostream<CharT,Tr>& os, variable<T> const& v )
{
    os << v.name() << '=';

    if( v.has_value() )
        os << v.value();

    return os;
}

//____________________________________________________________________________//

template<typename T, typename V>
inline bool
operator==( variable<T> ev, V const& v )
{
    return ev.has_value() && ev.value() == v;
}

//____________________________________________________________________________//

template<typename T, typename V>
inline bool
operator==( V const& v, variable<T> ev )
{
    return ev.has_value() && ev.value() == v;
}

//____________________________________________________________________________//

template<typename T, typename V>
inline bool
operator!=( variable<T> ev, V const& v )
{
    return !ev.has_value() || ev.value() != v;
}

//____________________________________________________________________________//

template<typename T, typename V>
inline bool
operator!=( V const& v, variable<T> ev )
{
    return !ev.has_value() || ev.value() != v;
}

//____________________________________________________________________________//

} // namespace environment

} // namespace BOOST_RT_PARAM_NAMESPACE

} // namespace boost

// ************************************************************************** //
// ************************************************************************** //
// Implementation

#include <boost/test/utils/runtime/env/environment.hpp>

// ************************************************************************** //
// **************        runtime::environment::variable        ************** //
// ************************************************************************** //

namespace boost {

namespace BOOST_RT_PARAM_NAMESPACE {

namespace environment {

template<typename T>
variable<T>::variable( cstring var_name )
: variable_base( environment::var<T>( var_name ) )
{}

//____________________________________________________________________________//

template<typename T>
template<typename Modifiers>
variable<T>::variable( cstring var_name, Modifiers const& m )
: variable_base( environment::var<T>( var_name, m ) )
{}

//____________________________________________________________________________//

} // namespace environment

} // namespace BOOST_RT_PARAM_NAMESPACE

} // namespace boost

#endif // BOOST_RT_ENV_VARIABLE_HPP_062604GER