argument.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 : model of actual argument (both typed and abstract interface)
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP
  14. #define BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP
  15. // Boost.Test Runtime parameters
  16. #include <boost/test/utils/runtime/fwd.hpp>
  17. #include <boost/test/utils/runtime/errors.hpp>
  18. // Boost.Test
  19. #include <boost/test/utils/class_properties.hpp>
  20. #include <boost/test/utils/rtti.hpp>
  21. #include <boost/test/utils/basic_cstring/compare.hpp>
  22. #include <boost/test/detail/throw_exception.hpp>
  23. // STL
  24. #include <cassert>
  25. #include <boost/test/detail/suppress_warnings.hpp>
  26. namespace boost {
  27. namespace runtime {
  28. // ************************************************************************** //
  29. // ************** runtime::argument ************** //
  30. // ************************************************************************** //
  31. class argument {
  32. public:
  33. // Constructor
  34. argument( rtti::id_t value_type )
  35. : p_value_type( value_type )
  36. {}
  37. // Destructor
  38. virtual ~argument() {}
  39. // Public properties
  40. rtti::id_t const p_value_type;
  41. };
  42. // ************************************************************************** //
  43. // ************** runtime::typed_argument ************** //
  44. // ************************************************************************** //
  45. template<typename T>
  46. class typed_argument : public argument {
  47. public:
  48. // Constructor
  49. explicit typed_argument( T const& v )
  50. : argument( rtti::type_id<T>() )
  51. , p_value( v )
  52. {}
  53. unit_test::readwrite_property<T> p_value;
  54. };
  55. // ************************************************************************** //
  56. // ************** runtime::arguments_store ************** //
  57. // ************************************************************************** //
  58. class arguments_store {
  59. public:
  60. typedef std::map<cstring, argument_ptr> storage_type;
  61. /// Returns number of arguments in the store; mostly used for testing
  62. std::size_t size() const { return m_arguments.size(); }
  63. /// Clears the store for reuse
  64. void clear() { m_arguments.clear(); }
  65. /// Returns true if there is an argument corresponding to the specified parameter name
  66. bool has( cstring parameter_name ) const
  67. {
  68. return m_arguments.find( parameter_name ) != m_arguments.end();
  69. }
  70. /// Provides types access to argument value by parameter name
  71. template<typename T>
  72. T const& get( cstring parameter_name ) const {
  73. return const_cast<arguments_store*>(this)->get<T>( parameter_name );
  74. }
  75. template<typename T>
  76. T& get( cstring parameter_name ) {
  77. storage_type::const_iterator found = m_arguments.find( parameter_name );
  78. BOOST_TEST_I_ASSRT( found != m_arguments.end(),
  79. access_to_missing_argument()
  80. << "There is no argument provided for parameter "
  81. << parameter_name );
  82. argument_ptr arg = found->second;
  83. BOOST_TEST_I_ASSRT( arg->p_value_type == rtti::type_id<T>(),
  84. arg_type_mismatch()
  85. << "Access with invalid type for argument corresponding to parameter "
  86. << parameter_name );
  87. return static_cast<typed_argument<T>&>( *arg ).p_value.value;
  88. }
  89. /// Set's the argument value for specified parameter name
  90. template<typename T>
  91. void set( cstring parameter_name, T const& value )
  92. {
  93. m_arguments[parameter_name] = argument_ptr( new typed_argument<T>( value ) );
  94. }
  95. private:
  96. // Data members
  97. storage_type m_arguments;
  98. };
  99. } // namespace runtime
  100. } // namespace boost
  101. #include <boost/test/detail/enable_warnings.hpp>
  102. #endif // BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP