singleton.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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
  8. /// Defines single element monomorphic dataset
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER
  11. #define BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER
  12. // Boost.Test
  13. #include <boost/test/data/config.hpp>
  14. #include <boost/test/data/monomorphic/fwd.hpp>
  15. #include <boost/test/detail/suppress_warnings.hpp>
  16. //____________________________________________________________________________//
  17. namespace boost {
  18. namespace unit_test {
  19. namespace data {
  20. namespace monomorphic {
  21. // ************************************************************************** //
  22. // ************** singleton ************** //
  23. // ************************************************************************** //
  24. /// Models a single element data set
  25. template<typename T>
  26. class singleton {
  27. private:
  28. typedef typename boost::decay<T>::type sample;
  29. public:
  30. enum { arity = 1 };
  31. struct iterator {
  32. // Constructor
  33. explicit iterator( singleton<T> const* owner )
  34. : m_owner( owner )
  35. {}
  36. // forward iterator interface
  37. sample const& operator*() const { return m_owner->value(); }
  38. void operator++() {}
  39. private:
  40. singleton<T> const* m_owner;
  41. };
  42. //! Constructor
  43. explicit singleton( T&& value ) : m_value( std::forward<T>( value ) ) {}
  44. //! Move constructor
  45. singleton( singleton&& s ) : m_value( std::forward<T>( s.m_value ) ) {}
  46. //! Value access method
  47. T const& value() const { return m_value; }
  48. //! dataset interface
  49. data::size_t size() const { return 1; }
  50. iterator begin() const { return iterator( this ); }
  51. private:
  52. // Data members
  53. T m_value;
  54. };
  55. //____________________________________________________________________________//
  56. // a singleton is a dataset
  57. template<typename T>
  58. struct is_dataset<singleton<T>> : mpl::true_ {};
  59. //____________________________________________________________________________//
  60. } // namespace monomorphic
  61. /// @overload boost::unit_test::data::make()
  62. template<typename T>
  63. inline typename std::enable_if<!is_container_forward_iterable<T>::value &&
  64. !monomorphic::is_dataset<T>::value &&
  65. !boost::is_array<typename boost::remove_reference<T>::type>::value,
  66. monomorphic::singleton<T>
  67. >::type
  68. make( T&& v )
  69. {
  70. return monomorphic::singleton<T>( std::forward<T>( v ) );
  71. }
  72. //____________________________________________________________________________//
  73. /// @overload boost::unit_test::data::make
  74. inline monomorphic::singleton<char*>
  75. make( char* str )
  76. {
  77. return monomorphic::singleton<char*>( std::move(str) );
  78. }
  79. //____________________________________________________________________________//
  80. /// @overload boost::unit_test::data::make
  81. inline monomorphic::singleton<char const*>
  82. make( char const* str )
  83. {
  84. return monomorphic::singleton<char const*>( std::move(str) );
  85. }
  86. } // namespace data
  87. } // namespace unit_test
  88. } // namespace boost
  89. #include <boost/test/detail/enable_warnings.hpp>
  90. #endif // BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER