summaryrefslogtreecommitdiff
path: root/boost/test/utils/runtime/env/environment.ipp
blob: 6baf72f430435846ee067c994885ce753ac30f2c (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
//  (C) Copyright Gennadiy Rozental 2005-2014.
//  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 : implements model of program environment
// ***************************************************************************

#ifndef BOOST_TEST_UTILS_RUNTIME_ENV_ENVIRONMENT_IPP
#define BOOST_TEST_UTILS_RUNTIME_ENV_ENVIRONMENT_IPP

// Boost.Runtime.Parameter
#include <boost/test/utils/runtime/config.hpp>
#include <boost/test/utils/runtime/validation.hpp>

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

// Boost.Test
#include <boost/test/utils/basic_cstring/compare.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>

// STL
#include <map>
#include <list>

namespace boost {

namespace BOOST_TEST_UTILS_RUNTIME_PARAM_NAMESPACE {

namespace environment {

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

namespace rt_env_detail {

typedef std::map<cstring,rt_env_detail::variable_data> registry;
typedef std::list<dstring> keys;

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE registry& s_registry()    { static registry instance; return instance; }
BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE keys&     s_keys()        { static keys instance; return instance; }

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE variable_data&
new_var_record( cstring var_name )
{
    // save the name in list of keys
    s_keys().push_back( dstring() );
    dstring& key = s_keys().back();
    assign_op( key, var_name, 0 );

    // create and return new record
    variable_data& new_var_data = s_registry()[key];

    new_var_data.m_var_name = key;

    return new_var_data;
}

//____________________________________________________________________________//

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE variable_data*
find_var_record( cstring var_name )
{
    registry::iterator it = s_registry().find( var_name );

    return it == s_registry().end() ? 0 : &(it->second);
}

//____________________________________________________________________________//

#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4996) // getenv
#endif

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE cstring
sys_read_var( cstring var_name )
{
    using namespace std;
    return BOOST_TEST_UTILS_RUNTIME_PARAM_GETENV( var_name.begin() );
}

#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
//____________________________________________________________________________//

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE void
sys_write_var( cstring var_name, format_stream& var_value )
{
    BOOST_TEST_UTILS_RUNTIME_PARAM_PUTENV( var_name, cstring( var_value.str() ) );
}

//____________________________________________________________________________//

} // namespace rt_env_detail

BOOST_TEST_UTILS_RUNTIME_PARAM_INLINE variable_base
var( cstring var_name )
{
    rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name );

    BOOST_TEST_UTILS_RUNTIME_PARAM_VALIDATE_LOGIC( !!vd,
                                   BOOST_TEST_UTILS_RUNTIME_PARAM_LITERAL( "First access to the environment variable " )
                                        << var_name << BOOST_TEST_UTILS_RUNTIME_PARAM_LITERAL( " should be typed" ) );

    return variable_base( *vd );
}

//____________________________________________________________________________//

} // namespace environment

} // namespace BOOST_TEST_UTILS_RUNTIME_PARAM_NAMESPACE

} // namespace boost

#endif // BOOST_TEST_UTILS_RUNTIME_ENV_ENVIRONMENT_IPP_062904GER