interpret_argument_value.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 49312 $
  10. //
  11. // Description : default algorithms for string to specific type convertions
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER
  14. #define BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER
  15. // Boost.Runtime.Parameter
  16. #include <boost/test/utils/runtime/config.hpp>
  17. #include <boost/test/utils/runtime/trace.hpp>
  18. // Boost.Test
  19. #include <boost/test/utils/basic_cstring/io.hpp>
  20. #include <boost/test/utils/basic_cstring/compare.hpp>
  21. // Boost
  22. #include <boost/optional.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. // STL
  25. // !! could we eliminate these includes?
  26. #include <list>
  27. namespace boost {
  28. namespace BOOST_RT_PARAM_NAMESPACE {
  29. // ************************************************************************** //
  30. // ************** runtime::interpret_argument_value ************** //
  31. // ************************************************************************** //
  32. // returns true if source is used false otherwise
  33. // generic case
  34. template<typename T>
  35. struct interpret_argument_value_impl {
  36. static bool _( cstring source, boost::optional<T>& res )
  37. {
  38. BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<" << typeid(T).name() << ">" );
  39. res = lexical_cast<T>( source );
  40. BOOST_RT_PARAM_TRACE( "String " << source << " is interpreted as " << *res );
  41. return true;
  42. }
  43. };
  44. //____________________________________________________________________________//
  45. // dstring case
  46. template<>
  47. struct interpret_argument_value_impl<dstring> {
  48. static bool _( cstring source, boost::optional<dstring>& res )
  49. {
  50. BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<dstring>" );
  51. res = dstring();
  52. assign_op( *res, source, 0 );
  53. return true;
  54. }
  55. };
  56. //____________________________________________________________________________//
  57. // cstring case
  58. template<>
  59. struct interpret_argument_value_impl<cstring> {
  60. static bool _( cstring source, boost::optional<cstring>& res )
  61. {
  62. BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<cstring>" );
  63. res = source;
  64. return true;
  65. }
  66. };
  67. //____________________________________________________________________________//
  68. // specialization for type bool
  69. template<>
  70. struct interpret_argument_value_impl<bool> {
  71. static bool _( cstring source, boost::optional<bool>& res )
  72. {
  73. BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<bool>" );
  74. static literal_cstring YES( BOOST_RT_PARAM_CSTRING_LITERAL( "YES" ) );
  75. static literal_cstring Y( BOOST_RT_PARAM_CSTRING_LITERAL( "Y" ) );
  76. static literal_cstring NO( BOOST_RT_PARAM_CSTRING_LITERAL( "NO" ) );
  77. static literal_cstring N( BOOST_RT_PARAM_CSTRING_LITERAL( "N" ) );
  78. static literal_cstring one( BOOST_RT_PARAM_CSTRING_LITERAL( "1" ) );
  79. static literal_cstring zero( BOOST_RT_PARAM_CSTRING_LITERAL( "0" ) );
  80. source.trim();
  81. if( case_ins_eq( source, YES ) || case_ins_eq( source, Y ) || case_ins_eq( source, one ) ) {
  82. res = true;
  83. return true;
  84. }
  85. else if( case_ins_eq( source, NO ) || case_ins_eq( source, N ) || case_ins_eq( source, zero ) ) {
  86. res = false;
  87. return true;
  88. }
  89. else {
  90. res = true;
  91. return false;
  92. }
  93. }
  94. };
  95. //____________________________________________________________________________//
  96. template<typename T>
  97. inline bool
  98. interpret_argument_value( cstring source, boost::optional<T>& res, long )
  99. {
  100. return interpret_argument_value_impl<T>::_( source, res );
  101. }
  102. //____________________________________________________________________________//
  103. // specialization for list of values
  104. template<typename T>
  105. inline bool
  106. interpret_argument_value( cstring source, boost::optional<std::list<T> >& res, int )
  107. {
  108. BOOST_RT_PARAM_TRACE( "In interpret_argument_value<std::list<T>>" );
  109. res = std::list<T>();
  110. while( !source.is_empty() ) {
  111. // !! should we use token_iterator
  112. cstring::iterator single_value_end = std::find( source.begin(), source.end(), BOOST_RT_PARAM_LITERAL( ',' ) );
  113. boost::optional<T> value;
  114. interpret_argument_value( cstring( source.begin(), single_value_end ), value, 0 );
  115. res->push_back( *value );
  116. source.trim_left( single_value_end + 1 );
  117. }
  118. return true;
  119. }
  120. //____________________________________________________________________________//
  121. } // namespace BOOST_RT_PARAM_NAMESPACE
  122. } // namespace boost
  123. #endif // BOOST_RT_INTERPRET_ARGUMENT_VALUE_HPP_062604GER