From 089edf9d352da9a66a6fee72d11f0824b7e940bc Mon Sep 17 00:00:00 2001 From: Robert Matusewicz Date: Tue, 19 Aug 2014 19:34:46 +0200 Subject: [PATCH] add support for std::initializer_list to boost::container::static_vector --- include/boost/container/static_vector.hpp | 67 +++++++++++++++++++++++ test/static_vector_test.cpp | 38 +++++++++++++ 2 files changed, 105 insertions(+) diff --git a/include/boost/container/static_vector.hpp b/include/boost/container/static_vector.hpp index e59dd52..1ae8011 100644 --- a/include/boost/container/static_vector.hpp +++ b/include/boost/container/static_vector.hpp @@ -207,6 +207,24 @@ public: : base_t(first, last) {} +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre + //! @li distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Constructs a static_vector containing copy of a range [il.begin(), il.end()). + //! + //! @param il std::initializer_list with values to initialize vector. + //! + //! @par Throws + //! If Value's constructor taking a dereferenced std::initializer_list throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector(std::initializer_list il) + : base_t(il) + {} +#endif + //! @brief Constructs a copy of other static_vector. //! //! @param other The static_vector which content will be copied to this one. @@ -281,6 +299,22 @@ public: return static_cast(base_t::operator=(static_cast(other))); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @brief Copy assigns Values stored in std::initializer_list to *this. + //! + //! @param il The std::initializer_list which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector & operator=(std::initializer_list il) + { + return static_cast(base_t::operator=(il)); + } +#endif + //! @pre other.size() <= capacity() //! //! @brief Copy assigns Values stored in the other static_vector to this one. @@ -538,6 +572,24 @@ public: template iterator insert(const_iterator p, Iterator first, Iterator last); +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Inserts a copy of a range [il.begin(), il.end()) at p. + //! + //! @param p The position at which new elements will be inserted. + //! @param il The std::initializer_list which contains elements that will be inserted. + //! + //! @par Throws + //! @li If Value's constructor and assignment taking a dereferenced std::initializer_list iterator. + //! + //! @par Complexity + //! Linear O(N). + iterator insert(const_iterator p, std::initializer_list il); +#endif + //! @pre \c p must be a valid iterator of \c *this in range [begin(), end()) //! //! @brief Erases Value from p. @@ -582,6 +634,21 @@ public: template void assign(Iterator first, Iterator last); +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Assigns a range [il.begin(), il.end()) of Values to this container. + //! + //! @param first std::initializer_list with values used to construct new content of this container. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws, + //! + //! @par Complexity + //! Linear O(N). + void assign(std::initializer_list il); +#endif + //! @pre count <= capacity() //! //! @brief Assigns a count copies of value to this container. diff --git a/test/static_vector_test.cpp b/test/static_vector_test.cpp index 6cbb315..29788d2 100644 --- a/test/static_vector_test.cpp +++ b/test/static_vector_test.cpp @@ -79,6 +79,41 @@ void test_ctor_nd(size_t n, T const& v) } } +void test_support_for_initializer_list() +{ +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + { + static_vector sv = {10, 8}; + BOOST_TEST(10 == sv[0]); + BOOST_TEST(8 == sv[1]); + + typedef static_vector sv_cap_1; + BOOST_TEST_THROWS(sv_cap_1({1, 1}), std::bad_alloc); + } + + { + static_vector sv; + sv.assign({1, 2}); + BOOST_TEST(1 == sv[0]); + BOOST_TEST(2 == sv[1]); + + BOOST_TEST_THROWS(sv.assign({1, 2, 3}), std::bad_alloc); + + static_vector greaterThanSv = {1, 2, 3}; + BOOST_TEST_THROWS(sv = greaterThanSv, std::bad_alloc); + } + + { + static_vector sv; + sv.insert(sv.begin(), {99, 95}); + BOOST_TEST(99 == sv[0]); + BOOST_TEST(95 == sv[1]); + + BOOST_TEST_THROWS(sv.insert(sv.begin(), {101, 102, 103}), std::bad_alloc); + } +#endif +} + template void test_resize_nc(size_t n) { @@ -654,6 +689,7 @@ bool default_init_test()//Test for default initialization return true; } + int main(int, char* []) { using boost::container::test::movable_and_copyable_int; @@ -773,6 +809,8 @@ int main(int, char* []) BOOST_TEST(default_init_test() == true); + test_support_for_initializer_list(); + return boost::report_errors(); }