delayed.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // (C) Copyright Raffi Enficiaud 2018.
  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 a lazy/delayed dataset store
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER
  11. #define BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER
  12. // Boost.Test
  13. #include <boost/test/data/config.hpp>
  14. #include <boost/test/data/monomorphic/fwd.hpp>
  15. #include <boost/test/data/index_sequence.hpp>
  16. #include <boost/core/ref.hpp>
  17. #include <algorithm>
  18. #include <memory>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. //____________________________________________________________________________//
  21. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  22. !defined(BOOST_NO_CXX11_HDR_TUPLE)
  23. namespace boost {
  24. namespace unit_test {
  25. namespace data {
  26. namespace monomorphic {
  27. // ************************************************************************** //
  28. // ************** delayed_dataset ************** //
  29. // ************************************************************************** //
  30. /// Delayed dataset
  31. ///
  32. /// This dataset holds another dataset that is instanciated on demand. It is
  33. /// constructed with the @c data::make_delayed<dataset_t>(arg1,....) instead of the
  34. /// @c data::make.
  35. template <class dataset_t, class ...Args>
  36. class delayed_dataset
  37. {
  38. public:
  39. enum { arity = dataset_t::arity };
  40. using iterator = decltype(std::declval<dataset_t>().begin());
  41. delayed_dataset(Args... args)
  42. : m_args(std::make_tuple(std::forward<Args>(args)...))
  43. {}
  44. // Mostly for VS2013
  45. delayed_dataset(delayed_dataset&& b)
  46. : m_args(std::move(b.m_args))
  47. , m_dataset(std::move(b.m_dataset))
  48. {}
  49. boost::unit_test::data::size_t size() const {
  50. return this->get().size();
  51. }
  52. // iterator
  53. iterator begin() const {
  54. return this->get().begin();
  55. }
  56. private:
  57. dataset_t& get() const {
  58. if(!m_dataset) {
  59. m_dataset = create(boost::unit_test::data::index_sequence_for<Args...>());
  60. }
  61. return *m_dataset;
  62. }
  63. template<std::size_t... I>
  64. std::unique_ptr<dataset_t>
  65. create(boost::unit_test::data::index_sequence<I...>) const
  66. {
  67. return std::unique_ptr<dataset_t>{new dataset_t(std::get<I>(m_args)...)};
  68. }
  69. std::tuple<typename std::decay<Args>::type...> m_args;
  70. mutable std::unique_ptr<dataset_t> m_dataset;
  71. };
  72. //____________________________________________________________________________//
  73. //! A lazy/delayed dataset is a dataset.
  74. template <class dataset_t, class ...Args>
  75. struct is_dataset< delayed_dataset<dataset_t, Args...> > : boost::mpl::true_ {};
  76. //____________________________________________________________________________//
  77. } // namespace monomorphic
  78. //! Delayed dataset instanciation
  79. template<class dataset_t, class ...Args>
  80. inline typename std::enable_if<
  81. monomorphic::is_dataset< dataset_t >::value,
  82. monomorphic::delayed_dataset<dataset_t, Args...>
  83. >::type
  84. make_delayed(Args... args)
  85. {
  86. return monomorphic::delayed_dataset<dataset_t, Args...>( std::forward<Args>(args)... );
  87. }
  88. } // namespace data
  89. } // namespace unit_test
  90. } // namespace boost
  91. #endif
  92. #include <boost/test/detail/enable_warnings.hpp>
  93. #endif // BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER