liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//  (C) Copyright Raffi Enficiaud 2018.
//  Distributed under the Boost Software License, Version 1.0.
//  (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
 
//  See http://www.boost.org/libs/test for the library home page.
//
/// @file
/// Defines a lazy/delayed dataset store
// ***************************************************************************
 
#ifndef BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER
#define BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER
 
// Boost.Test
#include <boost/test/data/config.hpp>
#include <boost/test/data/monomorphic/fwd.hpp>
#include <boost/test/data/index_sequence.hpp>
 
#include <boost/core/ref.hpp>
 
#include <algorithm>
#include <memory>
 
#include <boost/test/detail/suppress_warnings.hpp>
 
//____________________________________________________________________________//
 
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
    !defined(BOOST_NO_CXX11_HDR_TUPLE)
 
namespace boost {
namespace unit_test {
namespace data {
namespace monomorphic {
 
// ************************************************************************** //
// **************               delayed_dataset                ************** //
// ************************************************************************** //
 
 
/// Delayed dataset
///
/// This dataset holds another dataset that is instanciated on demand. It is
/// constructed with the @c data::make_delayed<dataset_t>(arg1,....) instead of the
/// @c data::make.
template <class dataset_t, class ...Args>
class delayed_dataset
{
public:
    enum { arity = dataset_t::arity };
    using iterator = decltype(std::declval<dataset_t>().begin());
 
    delayed_dataset(Args... args)
    : m_args(std::make_tuple(std::forward<Args>(args)...))
    {}
 
    // Mostly for VS2013
    delayed_dataset(delayed_dataset&& b) 
    : m_args(std::move(b.m_args))
    , m_dataset(std::move(b.m_dataset))
    {}
 
    boost::unit_test::data::size_t size() const {
        return this->get().size();
    }
 
    // iterator
    iterator begin() const {
        return this->get().begin();
    }
  
private:
 
  dataset_t& get() const {
      if(!m_dataset) {
          m_dataset = create(boost::unit_test::data::index_sequence_for<Args...>());
      }
      return *m_dataset;
  }
 
  template<std::size_t... I>
  std::unique_ptr<dataset_t>
  create(boost::unit_test::data::index_sequence<I...>) const
  {
      return std::unique_ptr<dataset_t>{new dataset_t(std::get<I>(m_args)...)};
  }
 
  std::tuple<typename std::decay<Args>::type...> m_args;
  mutable std::unique_ptr<dataset_t> m_dataset;
};
 
//____________________________________________________________________________//
 
//! A lazy/delayed dataset is a dataset.
template <class dataset_t, class ...Args>
struct is_dataset< delayed_dataset<dataset_t, Args...> > : boost::mpl::true_ {};
 
//____________________________________________________________________________//
 
} // namespace monomorphic
 
 
//! Delayed dataset instanciation
template<class dataset_t, class ...Args>
inline typename std::enable_if<
  monomorphic::is_dataset< dataset_t >::value,
  monomorphic::delayed_dataset<dataset_t, Args...>
>::type
make_delayed(Args... args)
{
    return monomorphic::delayed_dataset<dataset_t, Args...>( std::forward<Args>(args)... );
}
 
 
} // namespace data
} // namespace unit_test
} // namespace boost
 
#endif
 
#include <boost/test/detail/enable_warnings.hpp>
 
#endif // BOOST_TEST_DATA_MONOMORPHIC_DELAYED_HPP_062018GER