diff options
author | Andrew Geissler <andrewg@us.ibm.com> | 2016-03-11 08:46:44 -0600 |
---|---|---|
committer | A. P. Williams III <iawillia@us.ibm.com> | 2016-03-17 11:44:46 -0400 |
commit | 71cf29da3d308290ffd860fb263b4a05ddcb364a (patch) | |
tree | d8ff026c8cb8eb2d1beff2adadbbd118549c9e56 | |
parent | 497d9f20a3bc65847435c82358b32cd2cd6d6951 (diff) | |
download | talos-hostboot-71cf29da3d308290ffd860fb263b4a05ddcb364a.tar.gz talos-hostboot-71cf29da3d308290ffd860fb263b4a05ddcb364a.zip |
Need to implement new Ctor for std::vector
Change-Id: Ia7743dc8d3a83b4be361626b1dea57f39e4dd483
RTC: 149398
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/21938
Tested-by: Jenkins Server
Reviewed-by: Christian R. Geddes <crgeddes@us.ibm.com>
Reviewed-by: A. P. Williams III <iawillia@us.ibm.com>
-rw-r--r-- | src/include/initializer_list | 123 | ||||
-rw-r--r-- | src/include/list | 64 | ||||
-rwxr-xr-x | src/include/usr/cxxtest/TestSuite.H | 2 | ||||
-rw-r--r-- | src/include/vector | 61 | ||||
-rw-r--r-- | src/usr/testcore/lib/stl_functional.H | 91 |
5 files changed, 292 insertions, 49 deletions
diff --git a/src/include/initializer_list b/src/include/initializer_list new file mode 100644 index 000000000..0926254b7 --- /dev/null +++ b/src/include/initializer_list @@ -0,0 +1,123 @@ +/* IBM_PROLOG_BEGIN_TAG */ +/* This is an automatically generated prolog. */ +/* */ +/* $Source: src/include/initializer_list $ */ +/* */ +/* OpenPOWER HostBoot Project */ +/* */ +/* Contributors Listed Below - COPYRIGHT 2016 */ +/* [+] International Business Machines Corp. */ +/* */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); */ +/* you may not use this file except in compliance with the License. */ +/* You may obtain a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */ +/* implied. See the License for the specific language governing */ +/* permissions and limitations under the License. */ +/* */ +/* IBM_PROLOG_END_TAG */ +#ifndef stl_initializer_list +#define stl_initializer_list + +/** + * @file initializer_list + * @brief simple stl initalizer_list template class declaration. + * + * Provides compile time stl initialization in vector and list classes + */ + +#include <stddef.h> + +#if !defined( __STDC_LIMIT_MACROS) +#define __STDC_LIMIT_MACROS +#endif +#include <stdint.h> +#include <new> +#include <algorithm> +#include <assert.h> + +namespace std +{ + + /** + * @class initializer_list + * subset of stl initializer_list + */ + template <class T> + class initializer_list + { + public: + + typedef T value_type; + typedef T & reference; + typedef const T & const_reference; + typedef size_t size_type; + typedef T * iterator; + typedef const T * const_iterator; + typedef T * pointer; + + + private: + + iterator iv_array; + size_type iv_len; + + /** + * @brief Private constructor for compiler when someone declares + * initializer list using {} + * @note The compiler can call this private constructor + */ + constexpr initializer_list(const_iterator x, size_type s) + : + iv_array(x), + iv_len(s) + {} + + public: + + /** + * @brief Constructor default + */ + constexpr initializer_list(void) + : + iv_array(NULL), + iv_len(NULL) + {} + + /** + * @brief Get the number of elements in the container + * @return number of elements in the container + */ + constexpr size_type size() const + { + return iv_len; + } + + // Iterators -------------------- + + /** + * @brief Get const_iterator to the first element + * @return const_iterator of first element + */ + constexpr const_iterator begin() const + { + return (iv_array); + } + + /** + * @brief Get const_iterator to the last element + 1 + * @return const_iterator to last element + 1 + */ + constexpr const_iterator end() const + { + return (iv_array + iv_len); + } + }; +} +#endif diff --git a/src/include/list b/src/include/list index 01e606a93..f7b0195ad 100644 --- a/src/include/list +++ b/src/include/list @@ -5,7 +5,9 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* COPYRIGHT International Business Machines Corp. 2011,2014 */ +/* Contributors Listed Below - COPYRIGHT 2011,2016 */ +/* [+] International Business Machines Corp. */ +/* */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); */ /* you may not use this file except in compliance with the License. */ @@ -30,13 +32,14 @@ #include <stdint.h> #include <new> #include <algorithm> +#include <initializer_list> namespace std { /** * @class ListNode_t - * + * * @note Non template dependent part of a list node. This code will not * get duplicated for each type T */ @@ -76,7 +79,7 @@ namespace std temp.unlink(); // take temp out of chain } - /** + /** * link this node into the i_node chain just before i_node. * @param[in] A node in node chain to link this node into */ @@ -121,7 +124,7 @@ namespace std i_first->iv_prev = nodeP; i_last->iv_next = this; } - + /** * Query if node is part of a chain */ @@ -133,7 +136,7 @@ namespace std /** * @class ListNodeData_t - * + * * @note Template dependent part of a list node. */ template <typename T> @@ -218,9 +221,9 @@ namespace std * @return reference to iterator */ __attribute__ ((always_inline)) inline - _This operator++(int) + _This operator++(int) { - _This tmp = *this; + _This tmp = *this; iv_node = iv_node->iv_next; return tmp; } @@ -304,7 +307,7 @@ namespace std */ __attribute__ ((always_inline)) inline ListConstIterator_t() : iv_node() - {} + {} /** * Construct from a const node pointer @@ -312,7 +315,7 @@ namespace std */ __attribute__ ((always_inline)) inline explicit ListConstIterator_t(const ListNode_t* i_ln) : iv_node(i_ln) - {} + {} /** * Construct from another const_iterator @@ -338,7 +341,7 @@ namespace std */ __attribute__ ((always_inline)) inline pointer operator->() const - { + { return &static_cast<_Node*>(iv_node)->iv_data; } @@ -350,19 +353,19 @@ namespace std _This& operator++() { iv_node = iv_node->iv_next; return *this; - } + } /** * Post Increment * @return reference to iterator */ __attribute__ ((always_inline)) inline - _This operator++(int) + _This operator++(int) { - _This tmp = *this; + _This tmp = *this; iv_node = iv_node->iv_next; return tmp; - } + } /** * Pre decrement @@ -373,8 +376,8 @@ namespace std { iv_node = iv_node->iv_prev; return *this; - } - + } + /** * Post decrement * @return reference to iterator @@ -385,7 +388,7 @@ namespace std _This tmp = *this; iv_node = iv_node->iv_prev; return *this; - } + } /** * Comparison @@ -397,7 +400,7 @@ namespace std { return iv_node == i_ln.iv_node; } - + /** * Negative Comparison * @param[in] Itertor to compare @@ -433,8 +436,8 @@ namespace std /** * @class std::list - * - * @note + * + * @note * <p>Does not support: * <ul> * <li> allocators </li> @@ -528,6 +531,19 @@ namespace std } /** + * ctor with input initialization_list + * @param[in] init_list Initialization list to populate list with + */ + list(std::initializer_list<T> init_list) + { + iv_node.reset(); + for (auto&& i: init_list) + { + push_back(i); + } + } + + /** * ctor from InputTerator * @param[i] first Iterator to first element to copy * @param[i] last Iterator to last element + 1 to copy. @@ -536,7 +552,7 @@ namespace std list ( InputIterator first, InputIterator last) { iv_node.reset(); - while(first != last) push_back(*first++); + while(first != last) push_back(*first++); } /** @@ -606,7 +622,7 @@ namespace std //const_reverse_iterator rend() const; // ---------------- Capacity - + /** * Query for empty container * @return bool, true if size()==0 else false. @@ -793,7 +809,7 @@ namespace std { while(n--) insert ( pos, ref); } - + /** * Insert a copy of a container slice into this container @@ -889,7 +905,7 @@ namespace std * @param[in] x list object containing the same type of elements as this one. * @param[in] i postion source in list x to move element from * @pre x can be *this if pos != i - * @post Iterators pointing to moved elements are no longer valid. + * @post Iterators pointing to moved elements are no longer valid. * this->size() += 1, if (x != *this) x.size() -= 1; */ void splice ( iterator pos, list<T>& x, iterator i) diff --git a/src/include/usr/cxxtest/TestSuite.H b/src/include/usr/cxxtest/TestSuite.H index 37ea93310..3957cfd49 100755 --- a/src/include/usr/cxxtest/TestSuite.H +++ b/src/include/usr/cxxtest/TestSuite.H @@ -5,7 +5,7 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* Contributors Listed Below - COPYRIGHT 2011,2015 */ +/* Contributors Listed Below - COPYRIGHT 2011,2016 */ /* [+] International Business Machines Corp. */ /* */ /* */ diff --git a/src/include/vector b/src/include/vector index bbe33df47..da83776e8 100644 --- a/src/include/vector +++ b/src/include/vector @@ -5,7 +5,7 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* Contributors Listed Below - COPYRIGHT 2011,2015 */ +/* Contributors Listed Below - COPYRIGHT 2011,2016 */ /* [+] International Business Machines Corp. */ /* */ /* */ @@ -39,6 +39,7 @@ #include <new> #include <algorithm> #include <assert.h> +#include <initializer_list> namespace std { @@ -122,6 +123,24 @@ namespace std } /** + * CTOR create a vector from initializer_list + * @param[in] init_list Initializer list + * @returns None. + * @post vector is created with init_list items + */ + vector(std::initializer_list<T> init_list) + : + iv_start(NULL), + iv_finish(NULL), + iv_end_of_storage(NULL) + { + for (auto&& i: init_list) + { + push_back(i); + } + } + + /** * CTOR create a vector from a container slice * @param[in] first iterator first in source sequence * @param[in] last iterator one past end of source sequence @@ -158,7 +177,7 @@ namespace std /** * Assignment operator. * @param[in] x A vector. - * @return A vector (for the purpose of multiple assigns). + * @return A vector (for the purpose of multiple assigns). * @pre None. * @post *this == x, this->capacity() == x.size(). * All previously obtained iterators are invalid. @@ -168,11 +187,11 @@ namespace std clear(); reserve(x.size()); iv_finish = ctor_copy(x.iv_start, x.iv_finish, iv_start); - return(*this); + return(*this); } - + // Iterators -------------------- - + /** * Get iterator to the first vector element * @return iterator of rist vector element @@ -181,7 +200,7 @@ namespace std */ __attribute__ ((always_inline)) iterator begin() - { + { return (iv_start); } @@ -193,7 +212,7 @@ namespace std */ __attribute__ ((always_inline)) const_iterator begin() const - { + { return (iv_start); } @@ -205,7 +224,7 @@ namespace std */ __attribute__ ((always_inline)) iterator end() - { + { return (iv_finish); } @@ -217,7 +236,7 @@ namespace std */ __attribute__ ((always_inline)) const_iterator end() const - { + { return (iv_finish); } @@ -267,7 +286,7 @@ namespace std /** * Get the number of elements the vector can hold before needing to reallocate storage. - * @return element capacity of the vector + * @return element capacity of the vector * @pre None. * @post None. */ @@ -310,7 +329,7 @@ namespace std __attribute__ ((always_inline)) reference operator[](size_type n) { - return(*(iv_start + n)); + return(*(iv_start + n)); } /** @@ -339,7 +358,7 @@ namespace std const_reference operator[](size_type index) const { assert(index < size()); - return(*(iv_start + index)); + return(*(iv_start + index)); } /** @@ -426,7 +445,7 @@ namespace std /* * Assign new content to the vector object * @param[in] n number of elements to assign - * @param[in] x reference to element to copy in + * @param[in] x reference to element to copy in */ void assign ( size_type n, const T& x) { @@ -444,7 +463,7 @@ namespace std * @post All previously obtained iterators are invalid. */ __attribute__ ((always_inline)) - void push_back(const T& x) + void push_back(const T& x) { reserve(size() + 1); new (iv_finish++) T(x); @@ -500,7 +519,7 @@ namespace std * @note element pointed to by last is not inserted. */ template <class InputIterator> - void insert (iterator position, InputIterator first, + void insert (iterator position, InputIterator first, InputIterator last); @@ -530,7 +549,7 @@ namespace std * @post All previously obtained iterators are invalid. * @note The element pointed to be last is not deleted. */ - iterator erase(iterator first, iterator last) + iterator erase(iterator first, iterator last) { assert(last >= first); assert(first >= iv_start); @@ -545,7 +564,7 @@ namespace std iv_finish->~T(); } return first; - } + } /** @@ -653,7 +672,7 @@ namespace std ++first; } } - + /** * Free all the storage allocated to this vector @@ -670,7 +689,7 @@ namespace std * @param[in] n, number of elements required */ __attribute__ ((always_inline)) - iterator allocate_storage(size_type n) + iterator allocate_storage(size_type n) { return (iterator) new uint8_t[n * sizeof(T)]; } @@ -794,7 +813,7 @@ void std::vector<T>::insert (iterator position, size_type n, const T& x) template <class T> template <class InputIterator> void std::vector<T>::insert (iterator position, - InputIterator first, + InputIterator first, InputIterator last) // Should only move storage if there is not room // InputIterators are not random access (eg. can't do diff = last - first) @@ -863,5 +882,5 @@ void std::vector<T>::resize(size_type n, T x) // else n == size() do nothing } -#endif +#endif /* vim: set filetype=cpp : */ diff --git a/src/usr/testcore/lib/stl_functional.H b/src/usr/testcore/lib/stl_functional.H index 5aac940f0..4b3fe6ea0 100644 --- a/src/usr/testcore/lib/stl_functional.H +++ b/src/usr/testcore/lib/stl_functional.H @@ -5,7 +5,7 @@ /* */ /* OpenPOWER HostBoot Project */ /* */ -/* Contributors Listed Below - COPYRIGHT 2014 */ +/* Contributors Listed Below - COPYRIGHT 2014,2016 */ /* [+] International Business Machines Corp. */ /* */ /* */ @@ -28,6 +28,8 @@ #include <cxxtest/TestSuite.H> #include <functional> #include <vector> +#include <list> +#include <initializer_list> static bool STLFunctionalTest__matches1(int i) { @@ -42,7 +44,7 @@ static bool STLFunctionalTest__matches(int n, int m) class STLFunctionalTest : public CxxTest::TestSuite { public: - void testUnaryPtrFun() + void test_UnaryPtrFun() { using std::ptr_fun; @@ -57,7 +59,7 @@ class STLFunctionalTest : public CxxTest::TestSuite } } - void testBinaryPtrFun() + void test_BinaryPtrFun() { using std::ptr_fun; using std::vector; @@ -84,6 +86,89 @@ class STLFunctionalTest : public CxxTest::TestSuite } } } + + void test_InitializerListVectorFun() + { + using std::vector; + + vector<uint32_t> v {0,1,2,3,4}; + uint32_t VEC_SIZE = 5; + + TS_TRACE("max size of vector v is %d",v.max_size()); + TS_TRACE("BEGIN: size of vector v is %d",v.size()); + + if(v.size() != VEC_SIZE) + { + TS_FAIL("Size not equal to expected value expected:%d " + "actual:%d!", + VEC_SIZE,v.size()); + } + + for(uint32_t i=0;i<VEC_SIZE;i++) + { + if (v[i] != i) + { + TS_FAIL("Vector array index %d is not equal to %d, " + "it is %d", + i,i,v[i]); + } + } + + // Pop a couple off + v.pop_back(); + v.pop_back(); + + if(v.size() != (VEC_SIZE-2)) + { + TS_FAIL("pop_back did not reduce size as expected!"); + } + + // Add some back + v.push_back(100); + v.push_back(99); + + if(v.size() != VEC_SIZE) + { + TS_FAIL("Size not equal to expected value expected:%d " + "actual:%d after pop and push!", + VEC_SIZE,v.size()); + } + + TS_TRACE("END: size of vector v is %d",v.size()); + } + + void test_InitializerListFun() + { + using std::list; + std::list<uint32_t> l = {0,1,2,3,4}; + + uint32_t LIST_SIZE = 5; + if(l.size() != LIST_SIZE) + { + TS_FAIL("Size not equal to expected value expected:%d " + "actual:%d!", + LIST_SIZE,l.size()); + } + + for(uint32_t i=0;i<LIST_SIZE;i++) + { + if (l.front() != i) + { + TS_FAIL("Index %d is not equal to %d, " + "it is %d", + i,i,l.front()); + } + l.pop_front(); + } + + if(l.size() != 0) + { + TS_FAIL("List size not equal to 0 like it should be! %d", + l.size()); + } + + } + }; #endif |