blob: 4dec761e5d9be5c3008caed835dcf700894cded2 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015#ifndef NDNBOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER
16#define NDNBOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070017
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
34namespace ndnboost {
35
Jeff Thompson3d613fd2013-10-15 15:39:04 -070036namespace NDNBOOST_RT_PARAM_NAMESPACE {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070037
38// ************************************************************************** //
39// ************** runtime::interpret_argument_value ************** //
40// ************************************************************************** //
41// returns true if source is used false otherwise
42
43// generic case
44template<typename T>
45struct interpret_argument_value_impl {
46 static bool _( cstring source, ndnboost::optional<T>& res )
47 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070048 NDNBOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<" << typeid(T).name() << ">" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070049
50 res = lexical_cast<T>( source );
51
Jeff Thompson3d613fd2013-10-15 15:39:04 -070052 NDNBOOST_RT_PARAM_TRACE( "String " << source << " is interpreted as " << *res );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070053 return true;
54 }
55};
56
57
58//____________________________________________________________________________//
59
60// dstring case
61template<>
62struct interpret_argument_value_impl<dstring> {
63 static bool _( cstring source, ndnboost::optional<dstring>& res )
64 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070065 NDNBOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<dstring>" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070066
67 res = dstring();
68 assign_op( *res, source, 0 );
69
70 return true;
71 }
72};
73
74//____________________________________________________________________________//
75
76// cstring case
77template<>
78struct interpret_argument_value_impl<cstring> {
79 static bool _( cstring source, ndnboost::optional<cstring>& res )
80 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070081 NDNBOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<cstring>" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070082
83 res = source;
84
85 return true;
86 }
87};
88
89//____________________________________________________________________________//
90
91// specialization for type bool
92template<>
93struct interpret_argument_value_impl<bool> {
94 static bool _( cstring source, ndnboost::optional<bool>& res )
95 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070096 NDNBOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<bool>" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070097
Jeff Thompson3d613fd2013-10-15 15:39:04 -070098 static literal_cstring YES( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "YES" ) );
99 static literal_cstring Y( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "Y" ) );
100 static literal_cstring NO( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "NO" ) );
101 static literal_cstring N( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "N" ) );
102 static literal_cstring one( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "1" ) );
103 static literal_cstring zero( NDNBOOST_RT_PARAM_CSTRING_LITERAL( "0" ) );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700104
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
124template<typename T>
125inline bool
126interpret_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
134template<typename T>
135inline bool
136interpret_argument_value( cstring source, ndnboost::optional<std::list<T> >& res, int )
137{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138 NDNBOOST_RT_PARAM_TRACE( "In interpret_argument_value<std::list<T>>" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700139
140 res = std::list<T>();
141
142 while( !source.is_empty() ) {
143 // !! should we use token_iterator
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700144 cstring::iterator single_value_end = std::find( source.begin(), source.end(), NDNBOOST_RT_PARAM_LITERAL( ',' ) );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700145
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700159} // namespace NDNBOOST_RT_PARAM_NAMESPACE
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700160
161} // namespace ndnboost
162
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700163#endif // NDNBOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER