initializer_list.hpp 4.5 KB

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