predicate_result.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // (C) Copyright Gennadiy Rozental 2001-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: 54633 $
  10. //
  11. // Description : enhanced result for test predicate that include message explaining failure
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_PREDICATE_RESULT_HPP_012705GER
  14. #define BOOST_TEST_PREDICATE_RESULT_HPP_012705GER
  15. // Boost.Test
  16. #include <boost/test/utils/class_properties.hpp>
  17. #include <boost/test/utils/wrap_stringstream.hpp>
  18. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  19. // Boost
  20. #include <boost/shared_ptr.hpp>
  21. #include <boost/detail/workaround.hpp>
  22. // STL
  23. #include <cstddef> // for std::size_t
  24. #include <boost/test/detail/suppress_warnings.hpp>
  25. //____________________________________________________________________________//
  26. namespace boost {
  27. namespace test_tools {
  28. // ************************************************************************** //
  29. // ************** predicate_result ************** //
  30. // ************************************************************************** //
  31. class BOOST_TEST_DECL predicate_result {
  32. typedef unit_test::const_string const_string;
  33. struct dummy { void nonnull() {}; };
  34. typedef void (dummy::*safe_bool)();
  35. public:
  36. // Constructor
  37. predicate_result( bool pv_ )
  38. : p_predicate_value( pv_ )
  39. {}
  40. template<typename BoolConvertable>
  41. predicate_result( BoolConvertable const& pv_ ) : p_predicate_value( !!pv_ ) {}
  42. // Access methods
  43. bool operator!() const { return !p_predicate_value; }
  44. void operator=( bool pv_ ) { p_predicate_value.value = pv_; }
  45. operator safe_bool() const { return !!p_predicate_value ? &dummy::nonnull : 0; }
  46. // Public properties
  47. BOOST_READONLY_PROPERTY( bool, (predicate_result) ) p_predicate_value;
  48. // Access methods
  49. bool has_empty_message() const { return !m_message; }
  50. wrap_stringstream& message()
  51. {
  52. if( !m_message )
  53. m_message.reset( new wrap_stringstream );
  54. return *m_message;
  55. }
  56. const_string message() const { return !m_message ? const_string() : const_string( m_message->str() ); }
  57. private:
  58. // Data members
  59. shared_ptr<wrap_stringstream> m_message;
  60. };
  61. } // namespace test_tools
  62. } // namespace boost
  63. //____________________________________________________________________________//
  64. #include <boost/test/detail/enable_warnings.hpp>
  65. #endif // BOOST_TEST_PREDICATE_RESULT_HPP_012705GER