argument_factory.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // (C) Copyright Gennadiy Rozental 2001.
  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$
  10. //
  11. // Description : argument factories for different kinds of parameters
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RUNTIME_ARGUMENT_FACTORY_HPP
  14. #define BOOST_TEST_UTILS_RUNTIME_ARGUMENT_FACTORY_HPP
  15. // Boost.Test Runtime parameters
  16. #include <boost/test/utils/runtime/errors.hpp>
  17. #include <boost/test/utils/runtime/argument.hpp>
  18. // Boost.Test
  19. #include <boost/test/utils/basic_cstring/io.hpp>
  20. #include <boost/test/utils/basic_cstring/compare.hpp>
  21. #include <boost/test/utils/string_cast.hpp>
  22. // Boost
  23. #include <boost/function/function2.hpp>
  24. // STL
  25. #include <vector>
  26. #include <boost/test/detail/suppress_warnings.hpp>
  27. namespace boost {
  28. namespace runtime {
  29. // ************************************************************************** //
  30. // ************** runtime::value_interpreter ************** //
  31. // ************************************************************************** //
  32. template<typename ValueType, bool is_enum>
  33. struct value_interpreter;
  34. //____________________________________________________________________________//
  35. template<typename ValueType>
  36. struct value_interpreter<ValueType, false> {
  37. template<typename Modifiers>
  38. explicit value_interpreter( Modifiers const& ) {}
  39. ValueType interpret( cstring param_name, cstring source ) const
  40. {
  41. ValueType res;
  42. if( !unit_test::utils::string_as<ValueType>( source, res ) )
  43. BOOST_TEST_I_THROW( format_error( param_name ) << source <<
  44. " can't be interpreted as value of parameter " << param_name << "." );
  45. return res;
  46. }
  47. };
  48. //____________________________________________________________________________//
  49. template<>
  50. struct value_interpreter<std::string, false> {
  51. template<typename Modifiers>
  52. explicit value_interpreter( Modifiers const& ) {}
  53. std::string interpret( cstring, cstring source ) const
  54. {
  55. return std::string( source.begin(), source.size() );
  56. }
  57. };
  58. //____________________________________________________________________________//
  59. template<>
  60. struct value_interpreter<cstring, false> {
  61. template<typename Modifiers>
  62. explicit value_interpreter( Modifiers const& ) {}
  63. cstring interpret( cstring, cstring source ) const
  64. {
  65. return source;
  66. }
  67. };
  68. //____________________________________________________________________________//
  69. template<>
  70. struct value_interpreter<bool, false> {
  71. template<typename Modifiers>
  72. explicit value_interpreter( Modifiers const& ) {}
  73. bool interpret( cstring param_name, cstring source ) const
  74. {
  75. static cstring const s_YES( "YES" );
  76. static cstring const s_Y( "Y" );
  77. static cstring const s_NO( "NO" );
  78. static cstring const s_N( "N" );
  79. static cstring const s_TRUE( "TRUE" );
  80. static cstring const s_FALSE( "FALSE" );
  81. static cstring const s_one( "1" );
  82. static cstring const s_zero( "0" );
  83. source.trim();
  84. if( source.is_empty() ||
  85. case_ins_eq( source, s_YES ) ||
  86. case_ins_eq( source, s_Y ) ||
  87. case_ins_eq( source, s_one ) ||
  88. case_ins_eq( source, s_TRUE ) )
  89. return true;
  90. if( case_ins_eq( source, s_NO ) ||
  91. case_ins_eq( source, s_N ) ||
  92. case_ins_eq( source, s_zero ) ||
  93. case_ins_eq( source, s_FALSE ) )
  94. return false;
  95. BOOST_TEST_I_THROW( format_error( param_name ) << source << " can't be interpreted as bool value." );
  96. }
  97. };
  98. //____________________________________________________________________________//
  99. template<typename EnumType>
  100. struct value_interpreter<EnumType, true> {
  101. template<typename Modifiers>
  102. explicit value_interpreter( Modifiers const& m )
  103. #if defined(BOOST_TEST_CLA_NEW_API)
  104. : m_name_to_value( m[enum_values<EnumType>::value] )
  105. {
  106. }
  107. #else
  108. {
  109. std::vector<std::pair<cstring,EnumType> > const& values = m[enum_values<EnumType>::value];
  110. m_name_to_value.insert( values.begin(), values.end() );
  111. }
  112. #endif
  113. EnumType interpret( cstring param_name, cstring source ) const
  114. {
  115. typename std::map<cstring,EnumType>::const_iterator found = m_name_to_value.find( source );
  116. BOOST_TEST_I_ASSRT( found != m_name_to_value.end(),
  117. format_error( param_name ) << source <<
  118. " is not a valid enumeration value name for parameter " << param_name << "." );
  119. return found->second;
  120. }
  121. private:
  122. // Data members
  123. std::map<cstring,EnumType> m_name_to_value;
  124. };
  125. //____________________________________________________________________________//
  126. // ************************************************************************** //
  127. // ************** runtime::argument_factory ************** //
  128. // ************************************************************************** //
  129. template<typename ValueType, bool is_enum, bool repeatable>
  130. class argument_factory;
  131. //____________________________________________________________________________//
  132. template<typename ValueType, bool is_enum>
  133. class argument_factory<ValueType, is_enum, false> {
  134. public:
  135. template<typename Modifiers>
  136. explicit argument_factory( Modifiers const& m )
  137. : m_interpreter( m )
  138. , m_optional_value( nfp::opt_get( m, optional_value, ValueType() ) )
  139. , m_default_value( nfp::opt_get( m, default_value, ValueType() ) )
  140. {
  141. }
  142. void produce_argument( cstring source, cstring param_name, arguments_store& store ) const
  143. {
  144. store.set( param_name, source.empty() ? m_optional_value : m_interpreter.interpret( param_name, source ) );
  145. }
  146. void produce_default( cstring param_name, arguments_store& store ) const
  147. {
  148. store.set( param_name, m_default_value );
  149. }
  150. private:
  151. // Data members
  152. typedef value_interpreter<ValueType, is_enum> interp_t;
  153. interp_t m_interpreter;
  154. ValueType m_optional_value;
  155. ValueType m_default_value;
  156. };
  157. //____________________________________________________________________________//
  158. template<typename ValueType, bool is_enum>
  159. class argument_factory<ValueType, is_enum, true> {
  160. public:
  161. template<typename Modifiers>
  162. explicit argument_factory( Modifiers const& m )
  163. : m_interpreter( m )
  164. {
  165. }
  166. void produce_argument( cstring source, cstring param_name, arguments_store& store ) const
  167. {
  168. ValueType value = m_interpreter.interpret( param_name, source );
  169. if( store.has( param_name ) ) {
  170. std::vector<ValueType>& values = store.get<std::vector<ValueType> >( param_name );
  171. values.push_back( value );
  172. }
  173. else {
  174. std::vector<ValueType> values( 1, value );
  175. store.set( param_name, values );
  176. }
  177. }
  178. void produce_default( cstring param_name, arguments_store& store ) const
  179. {
  180. store.set( param_name, std::vector<ValueType>() );
  181. }
  182. private:
  183. // Data members
  184. value_interpreter<ValueType, is_enum> m_interpreter;
  185. };
  186. //____________________________________________________________________________//
  187. } // namespace runtime
  188. } // namespace boost
  189. #include <boost/test/detail/enable_warnings.hpp>
  190. #endif // BOOST_TEST_UTILS_RUNTIME_ARGUMENT_FACTORY_HPP