generate.hpp 3.4 KB

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