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 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/fwd.hpp> 18 19 #include <boost/core/ref.hpp> 20 21 #include <boost/test/detail/suppress_warnings.hpp> 22 23 //____________________________________________________________________________// 24 25 namespace boost { 26 namespace unit_test { 27 namespace data { 28 namespace monomorphic { 29 30 // ************************************************************************** // 31 // ************** generated_by ************** // 32 // ************************************************************************** // 33 34 /*!@brief Generators interface 35 * 36 * This class implements the dataset concept over a generator. Examples of generators are: 37 * - xrange_t 38 * - random_t 39 * 40 * The generator concept is the following: 41 * - the type of the generated samples is given by field @c sample 42 * - the member function @c capacity should return the size of the collection being generated (potentially infinite) 43 * - the member function @c next should change the state of the generator to the next generated value 44 * - the member function @c reset should put the state of the object in the same state as right after its instanciation 45 */ 46 template<typename Generator> 47 class generated_by { 48 public: 49 typedef typename Generator::sample sample; 50 51 enum { arity = 1 }; 52 53 struct iterator { 54 // Constructor iteratorboost::unit_test::data::monomorphic::generated_by::iterator55 explicit iterator( Generator& gen ) 56 : m_gen( &gen ) 57 { 58 if(m_gen->capacity() > 0) { 59 m_gen->reset(); 60 ++*this; 61 } 62 } 63 64 // forward iterator interface operator *boost::unit_test::data::monomorphic::generated_by::iterator65 sample const& operator*() const { return m_curr_sample; } operator ++boost::unit_test::data::monomorphic::generated_by::iterator66 void operator++() { m_curr_sample = m_gen->next(); } 67 68 private: 69 // Data members 70 Generator* m_gen; 71 sample m_curr_sample; 72 }; 73 74 typedef Generator generator_type; 75 76 // Constructor generated_by(Generator && G)77 explicit generated_by( Generator&& G ) 78 : m_generator( std::forward<Generator>(G) ) 79 {} 80 81 // Move constructor generated_by(generated_by && rhs)82 generated_by( generated_by&& rhs ) 83 : m_generator( std::forward<Generator>(rhs.m_generator) ) 84 {} 85 86 //! Size of the underlying dataset size() const87 data::size_t size() const { return m_generator.capacity(); } 88 89 //! Iterator on the beginning of the dataset begin() const90 iterator begin() const { return iterator( boost::ref(const_cast<Generator&>(m_generator)) ); } 91 92 private: 93 // Data members 94 Generator m_generator; 95 }; 96 97 //____________________________________________________________________________// 98 99 //! A generated dataset is a dataset. 100 template<typename Generator> 101 struct is_dataset<generated_by<Generator>> : mpl::true_ {}; 102 103 } // namespace monomorphic 104 } // namespace data 105 } // namespace unit_test 106 } // namespace boost 107 108 #include <boost/test/detail/enable_warnings.hpp> 109 110 #endif // BOOST_TEST_DATA_MONOMORPHIC_GENERATE_HPP_112011GER 111 112