singleton.hpp 3.5 KB

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