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 monomorphic dataset based on C++11 initializer_list template
10 // ***************************************************************************
11 
12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_INITIALIZATION_LIST_HPP_091515GER
13 #define BOOST_TEST_DATA_MONOMORPHIC_INITIALIZATION_LIST_HPP_091515GER
14 
15 // Boost.Test
16 #include <boost/test/data/config.hpp>
17 #include <boost/test/data/monomorphic/fwd.hpp>
18 
19 #include <boost/core/ignore_unused.hpp>
20 
21 #include <vector>
22 
23 #include <boost/test/detail/suppress_warnings.hpp>
24 
25 //____________________________________________________________________________//
26 
27 namespace boost {
28 namespace unit_test {
29 namespace data {
30 namespace monomorphic {
31 
32 // ************************************************************************** //
33 // **************                initializer_list              ************** //
34 // ************************************************************************** //
35 
36 /// Dataset view from an initializer_list or variadic template arguments
37 ///
38 /// The data should be stored in the dataset, and since the elements
39 /// are passed by an @c std::initializer_list , it implies a copy of
40 /// the elements.
41 template<typename T>
42 class init_list {
43 public:
44     enum { arity = 1 };
45 
46     typedef typename std::vector<T>::const_iterator iterator;
47 
48     //! Constructor copies content of initializer_list
init_list(std::initializer_list<T> il)49     init_list( std::initializer_list<T> il )
50     : m_data( il )
51     {}
52 
53 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
54     !defined(BOOST_TEST_ERRONEOUS_INIT_LIST)
55     //! Variadic template initialization
56     template <class ...Args>
init_list(Args &&...args)57     init_list( Args&& ... args ) {
58       int dummy[] = { 0, (m_data.emplace_back(std::forward<Args&&>(args)), 0)... };
59       boost::ignore_unused(dummy);
60     }
61 #endif
62 
63     //! dataset interface
size() const64     data::size_t    size() const    { return m_data.size(); }
begin() const65     iterator        begin() const   { return m_data.begin(); }
66 
67 private:
68     // Data members
69     std::vector<T> m_data;
70 };
71 
72 //! Specialization of init_list for type bool
73 template <>
74 class init_list<bool> {
75 public:
76     typedef bool sample;
77 
78     enum { arity = 1 };
79 
80     //! Constructor copies content of initializer_list
init_list(std::initializer_list<bool> && il)81     init_list( std::initializer_list<bool>&& il )
82     : m_data( std::forward<std::initializer_list<bool>>( il ) )
83     {}
84 
85 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
86     !defined(BOOST_TEST_ERRONEOUS_INIT_LIST)
87     //! Variadic template initialization
88     template <class ...Args>
init_list(Args &&...args)89     init_list( Args&& ... args ) : m_data{ args... }
90     { }
91 #endif
92 
93     struct non_proxy_iterator {
94         std::vector<bool>::const_iterator iterator;
non_proxy_iteratorboost::unit_test::data::monomorphic::init_list::non_proxy_iterator95         non_proxy_iterator(std::vector<bool>::const_iterator &&it)
96         : iterator(std::forward<std::vector<bool>::const_iterator>(it))
97         {}
98 
operator *boost::unit_test::data::monomorphic::init_list::non_proxy_iterator99         bool operator*() const {
100             return *iterator;
101         }
102 
operator ++boost::unit_test::data::monomorphic::init_list::non_proxy_iterator103         non_proxy_iterator& operator++() {
104             ++iterator;
105             return *this;
106         }
107     };
108 
109     typedef non_proxy_iterator iterator;
110 
111     //! dataset interface
size() const112     data::size_t    size() const    { return m_data.size(); }
begin() const113     iterator        begin() const   { return m_data.begin(); }
114 
115 private:
116     // Data members
117     std::vector<bool> m_data;
118 };
119 
120 //____________________________________________________________________________//
121 
122 //! An array dataset is a dataset
123 template<typename T>
124 struct is_dataset<init_list<T>> : mpl::true_ {};
125 
126 } // namespace monomorphic
127 
128 //____________________________________________________________________________//
129 
130 //! @overload boost::unit_test::data::make()
131 template<typename T>
132 inline monomorphic::init_list<T>
make(std::initializer_list<T> && il)133 make( std::initializer_list<T>&& il )
134 {
135     return monomorphic::init_list<T>( std::forward<std::initializer_list<T>>( il ) );
136 }
137 
138 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
139     !defined(BOOST_TEST_ERRONEOUS_INIT_LIST)
140 template<class T, class ...Args>
141 inline typename std::enable_if<
142   !monomorphic::has_dataset<T, Args...>::value,
143   monomorphic::init_list<T>
144 >::type
make(T && arg0,Args &&...args)145 make( T&& arg0, Args&&... args )
146 {
147     return monomorphic::init_list<T>( std::forward<T>(arg0), std::forward<Args>( args )... );
148 }
149 #endif
150 
151 
152 } // namespace data
153 } // namespace unit_test
154 } // namespace boost
155 
156 #include <boost/test/detail/enable_warnings.hpp>
157 
158 #endif // BOOST_TEST_DATA_MONOMORPHIC_INITIALIZATION_LIST_HPP_091515GER
159 
160