Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame^] | 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: 49312 $ |
| 11 | // |
| 12 | // Description : default algorithms for string to specific type convertions |
| 13 | // *************************************************************************** |
| 14 | |
| 15 | #ifndef BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER |
| 16 | #define BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER |
| 17 | |
| 18 | // Boost.Runtime.Parameter |
| 19 | #include <ndnboost/test/utils/runtime/config.hpp> |
| 20 | #include <ndnboost/test/utils/runtime/trace.hpp> |
| 21 | |
| 22 | // Boost.Test |
| 23 | #include <ndnboost/test/utils/basic_cstring/io.hpp> |
| 24 | #include <ndnboost/test/utils/basic_cstring/compare.hpp> |
| 25 | |
| 26 | // Boost |
| 27 | #include <ndnboost/optional.hpp> |
| 28 | #include <ndnboost/lexical_cast.hpp> |
| 29 | |
| 30 | // STL |
| 31 | // !! could we eliminate these includes? |
| 32 | #include <list> |
| 33 | |
| 34 | namespace ndnboost { |
| 35 | |
| 36 | namespace BOOST_RT_PARAM_NAMESPACE { |
| 37 | |
| 38 | // ************************************************************************** // |
| 39 | // ************** runtime::interpret_argument_value ************** // |
| 40 | // ************************************************************************** // |
| 41 | // returns true if source is used false otherwise |
| 42 | |
| 43 | // generic case |
| 44 | template<typename T> |
| 45 | struct interpret_argument_value_impl { |
| 46 | static bool _( cstring source, ndnboost::optional<T>& res ) |
| 47 | { |
| 48 | BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<" << typeid(T).name() << ">" ); |
| 49 | |
| 50 | res = lexical_cast<T>( source ); |
| 51 | |
| 52 | BOOST_RT_PARAM_TRACE( "String " << source << " is interpreted as " << *res ); |
| 53 | return true; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | //____________________________________________________________________________// |
| 59 | |
| 60 | // dstring case |
| 61 | template<> |
| 62 | struct interpret_argument_value_impl<dstring> { |
| 63 | static bool _( cstring source, ndnboost::optional<dstring>& res ) |
| 64 | { |
| 65 | BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<dstring>" ); |
| 66 | |
| 67 | res = dstring(); |
| 68 | assign_op( *res, source, 0 ); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | //____________________________________________________________________________// |
| 75 | |
| 76 | // cstring case |
| 77 | template<> |
| 78 | struct interpret_argument_value_impl<cstring> { |
| 79 | static bool _( cstring source, ndnboost::optional<cstring>& res ) |
| 80 | { |
| 81 | BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<cstring>" ); |
| 82 | |
| 83 | res = source; |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | //____________________________________________________________________________// |
| 90 | |
| 91 | // specialization for type bool |
| 92 | template<> |
| 93 | struct interpret_argument_value_impl<bool> { |
| 94 | static bool _( cstring source, ndnboost::optional<bool>& res ) |
| 95 | { |
| 96 | BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<bool>" ); |
| 97 | |
| 98 | static literal_cstring YES( BOOST_RT_PARAM_CSTRING_LITERAL( "YES" ) ); |
| 99 | static literal_cstring Y( BOOST_RT_PARAM_CSTRING_LITERAL( "Y" ) ); |
| 100 | static literal_cstring NO( BOOST_RT_PARAM_CSTRING_LITERAL( "NO" ) ); |
| 101 | static literal_cstring N( BOOST_RT_PARAM_CSTRING_LITERAL( "N" ) ); |
| 102 | static literal_cstring one( BOOST_RT_PARAM_CSTRING_LITERAL( "1" ) ); |
| 103 | static literal_cstring zero( BOOST_RT_PARAM_CSTRING_LITERAL( "0" ) ); |
| 104 | |
| 105 | source.trim(); |
| 106 | |
| 107 | if( case_ins_eq( source, YES ) || case_ins_eq( source, Y ) || case_ins_eq( source, one ) ) { |
| 108 | res = true; |
| 109 | return true; |
| 110 | } |
| 111 | else if( case_ins_eq( source, NO ) || case_ins_eq( source, N ) || case_ins_eq( source, zero ) ) { |
| 112 | res = false; |
| 113 | return true; |
| 114 | } |
| 115 | else { |
| 116 | res = true; |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | //____________________________________________________________________________// |
| 123 | |
| 124 | template<typename T> |
| 125 | inline bool |
| 126 | interpret_argument_value( cstring source, ndnboost::optional<T>& res, long ) |
| 127 | { |
| 128 | return interpret_argument_value_impl<T>::_( source, res ); |
| 129 | } |
| 130 | |
| 131 | //____________________________________________________________________________// |
| 132 | |
| 133 | // specialization for list of values |
| 134 | template<typename T> |
| 135 | inline bool |
| 136 | interpret_argument_value( cstring source, ndnboost::optional<std::list<T> >& res, int ) |
| 137 | { |
| 138 | BOOST_RT_PARAM_TRACE( "In interpret_argument_value<std::list<T>>" ); |
| 139 | |
| 140 | res = std::list<T>(); |
| 141 | |
| 142 | while( !source.is_empty() ) { |
| 143 | // !! should we use token_iterator |
| 144 | cstring::iterator single_value_end = std::find( source.begin(), source.end(), BOOST_RT_PARAM_LITERAL( ',' ) ); |
| 145 | |
| 146 | ndnboost::optional<T> value; |
| 147 | interpret_argument_value( cstring( source.begin(), single_value_end ), value, 0 ); |
| 148 | |
| 149 | res->push_back( *value ); |
| 150 | |
| 151 | source.trim_left( single_value_end + 1 ); |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | //____________________________________________________________________________// |
| 158 | |
| 159 | } // namespace BOOST_RT_PARAM_NAMESPACE |
| 160 | |
| 161 | } // namespace ndnboost |
| 162 | |
| 163 | #endif // BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER |