diff --git a/deque_base.h b/deque_base.h index 72fc51d4..16df1572 100644 --- a/deque_base.h +++ b/deque_base.h @@ -33,13 +33,12 @@ SOFTWARE. #ifndef __ETL_DEQUE_BASE__ #define __ETL_DEQUE_BASE__ -#include +#include #include "exception.h" namespace etl { -#ifdef ETL_USE_EXCEPTIONS //*************************************************************************** /// Exception base for deques ///\ingroup deque @@ -63,7 +62,7 @@ namespace etl public: deque_full() - : deque_exception("deque full") + : deque_exception("deque: full") { } }; @@ -77,7 +76,7 @@ namespace etl public: deque_empty() - : deque_exception("deque empty") + : deque_exception("deque: empty") { } }; @@ -91,13 +90,11 @@ namespace etl public: deque_out_of_bounds() - : deque_exception("deque out of bounds") + : deque_exception("deque: out of bounds") { } }; -#endif - //*************************************************************************** /// The base class for all templated deque types. ///\ingroup deque @@ -153,6 +150,15 @@ namespace etl return MAX_SIZE; } + //************************************************************************* + /// Returns the remaining capacity. + ///\return The remaining capacity. + //************************************************************************* + size_t available() const + { + return max_size() - size(); + } + //************************************************************************* /// Clears the deque. //************************************************************************* diff --git a/ideque.h b/ideque.h index e464e153..db98982c 100644 --- a/ideque.h +++ b/ideque.h @@ -30,13 +30,17 @@ SOFTWARE. #define __ETL_IDEQUE__ #define __ETL_IN_IDEQUE_H__ -#include +#include #include #include "type_traits.h" #include "deque_base.h" #include "parameter_type.h" +#ifndef ETL_THROW_EXCEPTIONS +#include "error_handler.h" +#endif + namespace etl { //*************************************************************************** @@ -482,18 +486,22 @@ namespace etl //************************************************************************* /// Assigns 'n' copies of a value to the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is 'n' is too large. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is 'n' is too large. ///\param n The number of copies to assign. ///\param value The value to add.< //************************************************************************* void assign(size_type n, const value_type& value) { -#ifdef ETL_USE_EXCEPTIONS + if (n > MAX_SIZE) { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } + clear(); @@ -506,18 +514,20 @@ namespace etl //************************************************************************* /// Gets a reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. ///\return A reference to the item at the index. //************************************************************************* reference at(size_t index) { -#ifdef ETL_USE_EXCEPTIONS if (index >= current_size) { +#ifdef ETL_THROW_EXCEPTIONS throw deque_out_of_bounds(); - } +#else + error_handler::error((deque_out_of_bounds())); #endif - + } + iterator result(first); result += index; @@ -526,17 +536,19 @@ namespace etl //************************************************************************* /// Gets a const reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. ///\return A const reference to the item at the index. //************************************************************************* const_reference at(size_t index) const { -#ifdef ETL_USE_EXCEPTIONS if (index >= current_size) { +#ifdef ETL_THROW_EXCEPTIONS throw deque_out_of_bounds(); - } +#else + error_handler::error((deque_out_of_bounds())); #endif + } iterator result(first); result += index; @@ -546,7 +558,6 @@ namespace etl //************************************************************************* /// Gets a reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. ///\return A reference to the item at the index. //************************************************************************* reference operator [](size_t index) @@ -559,7 +570,6 @@ namespace etl //************************************************************************* /// Gets a const reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. ///\return A const reference to the item at the index. //************************************************************************* const_reference operator [](size_t index) const @@ -572,69 +582,37 @@ namespace etl //************************************************************************* /// Gets a reference to the item at the front of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is empty. ///\return A reference to the item at the front of the deque. //************************************************************************* reference front() { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw deque_empty(); - } -#endif - return *first; } //************************************************************************* /// Gets a const reference to the item at the front of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is empty. ///\return A const reference to the item at the front of the deque. //************************************************************************* const_reference front() const { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw deque_empty(); - } -#endif - return *first; } //************************************************************************* /// Gets a reference to the item at the back of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is empty. ///\return A reference to the item at the back of the deque. //************************************************************************* reference back() { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw deque_empty(); - } -#endif - return *last; } //************************************************************************* /// Gets a const reference to the item at the back of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is empty. ///\return A const reference to the item at the back of the deque. //************************************************************************* const_reference back() const { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw deque_empty(); - } -#endif - return *last; } @@ -746,7 +724,7 @@ namespace etl //************************************************************************* /// Inserts data into the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full if the deque is full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full if the deque is full. ///\param insert_position>The insert position. ///\param value>The value to insert. //************************************************************************* @@ -796,18 +774,20 @@ namespace etl } } else -#ifdef ETL_USE_EXCEPTIONS { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } return position; } //************************************************************************* /// Inserts 'n' copies of a value into the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full if the deque is full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full if the deque is full. ///\param insert_position The insert position. ///\param n The number of values to insert. ///\param value The value to insert. @@ -868,19 +848,21 @@ namespace etl current_size += n; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } return position; } //************************************************************************* /// Inserts a range into the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_empty if the deque is full. ///\param insert_position>The insert position. ///\param range_begin The beginning of the range to insert. ///\param range_end The end of the range to insert. @@ -957,19 +939,21 @@ namespace etl current_size += n; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } return position; } //************************************************************************* /// erase an item. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the position is out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the position is out of range. ///\param erase_position The position to erase. //************************************************************************* iterator erase(const_iterator erase_position) @@ -1005,19 +989,21 @@ namespace etl --current_size; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_out_of_bounds(); - } +#else + error_handler::error(deque_out_of_bounds()); #endif + } return position; } //************************************************************************* /// erase a range. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the iterators are out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the iterators are out of range. ///\param range_begin The beginning of the range to erase. ///\param range_end The end of the range to erase. //************************************************************************* @@ -1064,19 +1050,21 @@ namespace etl current_size -= length; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_out_of_bounds(); - } +#else + error_handler::error(deque_out_of_bounds()); #endif + } return position; } //************************************************************************* /// Adds an item to the back of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. ///\param item The item to push to the deque. //************************************************************************* void push_back(parameter_t item) @@ -1090,18 +1078,20 @@ namespace etl *last = item; ++current_size; - } -#ifdef ETL_USE_EXCEPTIONS + } else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } } //************************************************************************* /// Adds one to the front of the deque and returns a reference to the new element. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. ///\return A reference to the item to assign to. //************************************************************************* reference push_back() @@ -1115,12 +1105,14 @@ namespace etl ++current_size; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } return *last; } @@ -1143,7 +1135,7 @@ namespace etl //************************************************************************* /// Adds an item to the front of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. ///\param item The item to push to the deque. //************************************************************************* void push_front(parameter_t item) @@ -1158,17 +1150,19 @@ namespace etl *first = item; ++current_size; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } } //************************************************************************* /// Adds one to the front of the deque and returns a reference to the new element. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. ///\return A reference to the item to assign to. //************************************************************************* reference push_front() @@ -1182,12 +1176,14 @@ namespace etl ++current_size; } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_full(); - } +#else + error_handler::error(deque_full()); #endif + } return *first; } @@ -1210,7 +1206,7 @@ namespace etl //************************************************************************* /// Resizes the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is 'new_size' is too large. + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::deque_full is 'new_size' is too large. ///\param new_size The new size of the deque. ///\param value The value to assign if the new size is larger. Default = Default constructed value. //************************************************************************* @@ -1236,12 +1232,15 @@ namespace etl current_size = new_size; } -#ifdef ETL_USE_EXCEPTIONS + else { +#ifdef ETL_THROW_EXCEPTIONS throw deque_out_of_bounds(); - } +#else + error_handler::error(deque_out_of_bounds()); #endif + } } //************************************************************************* diff --git a/ilist.h b/ilist.h index 01b131be..7af4ed13 100644 --- a/ilist.h +++ b/ilist.h @@ -37,7 +37,7 @@ SOFTWARE. #include #include #include -#include +#include #include "nullptr.h" #include "list_base.h" @@ -84,7 +84,7 @@ namespace etl //*********************************************************************** /// Marks the node as free. //*********************************************************************** - inline void mark_as_free() + void mark_as_free() { previous = nullptr; next = nullptr; @@ -93,7 +93,7 @@ namespace etl //*********************************************************************** /// Checks if the node is free. //*********************************************************************** - inline bool is_free() const + bool is_free() const { return next == nullptr; } @@ -101,7 +101,7 @@ namespace etl //*********************************************************************** /// Reverses the previous & next pointers. //*********************************************************************** - inline void reverse() + void reverse() { std::swap(previous, next); } @@ -129,7 +129,7 @@ namespace etl //************************************************************************* /// Downcast a Node* to a Data_Node* //************************************************************************* - inline static Data_Node* data_cast(Node* p_node) + static Data_Node* data_cast(Node* p_node) { return static_cast(p_node); } @@ -137,7 +137,7 @@ namespace etl //************************************************************************* /// Downcast a Node& to a Data_Node& //************************************************************************* - inline static Data_Node& data_cast(Node& node) + static Data_Node& data_cast(Node& node) { return static_cast(node); } @@ -145,7 +145,7 @@ namespace etl //************************************************************************* /// Downcast a const Node* to a const Data_Node* //************************************************************************* - inline static const Data_Node* data_cast(const Node* p_node) + static const Data_Node* data_cast(const Node* p_node) { return static_cast(p_node); } @@ -153,7 +153,7 @@ namespace etl //************************************************************************* /// Downcast a const Node& to a const Data_Node& //************************************************************************* - inline static const Data_Node& data_cast(const Node& node) + static const Data_Node& data_cast(const Node& node) { return static_cast(node); } @@ -169,89 +169,89 @@ namespace etl friend class ilist; - inline iterator() + iterator() : p_node(nullptr) { } - inline iterator(Node& node) + iterator(Node& node) : p_node(&node) { } - inline iterator(const iterator& other) + iterator(const iterator& other) : p_node(other.p_node) { } - inline iterator& operator ++() + iterator& operator ++() { p_node = p_node->next; return *this; } - inline iterator operator ++(int) + iterator operator ++(int) { iterator temp(*this); p_node = p_node->next; return temp; } - inline iterator& operator --() + iterator& operator --() { p_node = p_node->previous; return *this; } - inline iterator operator --(int) + iterator operator --(int) { iterator temp(*this); p_node = p_node->previous; return temp; } - inline iterator operator =(const iterator& other) + iterator operator =(const iterator& other) { p_node = other.p_node; return *this; } - inline reference operator *() + reference operator *() { return ilist::data_cast(p_node)->value; } - inline const_reference operator *() const + const_reference operator *() const { return p_node->value; } - inline pointer operator &() + pointer operator &() { return &(ilist::data_cast(p_node)->value); } - inline const_pointer operator &() const + const_pointer operator &() const { return &(ilist::data_cast(p_node)->value); } - inline pointer operator ->() + pointer operator ->() { return &(ilist::data_cast(p_node)->value); } - inline const_pointer operator ->() const + const_pointer operator ->() const { return &(ilist::data_cast(p_node)->value); } - inline friend bool operator == (const iterator& lhs, const iterator& rhs) + friend bool operator == (const iterator& lhs, const iterator& rhs) { return lhs.p_node == rhs.p_node; } - inline friend bool operator != (const iterator& lhs, const iterator& rhs) + friend bool operator != (const iterator& lhs, const iterator& rhs) { return !(lhs == rhs); } @@ -270,84 +270,84 @@ namespace etl friend class ilist; - inline const_iterator() + const_iterator() : p_node(nullptr) { } - inline const_iterator(Node& node) + const_iterator(Node& node) : p_node(&node) { } - inline const_iterator(const Node& node) + const_iterator(const Node& node) : p_node(&node) { } - inline const_iterator(const typename ilist::iterator& other) + const_iterator(const typename ilist::iterator& other) : p_node(other.p_node) { } - inline const_iterator(const const_iterator& other) + const_iterator(const const_iterator& other) : p_node(other.p_node) { } - inline const_iterator& operator ++() + const_iterator& operator ++() { p_node = p_node->next; return *this; } - inline const_iterator operator ++(int) + const_iterator operator ++(int) { const_iterator temp(*this); p_node = p_node->next; return temp; } - inline const_iterator& operator --() + const_iterator& operator --() { p_node = p_node->previous; return *this; } - inline const_iterator operator --(int) + const_iterator operator --(int) { const_iterator temp(*this); p_node = p_node->previous; return temp; } - inline const_iterator operator =(const const_iterator& other) + const_iterator operator =(const const_iterator& other) { p_node = other.p_node; return *this; } - inline const_reference operator *() const + const_reference operator *() const { return ilist::data_cast(p_node)->value; } - inline const_pointer operator &() const + const_pointer operator &() const { return ilist::data_cast(p_node)->value; } - inline const Data_Node* operator ->() const + const Data_Node* operator ->() const { return p_node; } - inline friend bool operator == (const const_iterator& lhs, const const_iterator& rhs) + friend bool operator == (const const_iterator& lhs, const const_iterator& rhs) { return lhs.p_node == rhs.p_node; } - inline friend bool operator != (const const_iterator& lhs, const const_iterator& rhs) + friend bool operator != (const const_iterator& lhs, const const_iterator& rhs) { return !(lhs == rhs); } @@ -365,7 +365,7 @@ namespace etl //************************************************************************* /// Assignment operator. //************************************************************************* - inline ilist& operator = (const ilist& rhs) + ilist& operator = (const ilist& rhs) { assign(rhs.cbegin(), rhs.cend()); @@ -375,7 +375,7 @@ namespace etl //************************************************************************* /// Gets the beginning of the list. //************************************************************************* - inline iterator begin() + iterator begin() { return iterator(get_head()); } @@ -383,7 +383,7 @@ namespace etl //************************************************************************* /// Gets the beginning of the list. //************************************************************************* - inline const_iterator begin() const + const_iterator begin() const { return const_iterator(get_head()); } @@ -391,7 +391,7 @@ namespace etl //************************************************************************* /// Gets the end of the list. //************************************************************************* - inline iterator end() + iterator end() { return iterator(terminal_node); } @@ -399,7 +399,7 @@ namespace etl //************************************************************************* /// Gets the end of the list. //************************************************************************* - inline const_iterator end() const + const_iterator end() const { return const_iterator(static_cast(terminal_node)); } @@ -407,7 +407,7 @@ namespace etl //************************************************************************* /// Gets the beginning of the list. //************************************************************************* - inline const_iterator cbegin() const + const_iterator cbegin() const { return const_iterator(get_head()); } @@ -415,7 +415,7 @@ namespace etl //************************************************************************* /// Gets the end of the list. //************************************************************************* - inline const_iterator cend() const + const_iterator cend() const { return const_iterator(static_cast(terminal_node)); } @@ -423,7 +423,7 @@ namespace etl //************************************************************************* /// Gets the reverse beginning of the list. //************************************************************************* - inline reverse_iterator rbegin() + reverse_iterator rbegin() { return reverse_iterator(terminal_node); } @@ -431,7 +431,7 @@ namespace etl //************************************************************************* /// Gets the reverse beginning of the list. //************************************************************************* - inline const_reverse_iterator rbegin() const + const_reverse_iterator rbegin() const { return const_reverse_iterator(static_cast(terminal_node)); } @@ -439,7 +439,7 @@ namespace etl //************************************************************************* /// Gets the reverse end of the list. //************************************************************************* - inline reverse_iterator rend() + reverse_iterator rend() { return reverse_iterator(get_head()); } @@ -447,7 +447,7 @@ namespace etl //************************************************************************* /// Gets the reverse beginning of the list. //************************************************************************* - inline const_reverse_iterator crbegin() const + const_reverse_iterator crbegin() const { return const_reverse_iterator(static_cast(terminal_node)); } @@ -455,7 +455,7 @@ namespace etl //************************************************************************* /// Gets the reverse end of the list. //************************************************************************* - inline const_reverse_iterator crend() const + const_reverse_iterator crend() const { return const_reverse_iterator(get_head()); } @@ -463,7 +463,7 @@ namespace etl //************************************************************************* /// Gets a reference to the first element. //************************************************************************* - inline reference front() + reference front() { return data_cast(get_head()).value; } @@ -471,7 +471,7 @@ namespace etl //************************************************************************* /// Gets a const reference to the first element. //************************************************************************* - inline const_reference front() const + const_reference front() const { return data_cast(get_head()).value; } @@ -479,7 +479,7 @@ namespace etl //************************************************************************* /// Gets a reference to the last element. //************************************************************************* - inline reference back() + reference back() { return data_cast(get_tail()).value; } @@ -487,47 +487,50 @@ namespace etl //************************************************************************* /// Gets a reference to the last element. //************************************************************************* - inline const_reference back() const + const_reference back() const { return data_cast(get_tail()).value; } //************************************************************************* /// Assigns a range of values to the list. - /// If ETL_USE_EXCEPTIONS is defined throws etl::list_full if the list does not have enough free space. - /// If ETL_USE_EXCEPTIONS & _DEBUG are defined throws list_iterator if the iterators are reversed. + /// If ETL_THROW_EXCEPTIONS is defined throws etl::list_full if the list does not have enough free space. + /// If ETL_THROW_EXCEPTIONS & _DEBUG are defined throws list_iterator if the iterators are reversed. //************************************************************************* template void assign(TIterator first, TIterator last) { // Reset the links. join(terminal_node, terminal_node); - count = 0; + current_size = 0; // Add all of the elements. while (first != last) { if (!full()) { - Data_Node& data_node = node_pool[count]; + Data_Node& data_node = node_pool[current_size]; data_node.value = *first; join(get_tail(), data_node); join(data_node, terminal_node); } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } + ++first; - ++count; + ++current_size; } - next_free = count; + next_free = current_size; // Clear the remaining elements in the node pool. - for (size_t i = count; i < MAX_SIZE; ++i) + for (size_t i = current_size; i < MAX_SIZE; ++i) { node_pool[i].mark_as_free(); } @@ -540,32 +543,34 @@ namespace etl { // Reset the links. join(terminal_node, terminal_node); - count = 0; + current_size = 0; // Add all of the elements. - while (count < n) + while (current_size < n) { if (!full()) { - Data_Node& data_node = node_pool[count]; + Data_Node& data_node = node_pool[current_size]; data_node.value = value; join(*terminal_node.previous, data_node); join(data_node, terminal_node); } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } - ++count; + ++current_size; } - next_free = count; + next_free = current_size; // Clear the remaining elements in the node pool. - for (size_t i = count; i < MAX_SIZE; ++i) + for (size_t i = current_size; i < MAX_SIZE; ++i) { node_pool[i].mark_as_free(); } @@ -581,12 +586,14 @@ namespace etl Data_Node& data_node = node_pool[next_free]; insert_node(get_head(), data_node); } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } //************************************************************************* @@ -600,12 +607,14 @@ namespace etl data_node.value = value; insert_node(get_head(), data_node); } -#ifdef ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } //************************************************************************* @@ -629,12 +638,14 @@ namespace etl Data_Node& data_node = node_pool[next_free]; insert_node(terminal_node, data_node); } -#if ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } //************************************************************************* @@ -648,12 +659,14 @@ namespace etl data_node.value = value; insert_node(terminal_node, data_node); } -#if ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } //************************************************************************* @@ -681,17 +694,15 @@ namespace etl return iterator(data_node); } -#if ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - return end(); - } #else - { + error_handler::error(list_full()); return end(); - } #endif + } } //************************************************************************* @@ -708,12 +719,14 @@ namespace etl data_node.value = value; insert_node(*position.p_node, data_node); } -#if ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } } @@ -732,12 +745,14 @@ namespace etl data_node.value = *first++; insert_node(*position.p_node, data_node); } -#if ETL_USE_EXCEPTIONS else { +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); - } +#else + error_handler::error(list_full()); #endif + } } } @@ -774,7 +789,7 @@ namespace etl next_free = std::min(next_free, new_free); // One less. - --count; + --current_size; p_next = p_first->next; // Remember the next node. p_first->mark_as_free(); // Free the current node. @@ -799,9 +814,10 @@ namespace etl { if (n > MAX_SIZE) { -#if ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw list_full(); #else + error_handler::error(list_full()); n = MAX_SIZE; #endif } @@ -823,7 +839,7 @@ namespace etl //************************************************************************* /// Clears the list. //************************************************************************* - inline void clear() + void clear() { initialise(); } @@ -873,7 +889,7 @@ namespace etl /// Removes all but the first element from every consecutive group of equal /// elements in the container. //************************************************************************* - inline void unique() + void unique() { unique(std::equal_to()); } @@ -912,7 +928,7 @@ namespace etl /// Sort using in-place merge sort algorithm. /// Uses 'less-than operator as the predicate. //************************************************************************* - inline void sort() + void sort() { sort(std::less()); } @@ -1069,7 +1085,7 @@ namespace etl //************************************************************************* /// Join two nodes. //************************************************************************* - inline void join(Node& left, Node& right) + void join(Node& left, Node& right) { left.next = &right; right.previous = &left; @@ -1078,7 +1094,7 @@ namespace etl //************************************************************************* /// Join two nodes. //************************************************************************* - inline void join(Data_Node& left, Data_Node& right) + void join(Data_Node& left, Data_Node& right) { left.next = &right; right.previous = &left; @@ -1087,7 +1103,7 @@ namespace etl //************************************************************************* /// Is the list a trivial length? //************************************************************************* - inline bool is_trivial_list() const + bool is_trivial_list() const { return (size() < 2); } @@ -1120,7 +1136,7 @@ namespace etl join(node, position); // One more. - ++count; + ++current_size; // Update the position of the next free node in the pool. find_next_free(); @@ -1136,7 +1152,7 @@ namespace etl next_free = std::min(next_free, new_free); // One less. - --count; + --current_size; // Disconnect the node from the list. join(*node.previous, *node.next); @@ -1146,7 +1162,7 @@ namespace etl //************************************************************************* /// Get the head node. //************************************************************************* - inline Node& get_head() + Node& get_head() { return *terminal_node.next; } @@ -1154,7 +1170,7 @@ namespace etl //************************************************************************* /// Get the head node. //************************************************************************* - inline const Node& get_head() const + const Node& get_head() const { return *terminal_node.next; } @@ -1162,7 +1178,7 @@ namespace etl //************************************************************************* /// Get the tail node. //************************************************************************* - inline Node& get_tail() + Node& get_tail() { return *terminal_node.previous; } @@ -1170,7 +1186,7 @@ namespace etl //************************************************************************* /// Get the tail node. //************************************************************************* - inline const Node& get_tail() const + const Node& get_tail() const { return *terminal_node.previous; } @@ -1186,8 +1202,8 @@ namespace etl node_pool[i].mark_as_free(); } - next_free = 0; - count = 0; + next_free = 0; + current_size = 0; join(terminal_node, terminal_node); } }; diff --git a/iqueue.h b/iqueue.h index 826f46de..d06487b8 100644 --- a/iqueue.h +++ b/iqueue.h @@ -30,12 +30,16 @@ SOFTWARE. #define __ETL_IQUEUE__ #define __ETL_IN_IQUEUE_H__ -#include +#include #include "queue_base.h" #include "type_traits.h" #include "parameter_type.h" +#ifndef ETL_THROW_EXCEPTIONS +#include "error_handler.h" +#endif + namespace etl { //*************************************************************************** @@ -69,79 +73,43 @@ namespace etl //************************************************************************* /// Gets a reference to the item at the front of the queue.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. /// \return A reference to the item at the front of the queue. //************************************************************************* reference front() { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw queue_empty(); - } -#endif - return buffer[out]; } //************************************************************************* /// Gets a const reference to the item at the front of the queue.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. /// \return A const reference to the item at the front of the queue. //************************************************************************* const_reference front() const { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw queue_empty(); - } -#endif - return buffer[out]; } //************************************************************************* /// Gets a reference to the item at the back of the queue.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. /// \return A reference to the item at the back of the queue. //************************************************************************* reference back() { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw queue_empty(); - } -#endif - return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; } //************************************************************************* /// Gets a const reference to the item at the back of the queue.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the queue is empty, the return value is undefined. /// \return A const reference to the item at the back of the queue. //************************************************************************* const_reference back() const { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw queue_empty(); - } -#endif - return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; } //************************************************************************* /// Adds an item to the queue. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_full is the queue is already full, + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::queue_full is the queue is already full, /// otherwise does nothing if full. ///\param item The item to push to the queue. //************************************************************************* @@ -153,11 +121,15 @@ namespace etl in = (in == (MAX_SIZE - 1)) ? 0 : in + 1; ++current_size; } -#ifdef ETL_USE_EXCEPTIONS else +#ifdef ETL_THROW_EXCEPTIONS { throw queue_full(); } +#else + { + error_handler::error(forward_list_full()); + } #endif } @@ -165,7 +137,7 @@ namespace etl /// Allows a possibly more efficient 'push' by moving to the next input item /// and returning a reference to it. /// This may eliminate a copy by allowing direct construction in-place.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_full is the queue is already full, + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::queue_full is the queue is already full, /// otherwise does nothing if full. /// \return A reference to the position to 'push' to. //************************************************************************* @@ -178,11 +150,15 @@ namespace etl in = (in == (MAX_SIZE - 1)) ? 0 : in + 1; ++current_size; } -#ifdef ETL_USE_EXCEPTIONS else +#ifdef ETL_THROW_EXCEPTIONS { throw queue_full(); } +#else + { + error_handler::error(forward_list_full()); + } #endif return buffer[next]; diff --git a/istack.h b/istack.h index 9520b293..aebecb4f 100644 --- a/istack.h +++ b/istack.h @@ -30,12 +30,16 @@ SOFTWARE. #define __ETL_ISTACK__ #define __ETL_IN_ISTACK_H__ -#include +#include #include "stack_base.h" #include "type_traits.h" #include "parameter_type.h" +#ifndef ETL_THROW_EXCEPTIONS +#include "error_handler.h" +#endif + namespace etl { //*************************************************************************** @@ -69,25 +73,16 @@ namespace etl //************************************************************************* /// Gets a reference to the item at the top of the stack.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_empty if the stack is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the stack is empty, the return value is undefined. /// \return A reference to the item at the top of the stack. //************************************************************************* reference top() { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw stack_empty(); - } -#endif - return buffer[top_index]; } //************************************************************************* /// Adds an item to the stack. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_full is the stack is already full, + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::stack_full is the stack is already full, /// otherwise does nothing if full. ///\param item The item to push to the stack. //************************************************************************* @@ -98,11 +93,15 @@ namespace etl top_index = current_size++; buffer[top_index] = item; } -#ifdef ETL_USE_EXCEPTIONS else +#ifdef ETL_THROW_EXCEPTIONS { throw stack_full(); } +#else + { + error_handler::error(stack_full()); + } #endif } @@ -110,7 +109,7 @@ namespace etl /// Allows a possibly more efficient 'push' by moving to the next input item /// and returning a reference to it. /// This may eliminate a copy by allowing direct construction in-place.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_full is the stack is already full, + /// If ETL_THROW_EXCEPTIONS is defined, throws an etl::stack_full is the stack is already full, /// otherwise does nothing if full. /// \return A reference to the position to 'push' to. //************************************************************************* @@ -120,11 +119,15 @@ namespace etl { top_index = current_size++; } -#ifdef ETL_USE_EXCEPTIONS else +#ifdef ETL_THROW_EXCEPTIONS { throw stack_full(); } +#else + { + error_handler::error(stack_full()); + } #endif return buffer[top_index]; @@ -132,19 +135,10 @@ namespace etl //************************************************************************* /// Gets a const reference to the item at the top of the stack.
- /// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_empty if the stack is empty.
- /// If ETL_USE_EXCEPTIONS is not defined and the stack is empty, the return value is undefined. /// \return A const reference to the item at the top of the stack. //************************************************************************* const_reference top() const { -#ifdef ETL_USE_EXCEPTIONS - if (empty()) - { - throw stack_empty(); - } -#endif - return buffer[top_index]; } diff --git a/ivector.h b/ivector.h index be9ce379..dc4cad96 100644 --- a/ivector.h +++ b/ivector.h @@ -33,12 +33,16 @@ SOFTWARE. #include #include #include -#include +#include #include "vector_base.h" #include "type_traits.h" #include "parameter_type.h" +#ifndef ETL_THROW_EXCEPTIONS +#include "error_handler.h" +#endif + namespace etl { //*************************************************************************** @@ -198,19 +202,22 @@ namespace etl //********************************************************************* /// Resizes the vector. - /// If ETL_USE_EXCEPTIONS is defined and the new size is larger than the + /// If ETL_THROW_EXCEPTIONS is defined and the new size is larger than the /// maximum then a vector_full is thrown. ///\param newSize The new size. ///\param value The value to fill new elements with. Default = default contructed value. //********************************************************************* void resize(size_t newSize, T value = T()) { -#ifdef ETL_USE_EXCEPTIONS + if (newSize > MAX_SIZE) { +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); - } +#else + error_handler::error(vector_full()); #endif + } if (newSize > current_size) { @@ -242,36 +249,42 @@ namespace etl //********************************************************************* /// Returns a reference to the value at index 'i' - /// If ETL_USE_EXCEPTIONS is defined, throws a std::range_error if the index is out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws a std::range_error if the index is out of range. ///\param i The index. ///\return A reference to the value at index 'i' //********************************************************************* reference at(size_t i) { -#ifdef ETL_USE_EXCEPTIONS + if (i >= current_size) { +#ifdef ETL_THROW_EXCEPTIONS throw vector_out_of_bounds(); - } +#else + error_handler::error(vector_out_of_bounds()); #endif + } return p_buffer[i]; } //********************************************************************* /// Returns a const reference to the value at index 'i' - /// If ETL_USE_EXCEPTIONS is defined, throws a std::range_error if the index is out of range. + /// If ETL_THROW_EXCEPTIONS is defined, throws a std::range_error if the index is out of range. ///\param i The index. ///\return A const reference to the value at index 'i' //********************************************************************* const_reference at(size_t i) const { -#ifdef ETL_USE_EXCEPTIONS + if (i >= current_size) { +#ifdef ETL_THROW_EXCEPTIONS throw vector_out_of_bounds(); - } +#else + error_handler::error(vector_out_of_bounds()); #endif + } return p_buffer[i]; } @@ -332,8 +345,8 @@ namespace etl //********************************************************************* /// Assigns values to the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_iterator if the iterators are reversed. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_iterator if the iterators are reversed. ///\param first The iterator to the first element. ///\param last The iterator to the last element + 1. //********************************************************************* @@ -344,14 +357,18 @@ namespace etl if (count < 0) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_iterator(); +#else + error_handler::error((vector_iterator)); #endif } else if (static_cast(count) > MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else @@ -364,7 +381,7 @@ namespace etl //********************************************************************* /// Assigns values to the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. ///\param n The number of elements to add. ///\param value The value to insert for each element. //********************************************************************* @@ -372,8 +389,10 @@ namespace etl { if (n > MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else @@ -385,15 +404,17 @@ namespace etl //********************************************************************* /// Inserts a value at the end of the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector is already full. ///\param value The value to add. //********************************************************************* void push_back(parameter_t value) { if (current_size == MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else @@ -404,7 +425,7 @@ namespace etl //********************************************************************* /// Inserts a value to the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector is already full. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector is already full. ///\param position The position to insert at. ///\param value The value to insert. //********************************************************************* @@ -419,8 +440,10 @@ namespace etl { if ((current_size + 1) > MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else @@ -436,7 +459,7 @@ namespace etl //********************************************************************* /// Inserts 'n' values to the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. ///\param position The position to insert at. ///\param n The number of elements to add. ///\param value The value to insert. @@ -445,8 +468,10 @@ namespace etl { if ((current_size + n) > MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else @@ -459,7 +484,7 @@ namespace etl //********************************************************************* /// Inserts a range of values to the vector. - /// If ETL_USE_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. + /// If ETL_THROW_EXCEPTIONS is defined, throws vector_full if the vector does not have enough free space. ///\param position The position to insert at. ///\param first The first element to add. ///\param last The last + 1 element to add. @@ -471,8 +496,10 @@ namespace etl if ((current_size + count) > MAX_SIZE) { -#ifdef ETL_USE_EXCEPTIONS +#ifdef ETL_THROW_EXCEPTIONS throw vector_full(); +#else + error_handler::error(vector_full()); #endif } else diff --git a/list.h b/list.h index ea5e7fa2..aa1a6a7b 100644 --- a/list.h +++ b/list.h @@ -29,7 +29,7 @@ SOFTWARE. #ifndef __ETL_LIST__ #define __ETL_LIST__ -#include +#include #include "ilist.h" #include "container.h" @@ -66,7 +66,7 @@ namespace etl //************************************************************************* /// Default constructor. //************************************************************************* - inline list() + list() : ilist(&node_pool[0], MAX_SIZE) { } @@ -74,7 +74,7 @@ namespace etl //************************************************************************* /// Construct from size and value. //************************************************************************* - inline explicit list(size_t initialSize, typename ilist::parameter_t value = T()) + explicit list(size_t initialSize, typename ilist::parameter_t value = T()) : ilist(&node_pool[0], MAX_SIZE) { ilist::assign(initialSize, value); @@ -175,7 +175,7 @@ namespace etl // Swap the data. std::swap_ranges(etl::begin(node_pool), etl::end(node_pool), etl::begin(other.node_pool)); std::swap(this->next_free, other.next_free); - std::swap(this->count, other.count); + std::swap(this->current_size, other.current_size); } private: diff --git a/list_base.h b/list_base.h index 17839829..21060e8c 100644 --- a/list_base.h +++ b/list_base.h @@ -33,12 +33,11 @@ SOFTWARE. #ifndef __ETL_LIST_BASE__ #define __ETL_LIST_BASE__ -#include +#include #include "exception.h" namespace etl { -#ifdef ETL_USE_EXCEPTIONS //*************************************************************************** /// Exception for the list. ///\ingroup list @@ -62,7 +61,7 @@ namespace etl public: list_full() - : list_exception("List full") + : list_exception("list: full") { } }; @@ -76,11 +75,10 @@ namespace etl public: list_iterator() - : list_exception("Iterator problem") + : list_exception("list: iterator problem") { } }; -#endif //*************************************************************************** /// The base class for all lists. @@ -97,7 +95,7 @@ namespace etl //************************************************************************* size_type size() const { - return count; + return current_size; } //************************************************************************* @@ -113,7 +111,7 @@ namespace etl //************************************************************************* bool empty() const { - return count == 0; + return current_size == 0; } //************************************************************************* @@ -121,7 +119,16 @@ namespace etl //************************************************************************* bool full() const { - return count == MAX_SIZE; + return current_size == MAX_SIZE; + } + + //************************************************************************* + /// Returns the remaining capacity. + ///\return The remaining capacity. + //************************************************************************* + size_t available() const + { + return max_size() - size(); } protected: @@ -131,14 +138,14 @@ namespace etl //************************************************************************* list_base(size_type max_size) : next_free(0), - count(0), + current_size(0), MAX_SIZE(max_size) { } size_type next_free; ///< The index of the next free node. - size_type count; ///< The number of the used nodes. + size_type current_size; ///< The number of the used nodes. const size_type MAX_SIZE; ///< The maximum size of the list. }; } diff --git a/queue.h b/queue.h index 29fcde02..24098bd9 100644 --- a/queue.h +++ b/queue.h @@ -29,7 +29,7 @@ SOFTWARE. #ifndef __ETL_QUEUE__ #define __ETL_QUEUE__ -#include +#include #include "iqueue.h" #include "container.h" diff --git a/queue_base.h b/queue_base.h index 923b20b6..243e97f6 100644 --- a/queue_base.h +++ b/queue_base.h @@ -33,13 +33,12 @@ SOFTWARE. #ifndef __ETL_QUEUE_BASE__ #define __ETL_QUEUE_BASE__ -#include +#include #include "exception.h" namespace etl { -#ifdef ETL_USE_EXCEPTIONS //*************************************************************************** /// The base class for queue exceptions. ///\ingroup queue @@ -63,26 +62,11 @@ namespace etl public: queue_full() - : queue_exception("queue full") + : queue_exception("queue: full") { } }; - //*************************************************************************** - /// The exception thrown when the queue is empty. - ///\ingroup queue - //*************************************************************************** - class queue_empty : public queue_exception - { - public: - - queue_empty() - : queue_exception("queue empty") - { - } - }; -#endif - //*************************************************************************** /// The base class for all queues. ///\ingroup queue @@ -135,6 +119,15 @@ namespace etl return current_size == MAX_SIZE; } + //************************************************************************* + /// Returns the remaining capacity. + ///\return The remaining capacity. + //************************************************************************* + size_t available() const + { + return max_size() - size(); + } + //************************************************************************* /// Clears the queue to the empty state. //************************************************************************* diff --git a/stack.h b/stack.h index 53a3a1d4..61f99f96 100644 --- a/stack.h +++ b/stack.h @@ -29,7 +29,7 @@ SOFTWARE. #ifndef __ETL_STACK__ #define __ETL_STACK__ -#include +#include #include #include "istack.h" diff --git a/stack_base.h b/stack_base.h index 3673180e..306db1eb 100644 --- a/stack_base.h +++ b/stack_base.h @@ -33,13 +33,12 @@ SOFTWARE. #ifndef __ETL_STACK_BASE__ #define __ETL_STACK_BASE__ -#include +#include #include "exception.h" namespace etl { -#ifdef ETL_USE_EXCEPTIONS //*************************************************************************** ///\ingroup stack /// The base class for stack exceptions. @@ -63,26 +62,11 @@ namespace etl public: stack_full() - : stack_exception("stack full") + : stack_exception("stack: full") { } }; - //*************************************************************************** - ///\ingroup stack - /// The exception thrown when the stack is empty. - //*************************************************************************** - class stack_empty : public stack_exception - { - public: - - stack_empty() - : stack_exception("stack empty") - { - } - }; -#endif - //*************************************************************************** ///\ingroup stack /// A fixed capacity stack written in the STL style. @@ -136,6 +120,15 @@ namespace etl return MAX_SIZE; } + //************************************************************************* + /// Returns the remaining capacity. + ///\return The remaining capacity. + //************************************************************************* + size_t available() const + { + return max_size() - size(); + } + //************************************************************************* /// Clears the stack to the empty state. //*************************************************************************