summaryrefslogtreecommitdiff
path: root/boost/test/utils/runtime/interpret_argument_value.hpp
blob: 016caa017ff7fc601ee1b8cdf2b9ba25ee09882c (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
//  (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 : default algorithms for string to specific type convertions
// ***************************************************************************

#ifndef BOOST_TEST_UTILS_RUNTIME_INTERPRET_ARGUMENT_VALUE_HPP
#define BOOST_TEST_UTILS_RUNTIME_INTERPRET_ARGUMENT_VALUE_HPP

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

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

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

// STL
// !! could we eliminate these includes?
#include <list>

namespace boost {

namespace BOOST_TEST_UTILS_RUNTIME_PARAM_NAMESPACE {

// ************************************************************************** //
// **************       runtime::interpret_argument_value      ************** //
// ************************************************************************** //
// returns true if source is used false otherwise

// generic case
template<typename T>
struct interpret_argument_value_impl {
    static bool _( cstring source, boost::optional<T>& res )
    {
        BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "In interpret_argument_value_impl<" << typeid(T).name() << ">" );

        res = lexical_cast<T>( source );

        BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "String " << source << " is interpreted as " << *res );
        return true;
    }
};


//____________________________________________________________________________//

// dstring case
template<>
struct interpret_argument_value_impl<dstring> {
    static bool _( cstring source, boost::optional<dstring>& res )
    {
        BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "In interpret_argument_value_impl<dstring>" );

        res = dstring();
        assign_op( *res, source, 0 );

        return true;
    }
};

//____________________________________________________________________________//

// cstring case
template<>
struct interpret_argument_value_impl<cstring> {
    static bool _( cstring source, boost::optional<cstring>& res )
    {
        BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "In interpret_argument_value_impl<cstring>" );

        res = source;

        return true;
    }
};

//____________________________________________________________________________//

// specialization for type bool
template<>
struct interpret_argument_value_impl<bool> {
    static bool _( cstring source, boost::optional<bool>& res )
    {
        BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "In interpret_argument_value_impl<bool>" );

        static literal_cstring YES( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "YES" ) );
        static literal_cstring Y( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "Y" ) );
        static literal_cstring NO( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "NO" ) );
        static literal_cstring N( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "N" ) );
        static literal_cstring one( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "1" ) );
        static literal_cstring zero( BOOST_TEST_UTILS_RUNTIME_PARAM_CSTRING_LITERAL( "0" ) );

        source.trim();

        if( case_ins_eq( source, YES ) || case_ins_eq( source, Y ) || case_ins_eq( source, one ) ) {
            res = true;
            return true;
        }
        else if( case_ins_eq( source, NO ) || case_ins_eq( source, N ) || case_ins_eq( source, zero ) ) {
            res = false;
            return true;
        }
        else {
            res = true;
            return source.is_empty();
        }
    }
};

//____________________________________________________________________________//

template<typename T>
inline bool
interpret_argument_value( cstring source, boost::optional<T>& res, long )
{
    return interpret_argument_value_impl<T>::_( source, res );
}

//____________________________________________________________________________//

// specialization for list of values
template<typename T>
inline bool
interpret_argument_value( cstring source, boost::optional<std::list<T> >& res, int )
{
    BOOST_TEST_UTILS_RUNTIME_PARAM_TRACE( "In interpret_argument_value<std::list<T>>" );

    res = std::list<T>();

    while( !source.is_empty() ) {
        // !! should we use token_iterator
        cstring::iterator single_value_end = std::find( source.begin(), source.end(), BOOST_TEST_UTILS_RUNTIME_PARAM_LITERAL( ',' ) );

        boost::optional<T> value;
        interpret_argument_value( cstring( source.begin(), single_value_end ), value, 0 );

        res->push_back( *value );

        source.trim_left( single_value_end + 1 );
    }

    return true;
}

//____________________________________________________________________________//

} // namespace BOOST_TEST_UTILS_RUNTIME_PARAM_NAMESPACE

} // namespace boost

#endif // BOOST_TEST_UTILS_RUNTIME_INTERPRET_ARGUMENT_VALUE_HPP