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 6 // See http://www.boost.org/libs/test for the library home page. 7 // 8 /// @file 9 /// Defines single element monomorphic dataset 10 // *************************************************************************** 11 12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER 13 #define BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER 14 15 // Boost.Test 16 #include <boost/test/data/config.hpp> 17 #include <boost/test/data/monomorphic/fwd.hpp> 18 19 #include <boost/test/detail/suppress_warnings.hpp> 20 21 //____________________________________________________________________________// 22 23 namespace boost { 24 namespace unit_test { 25 namespace data { 26 namespace monomorphic { 27 28 // ************************************************************************** // 29 // ************** singleton ************** // 30 // ************************************************************************** // 31 32 /// Models a single element data set 33 template<typename T> 34 class singleton { 35 private: 36 typedef typename boost::decay<T>::type sample; 37 38 public: 39 40 enum { arity = 1 }; 41 42 struct iterator { 43 // Constructor iteratorboost::unit_test::data::monomorphic::singleton::iterator44 explicit iterator( singleton<T> const* owner ) 45 : m_owner( owner ) 46 {} 47 48 // forward iterator interface operator *boost::unit_test::data::monomorphic::singleton::iterator49 sample const& operator*() const { return m_owner->value(); } operator ++boost::unit_test::data::monomorphic::singleton::iterator50 void operator++() {} 51 52 private: 53 singleton<T> const* m_owner; 54 }; 55 56 //! Constructor singleton(T && value)57 explicit singleton( T&& value ) : m_value( std::forward<T>( value ) ) {} 58 59 //! Move constructor singleton(singleton && s)60 singleton( singleton&& s ) : m_value( std::forward<T>( s.m_value ) ) {} 61 62 //! Value access method value() const63 T const& value() const { return m_value; } 64 65 //! dataset interface size() const66 data::size_t size() const { return 1; } begin() const67 iterator begin() const { return iterator( this ); } 68 69 private: 70 // Data members 71 T m_value; 72 }; 73 74 //____________________________________________________________________________// 75 76 // a singleton is a dataset 77 template<typename T> 78 struct is_dataset<singleton<T>> : mpl::true_ {}; 79 80 //____________________________________________________________________________// 81 82 } // namespace monomorphic 83 84 /// @overload boost::unit_test::data::make() 85 template<typename T> 86 inline typename std::enable_if<!is_container_forward_iterable<T>::value && 87 !monomorphic::is_dataset<T>::value && 88 !boost::is_array<typename boost::remove_reference<T>::type>::value, 89 monomorphic::singleton<T> 90 >::type make(T && v)91make( T&& v ) 92 { 93 return monomorphic::singleton<T>( std::forward<T>( v ) ); 94 } 95 96 //____________________________________________________________________________// 97 98 /// @overload boost::unit_test::data::make 99 inline monomorphic::singleton<char*> make(char * str)100make( char* str ) 101 { 102 return monomorphic::singleton<char*>( std::move(str) ); 103 } 104 105 //____________________________________________________________________________// 106 107 /// @overload boost::unit_test::data::make 108 inline monomorphic::singleton<char const*> make(char const * str)109make( char const* str ) 110 { 111 return monomorphic::singleton<char const*>( std::move(str) ); 112 } 113 114 } // namespace data 115 } // namespace unit_test 116 } // namespace boost 117 118 #include <boost/test/detail/enable_warnings.hpp> 119 120 #endif // BOOST_TEST_DATA_MONOMORPHIC_SINGLETON_HPP_102211GER 121 122