1 // (C) Copyright Gennadiy Rozental 2011-2014. 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 generic interface for monomorphic dataset based on generator 10 // *************************************************************************** 11 12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_GENERATE_HPP_112011GER 13 #define BOOST_TEST_DATA_MONOMORPHIC_GENERATE_HPP_112011GER 14 15 // Boost.Test 16 #include <boost/test/data/config.hpp> 17 #include <boost/test/data/monomorphic/dataset.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 // ************** generated_by ************** // 30 // ************************************************************************** // 31 32 /*!@brief Generators interface 33 * 34 * This class implements the dataset concept over a generator. Examples of generators are: 35 * - xrange_t 36 * - random_t 37 * 38 * The generator concept is the following: 39 * - the type of the generated samples is given by field @c data_type 40 * - the member function @c capacity should return the size of the collection being generated (potentially infinite) 41 * - the member function @c next should change the state of the generator to the next generated value 42 * - the member function @c reset should put the state of the object in the same state as right after its instanciation 43 */ 44 template<typename Generator> 45 class generated_by : public monomorphic::dataset<typename Generator::data_type> { 46 typedef typename Generator::data_type T; 47 typedef monomorphic::dataset<T> base; 48 typedef typename base::iter_ptr iter_ptr; 49 50 struct iterator : public base::iterator { 51 // Constructor iteratorboost::unit_test::data::monomorphic::generated_by::iterator52 explicit iterator( Generator& gen ) 53 : m_gen( gen ) 54 { 55 if(m_gen.capacity() > 0) { 56 m_gen.reset(); 57 ++*this; 58 } 59 } 60 61 // forward iterator interface operator *boost::unit_test::data::monomorphic::generated_by::iterator62 virtual T const& operator*() { return m_curr_sample; } operator ++boost::unit_test::data::monomorphic::generated_by::iterator63 virtual void operator++() { m_curr_sample = m_gen.next(); } 64 65 private: 66 // Data members 67 Generator& m_gen; 68 T m_curr_sample; 69 }; 70 public: 71 enum { arity = 1 }; 72 typedef Generator generator_type; 73 74 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES 75 // Constructor generated_by(Generator && G)76 explicit generated_by( Generator&& G ) 77 : m_generator( std::forward<Generator>(G) ) 78 {} 79 80 // Move constructor generated_by(generated_by && rhs)81 generated_by( generated_by&& rhs ) 82 : m_generator( std::forward<Generator>(rhs.m_generator) ) 83 {} 84 #else 85 // Constructor generated_by(Generator const & G)86 explicit generated_by( Generator const& G ) 87 : m_generator( G ) 88 {} 89 #endif 90 91 //! Size of the underlying dataset size() const92 data::size_t size() const { return m_generator.capacity(); } 93 94 //! Iterator on the beginning of the dataset begin() const95 virtual iter_ptr begin() const { return boost::make_shared<iterator>( boost::ref(const_cast<Generator&>(m_generator)) ); } 96 97 private: 98 // Data members 99 Generator m_generator; 100 }; 101 102 103 //! A generated dataset is a dataset. 104 template<typename Generator> 105 struct is_dataset<generated_by<Generator> > : mpl::true_ {}; 106 107 108 } // namespace monomorphic 109 } // namespace data 110 } // namespace unit_test 111 } // namespace boost 112 113 #include <boost/test/detail/enable_warnings.hpp> 114 115 #endif // BOOST_TEST_DATA_MONOMORPHIC_GENERATE_HPP_112011GER 116 117