Added error_handler functionality

This commit is contained in:
jwellbelove 2014-12-08 20:32:42 +00:00
parent 4341d7169c
commit e74c826d1f
12 changed files with 339 additions and 328 deletions

View File

@ -33,13 +33,12 @@ SOFTWARE.
#ifndef __ETL_DEQUE_BASE__ #ifndef __ETL_DEQUE_BASE__
#define __ETL_DEQUE_BASE__ #define __ETL_DEQUE_BASE__
#include <cstddef> #include <stddef.h>
#include "exception.h" #include "exception.h"
namespace etl namespace etl
{ {
#ifdef ETL_USE_EXCEPTIONS
//*************************************************************************** //***************************************************************************
/// Exception base for deques /// Exception base for deques
///\ingroup deque ///\ingroup deque
@ -63,7 +62,7 @@ namespace etl
public: public:
deque_full() deque_full()
: deque_exception("deque full") : deque_exception("deque: full")
{ {
} }
}; };
@ -77,7 +76,7 @@ namespace etl
public: public:
deque_empty() deque_empty()
: deque_exception("deque empty") : deque_exception("deque: empty")
{ {
} }
}; };
@ -91,13 +90,11 @@ namespace etl
public: public:
deque_out_of_bounds() 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. /// The base class for all templated deque types.
///\ingroup deque ///\ingroup deque
@ -153,6 +150,15 @@ namespace etl
return MAX_SIZE; return MAX_SIZE;
} }
//*************************************************************************
/// Returns the remaining capacity.
///\return The remaining capacity.
//*************************************************************************
size_t available() const
{
return max_size() - size();
}
//************************************************************************* //*************************************************************************
/// Clears the deque. /// Clears the deque.
//************************************************************************* //*************************************************************************

151
ideque.h
View File

@ -30,13 +30,17 @@ SOFTWARE.
#define __ETL_IDEQUE__ #define __ETL_IDEQUE__
#define __ETL_IN_IDEQUE_H__ #define __ETL_IN_IDEQUE_H__
#include <cstddef> #include <stddef.h>
#include <iterator> #include <iterator>
#include "type_traits.h" #include "type_traits.h"
#include "deque_base.h" #include "deque_base.h"
#include "parameter_type.h" #include "parameter_type.h"
#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif
namespace etl namespace etl
{ {
//*************************************************************************** //***************************************************************************
@ -482,18 +486,22 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Assigns 'n' copies of a value to the deque. /// 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 n The number of copies to assign.
///\param value The value to add.< ///\param value The value to add.<
//************************************************************************* //*************************************************************************
void assign(size_type n, const value_type& value) void assign(size_type n, const value_type& value)
{ {
#ifdef ETL_USE_EXCEPTIONS
if (n > MAX_SIZE) if (n > MAX_SIZE)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
clear(); clear();
@ -506,18 +514,20 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the index. /// 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. ///\return A reference to the item at the index.
//************************************************************************* //*************************************************************************
reference at(size_t index) reference at(size_t index)
{ {
#ifdef ETL_USE_EXCEPTIONS
if (index >= current_size) if (index >= current_size)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_out_of_bounds(); throw deque_out_of_bounds();
} #else
error_handler::error((deque_out_of_bounds()));
#endif #endif
}
iterator result(first); iterator result(first);
result += index; result += index;
@ -526,17 +536,19 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the index. /// 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. ///\return A const reference to the item at the index.
//************************************************************************* //*************************************************************************
const_reference at(size_t index) const const_reference at(size_t index) const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (index >= current_size) if (index >= current_size)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_out_of_bounds(); throw deque_out_of_bounds();
} #else
error_handler::error((deque_out_of_bounds()));
#endif #endif
}
iterator result(first); iterator result(first);
result += index; result += index;
@ -546,7 +558,6 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the index. /// 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. ///\return A reference to the item at the index.
//************************************************************************* //*************************************************************************
reference operator [](size_t index) reference operator [](size_t index)
@ -559,7 +570,6 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the index. /// 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. ///\return A const reference to the item at the index.
//************************************************************************* //*************************************************************************
const_reference operator [](size_t index) const 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. /// 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. ///\return A reference to the item at the front of the deque.
//************************************************************************* //*************************************************************************
reference front() reference front()
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw deque_empty();
}
#endif
return *first; return *first;
} }
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the front of the deque. /// 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. ///\return A const reference to the item at the front of the deque.
//************************************************************************* //*************************************************************************
const_reference front() const const_reference front() const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw deque_empty();
}
#endif
return *first; return *first;
} }
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the back of the deque. /// 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. ///\return A reference to the item at the back of the deque.
//************************************************************************* //*************************************************************************
reference back() reference back()
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw deque_empty();
}
#endif
return *last; return *last;
} }
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the back of the deque. /// 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. ///\return A const reference to the item at the back of the deque.
//************************************************************************* //*************************************************************************
const_reference back() const const_reference back() const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw deque_empty();
}
#endif
return *last; return *last;
} }
@ -746,7 +724,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Inserts data into the deque. /// 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 insert_position>The insert position.
///\param value>The value to insert. ///\param value>The value to insert.
//************************************************************************* //*************************************************************************
@ -796,18 +774,20 @@ namespace etl
} }
} }
else else
#ifdef ETL_USE_EXCEPTIONS
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
return position; return position;
} }
//************************************************************************* //*************************************************************************
/// Inserts 'n' copies of a value into the deque. /// 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 insert_position The insert position.
///\param n The number of values to insert. ///\param n The number of values to insert.
///\param value The value to insert. ///\param value The value to insert.
@ -868,19 +848,21 @@ namespace etl
current_size += n; current_size += n;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
return position; return position;
} }
//************************************************************************* //*************************************************************************
/// Inserts a range into the deque. /// 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 insert_position>The insert position.
///\param range_begin The beginning of the range to insert. ///\param range_begin The beginning of the range to insert.
///\param range_end The end of the range to insert. ///\param range_end The end of the range to insert.
@ -957,19 +939,21 @@ namespace etl
current_size += n; current_size += n;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
return position; return position;
} }
//************************************************************************* //*************************************************************************
/// erase an item. /// 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. ///\param erase_position The position to erase.
//************************************************************************* //*************************************************************************
iterator erase(const_iterator erase_position) iterator erase(const_iterator erase_position)
@ -1005,19 +989,21 @@ namespace etl
--current_size; --current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_out_of_bounds(); throw deque_out_of_bounds();
} #else
error_handler::error(deque_out_of_bounds());
#endif #endif
}
return position; return position;
} }
//************************************************************************* //*************************************************************************
/// erase a range. /// 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_begin The beginning of the range to erase.
///\param range_end The end of the range to erase. ///\param range_end The end of the range to erase.
//************************************************************************* //*************************************************************************
@ -1064,19 +1050,21 @@ namespace etl
current_size -= length; current_size -= length;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_out_of_bounds(); throw deque_out_of_bounds();
} #else
error_handler::error(deque_out_of_bounds());
#endif #endif
}
return position; return position;
} }
//************************************************************************* //*************************************************************************
/// Adds an item to the back of the deque. /// 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. ///\param item The item to push to the deque.
//************************************************************************* //*************************************************************************
void push_back(parameter_t item) void push_back(parameter_t item)
@ -1090,18 +1078,20 @@ namespace etl
*last = item; *last = item;
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
/// Adds one to the front of the deque and returns a reference to the new element. /// 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. ///\return A reference to the item to assign to.
//************************************************************************* //*************************************************************************
reference push_back() reference push_back()
@ -1115,12 +1105,14 @@ namespace etl
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
return *last; return *last;
} }
@ -1143,7 +1135,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Adds an item to the front of the deque. /// 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. ///\param item The item to push to the deque.
//************************************************************************* //*************************************************************************
void push_front(parameter_t item) void push_front(parameter_t item)
@ -1158,17 +1150,19 @@ namespace etl
*first = item; *first = item;
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
/// Adds one to the front of the deque and returns a reference to the new element. /// 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. ///\return A reference to the item to assign to.
//************************************************************************* //*************************************************************************
reference push_front() reference push_front()
@ -1182,12 +1176,14 @@ namespace etl
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_full(); throw deque_full();
} #else
error_handler::error(deque_full());
#endif #endif
}
return *first; return *first;
} }
@ -1210,7 +1206,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Resizes the deque. /// 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 new_size The new size of the deque.
///\param value The value to assign if the new size is larger. Default = Default constructed value. ///\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; current_size = new_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw deque_out_of_bounds(); throw deque_out_of_bounds();
} #else
error_handler::error(deque_out_of_bounds());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************

226
ilist.h
View File

@ -37,7 +37,7 @@ SOFTWARE.
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <cstddef> #include <stddef.h>
#include "nullptr.h" #include "nullptr.h"
#include "list_base.h" #include "list_base.h"
@ -84,7 +84,7 @@ namespace etl
//*********************************************************************** //***********************************************************************
/// Marks the node as free. /// Marks the node as free.
//*********************************************************************** //***********************************************************************
inline void mark_as_free() void mark_as_free()
{ {
previous = nullptr; previous = nullptr;
next = nullptr; next = nullptr;
@ -93,7 +93,7 @@ namespace etl
//*********************************************************************** //***********************************************************************
/// Checks if the node is free. /// Checks if the node is free.
//*********************************************************************** //***********************************************************************
inline bool is_free() const bool is_free() const
{ {
return next == nullptr; return next == nullptr;
} }
@ -101,7 +101,7 @@ namespace etl
//*********************************************************************** //***********************************************************************
/// Reverses the previous & next pointers. /// Reverses the previous & next pointers.
//*********************************************************************** //***********************************************************************
inline void reverse() void reverse()
{ {
std::swap(previous, next); std::swap(previous, next);
} }
@ -129,7 +129,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Downcast a Node* to a Data_Node* /// 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<Data_Node*>(p_node); return static_cast<Data_Node*>(p_node);
} }
@ -137,7 +137,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Downcast a Node& to a Data_Node& /// 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<Data_Node&>(node); return static_cast<Data_Node&>(node);
} }
@ -145,7 +145,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Downcast a const Node* to a const Data_Node* /// 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<const Data_Node*>(p_node); return static_cast<const Data_Node*>(p_node);
} }
@ -153,7 +153,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Downcast a const Node& to a const Data_Node& /// 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<const Data_Node&>(node); return static_cast<const Data_Node&>(node);
} }
@ -169,89 +169,89 @@ namespace etl
friend class ilist; friend class ilist;
inline iterator() iterator()
: p_node(nullptr) : p_node(nullptr)
{ {
} }
inline iterator(Node& node) iterator(Node& node)
: p_node(&node) : p_node(&node)
{ {
} }
inline iterator(const iterator& other) iterator(const iterator& other)
: p_node(other.p_node) : p_node(other.p_node)
{ {
} }
inline iterator& operator ++() iterator& operator ++()
{ {
p_node = p_node->next; p_node = p_node->next;
return *this; return *this;
} }
inline iterator operator ++(int) iterator operator ++(int)
{ {
iterator temp(*this); iterator temp(*this);
p_node = p_node->next; p_node = p_node->next;
return temp; return temp;
} }
inline iterator& operator --() iterator& operator --()
{ {
p_node = p_node->previous; p_node = p_node->previous;
return *this; return *this;
} }
inline iterator operator --(int) iterator operator --(int)
{ {
iterator temp(*this); iterator temp(*this);
p_node = p_node->previous; p_node = p_node->previous;
return temp; return temp;
} }
inline iterator operator =(const iterator& other) iterator operator =(const iterator& other)
{ {
p_node = other.p_node; p_node = other.p_node;
return *this; return *this;
} }
inline reference operator *() reference operator *()
{ {
return ilist::data_cast(p_node)->value; return ilist::data_cast(p_node)->value;
} }
inline const_reference operator *() const const_reference operator *() const
{ {
return p_node->value; return p_node->value;
} }
inline pointer operator &() pointer operator &()
{ {
return &(ilist::data_cast(p_node)->value); return &(ilist::data_cast(p_node)->value);
} }
inline const_pointer operator &() const const_pointer operator &() const
{ {
return &(ilist::data_cast(p_node)->value); return &(ilist::data_cast(p_node)->value);
} }
inline pointer operator ->() pointer operator ->()
{ {
return &(ilist::data_cast(p_node)->value); return &(ilist::data_cast(p_node)->value);
} }
inline const_pointer operator ->() const const_pointer operator ->() const
{ {
return &(ilist::data_cast(p_node)->value); 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; 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); return !(lhs == rhs);
} }
@ -270,84 +270,84 @@ namespace etl
friend class ilist; friend class ilist;
inline const_iterator() const_iterator()
: p_node(nullptr) : p_node(nullptr)
{ {
} }
inline const_iterator(Node& node) const_iterator(Node& node)
: p_node(&node) : p_node(&node)
{ {
} }
inline const_iterator(const Node& node) const_iterator(const Node& node)
: p_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) : p_node(other.p_node)
{ {
} }
inline const_iterator(const const_iterator& other) const_iterator(const const_iterator& other)
: p_node(other.p_node) : p_node(other.p_node)
{ {
} }
inline const_iterator& operator ++() const_iterator& operator ++()
{ {
p_node = p_node->next; p_node = p_node->next;
return *this; return *this;
} }
inline const_iterator operator ++(int) const_iterator operator ++(int)
{ {
const_iterator temp(*this); const_iterator temp(*this);
p_node = p_node->next; p_node = p_node->next;
return temp; return temp;
} }
inline const_iterator& operator --() const_iterator& operator --()
{ {
p_node = p_node->previous; p_node = p_node->previous;
return *this; return *this;
} }
inline const_iterator operator --(int) const_iterator operator --(int)
{ {
const_iterator temp(*this); const_iterator temp(*this);
p_node = p_node->previous; p_node = p_node->previous;
return temp; return temp;
} }
inline const_iterator operator =(const const_iterator& other) const_iterator operator =(const const_iterator& other)
{ {
p_node = other.p_node; p_node = other.p_node;
return *this; return *this;
} }
inline const_reference operator *() const const_reference operator *() const
{ {
return ilist::data_cast(p_node)->value; return ilist::data_cast(p_node)->value;
} }
inline const_pointer operator &() const const_pointer operator &() const
{ {
return ilist::data_cast(p_node)->value; return ilist::data_cast(p_node)->value;
} }
inline const Data_Node* operator ->() const const Data_Node* operator ->() const
{ {
return p_node; 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; 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); return !(lhs == rhs);
} }
@ -365,7 +365,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Assignment operator. /// Assignment operator.
//************************************************************************* //*************************************************************************
inline ilist& operator = (const ilist& rhs) ilist& operator = (const ilist& rhs)
{ {
assign(rhs.cbegin(), rhs.cend()); assign(rhs.cbegin(), rhs.cend());
@ -375,7 +375,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the beginning of the list. /// Gets the beginning of the list.
//************************************************************************* //*************************************************************************
inline iterator begin() iterator begin()
{ {
return iterator(get_head()); return iterator(get_head());
} }
@ -383,7 +383,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the beginning of the list. /// Gets the beginning of the list.
//************************************************************************* //*************************************************************************
inline const_iterator begin() const const_iterator begin() const
{ {
return const_iterator(get_head()); return const_iterator(get_head());
} }
@ -391,7 +391,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the end of the list. /// Gets the end of the list.
//************************************************************************* //*************************************************************************
inline iterator end() iterator end()
{ {
return iterator(terminal_node); return iterator(terminal_node);
} }
@ -399,7 +399,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the end of the list. /// Gets the end of the list.
//************************************************************************* //*************************************************************************
inline const_iterator end() const const_iterator end() const
{ {
return const_iterator(static_cast<const Data_Node&>(terminal_node)); return const_iterator(static_cast<const Data_Node&>(terminal_node));
} }
@ -407,7 +407,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the beginning of the list. /// Gets the beginning of the list.
//************************************************************************* //*************************************************************************
inline const_iterator cbegin() const const_iterator cbegin() const
{ {
return const_iterator(get_head()); return const_iterator(get_head());
} }
@ -415,7 +415,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the end of the list. /// Gets the end of the list.
//************************************************************************* //*************************************************************************
inline const_iterator cend() const const_iterator cend() const
{ {
return const_iterator(static_cast<const Data_Node&>(terminal_node)); return const_iterator(static_cast<const Data_Node&>(terminal_node));
} }
@ -423,7 +423,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the reverse beginning of the list. /// Gets the reverse beginning of the list.
//************************************************************************* //*************************************************************************
inline reverse_iterator rbegin() reverse_iterator rbegin()
{ {
return reverse_iterator(terminal_node); return reverse_iterator(terminal_node);
} }
@ -431,7 +431,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the reverse beginning of the list. /// Gets the reverse beginning of the list.
//************************************************************************* //*************************************************************************
inline const_reverse_iterator rbegin() const const_reverse_iterator rbegin() const
{ {
return const_reverse_iterator(static_cast<const Data_Node&>(terminal_node)); return const_reverse_iterator(static_cast<const Data_Node&>(terminal_node));
} }
@ -439,7 +439,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the reverse end of the list. /// Gets the reverse end of the list.
//************************************************************************* //*************************************************************************
inline reverse_iterator rend() reverse_iterator rend()
{ {
return reverse_iterator(get_head()); return reverse_iterator(get_head());
} }
@ -447,7 +447,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the reverse beginning of the list. /// Gets the reverse beginning of the list.
//************************************************************************* //*************************************************************************
inline const_reverse_iterator crbegin() const const_reverse_iterator crbegin() const
{ {
return const_reverse_iterator(static_cast<const Data_Node&>(terminal_node)); return const_reverse_iterator(static_cast<const Data_Node&>(terminal_node));
} }
@ -455,7 +455,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets the reverse end of the list. /// Gets the reverse end of the list.
//************************************************************************* //*************************************************************************
inline const_reverse_iterator crend() const const_reverse_iterator crend() const
{ {
return const_reverse_iterator(get_head()); return const_reverse_iterator(get_head());
} }
@ -463,7 +463,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the first element. /// Gets a reference to the first element.
//************************************************************************* //*************************************************************************
inline reference front() reference front()
{ {
return data_cast(get_head()).value; return data_cast(get_head()).value;
} }
@ -471,7 +471,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a const reference to the first element. /// Gets a const reference to the first element.
//************************************************************************* //*************************************************************************
inline const_reference front() const const_reference front() const
{ {
return data_cast(get_head()).value; return data_cast(get_head()).value;
} }
@ -479,7 +479,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the last element. /// Gets a reference to the last element.
//************************************************************************* //*************************************************************************
inline reference back() reference back()
{ {
return data_cast(get_tail()).value; return data_cast(get_tail()).value;
} }
@ -487,47 +487,50 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the last element. /// Gets a reference to the last element.
//************************************************************************* //*************************************************************************
inline const_reference back() const const_reference back() const
{ {
return data_cast(get_tail()).value; return data_cast(get_tail()).value;
} }
//************************************************************************* //*************************************************************************
/// Assigns a range of values to the list. /// 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_THROW_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 & _DEBUG are defined throws list_iterator if the iterators are reversed.
//************************************************************************* //*************************************************************************
template <typename TIterator> template <typename TIterator>
void assign(TIterator first, TIterator last) void assign(TIterator first, TIterator last)
{ {
// Reset the links. // Reset the links.
join(terminal_node, terminal_node); join(terminal_node, terminal_node);
count = 0; current_size = 0;
// Add all of the elements. // Add all of the elements.
while (first != last) while (first != last)
{ {
if (!full()) if (!full())
{ {
Data_Node& data_node = node_pool[count]; Data_Node& data_node = node_pool[current_size];
data_node.value = *first; data_node.value = *first;
join(get_tail(), data_node); join(get_tail(), data_node);
join(data_node, terminal_node); join(data_node, terminal_node);
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
++first; ++first;
++count; ++current_size;
} }
next_free = count; next_free = current_size;
// Clear the remaining elements in the node pool. // 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(); node_pool[i].mark_as_free();
} }
@ -540,32 +543,34 @@ namespace etl
{ {
// Reset the links. // Reset the links.
join(terminal_node, terminal_node); join(terminal_node, terminal_node);
count = 0; current_size = 0;
// Add all of the elements. // Add all of the elements.
while (count < n) while (current_size < n)
{ {
if (!full()) if (!full())
{ {
Data_Node& data_node = node_pool[count]; Data_Node& data_node = node_pool[current_size];
data_node.value = value; data_node.value = value;
join(*terminal_node.previous, data_node); join(*terminal_node.previous, data_node);
join(data_node, terminal_node); join(data_node, terminal_node);
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
++count; ++current_size;
} }
next_free = count; next_free = current_size;
// Clear the remaining elements in the node pool. // 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(); node_pool[i].mark_as_free();
} }
@ -581,12 +586,14 @@ namespace etl
Data_Node& data_node = node_pool[next_free]; Data_Node& data_node = node_pool[next_free];
insert_node(get_head(), data_node); insert_node(get_head(), data_node);
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
@ -600,12 +607,14 @@ namespace etl
data_node.value = value; data_node.value = value;
insert_node(get_head(), data_node); insert_node(get_head(), data_node);
} }
#ifdef ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
@ -629,12 +638,14 @@ namespace etl
Data_Node& data_node = node_pool[next_free]; Data_Node& data_node = node_pool[next_free];
insert_node(terminal_node, data_node); insert_node(terminal_node, data_node);
} }
#if ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
@ -648,12 +659,14 @@ namespace etl
data_node.value = value; data_node.value = value;
insert_node(terminal_node, data_node); insert_node(terminal_node, data_node);
} }
#if ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
@ -681,17 +694,15 @@ namespace etl
return iterator(data_node); return iterator(data_node);
} }
#if ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
return end();
}
#else #else
{ error_handler::error(list_full());
return end(); return end();
}
#endif #endif
}
} }
//************************************************************************* //*************************************************************************
@ -708,12 +719,14 @@ namespace etl
data_node.value = value; data_node.value = value;
insert_node(*position.p_node, data_node); insert_node(*position.p_node, data_node);
} }
#if ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
} }
@ -732,12 +745,14 @@ namespace etl
data_node.value = *first++; data_node.value = *first++;
insert_node(*position.p_node, data_node); insert_node(*position.p_node, data_node);
} }
#if ETL_USE_EXCEPTIONS
else else
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
} #else
error_handler::error(list_full());
#endif #endif
}
} }
} }
@ -774,7 +789,7 @@ namespace etl
next_free = std::min(next_free, new_free); next_free = std::min(next_free, new_free);
// One less. // One less.
--count; --current_size;
p_next = p_first->next; // Remember the next node. p_next = p_first->next; // Remember the next node.
p_first->mark_as_free(); // Free the current node. p_first->mark_as_free(); // Free the current node.
@ -799,9 +814,10 @@ namespace etl
{ {
if (n > MAX_SIZE) if (n > MAX_SIZE)
{ {
#if ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw list_full(); throw list_full();
#else #else
error_handler::error(list_full());
n = MAX_SIZE; n = MAX_SIZE;
#endif #endif
} }
@ -823,7 +839,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Clears the list. /// Clears the list.
//************************************************************************* //*************************************************************************
inline void clear() void clear()
{ {
initialise(); initialise();
} }
@ -873,7 +889,7 @@ namespace etl
/// Removes all but the first element from every consecutive group of equal /// Removes all but the first element from every consecutive group of equal
/// elements in the container. /// elements in the container.
//************************************************************************* //*************************************************************************
inline void unique() void unique()
{ {
unique(std::equal_to<T>()); unique(std::equal_to<T>());
} }
@ -912,7 +928,7 @@ namespace etl
/// Sort using in-place merge sort algorithm. /// Sort using in-place merge sort algorithm.
/// Uses 'less-than operator as the predicate. /// Uses 'less-than operator as the predicate.
//************************************************************************* //*************************************************************************
inline void sort() void sort()
{ {
sort(std::less<T>()); sort(std::less<T>());
} }
@ -1069,7 +1085,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Join two nodes. /// Join two nodes.
//************************************************************************* //*************************************************************************
inline void join(Node& left, Node& right) void join(Node& left, Node& right)
{ {
left.next = &right; left.next = &right;
right.previous = &left; right.previous = &left;
@ -1078,7 +1094,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Join two nodes. /// Join two nodes.
//************************************************************************* //*************************************************************************
inline void join(Data_Node& left, Data_Node& right) void join(Data_Node& left, Data_Node& right)
{ {
left.next = &right; left.next = &right;
right.previous = &left; right.previous = &left;
@ -1087,7 +1103,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Is the list a trivial length? /// Is the list a trivial length?
//************************************************************************* //*************************************************************************
inline bool is_trivial_list() const bool is_trivial_list() const
{ {
return (size() < 2); return (size() < 2);
} }
@ -1120,7 +1136,7 @@ namespace etl
join(node, position); join(node, position);
// One more. // One more.
++count; ++current_size;
// Update the position of the next free node in the pool. // Update the position of the next free node in the pool.
find_next_free(); find_next_free();
@ -1136,7 +1152,7 @@ namespace etl
next_free = std::min(next_free, new_free); next_free = std::min(next_free, new_free);
// One less. // One less.
--count; --current_size;
// Disconnect the node from the list. // Disconnect the node from the list.
join(*node.previous, *node.next); join(*node.previous, *node.next);
@ -1146,7 +1162,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Get the head node. /// Get the head node.
//************************************************************************* //*************************************************************************
inline Node& get_head() Node& get_head()
{ {
return *terminal_node.next; return *terminal_node.next;
} }
@ -1154,7 +1170,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Get the head node. /// Get the head node.
//************************************************************************* //*************************************************************************
inline const Node& get_head() const const Node& get_head() const
{ {
return *terminal_node.next; return *terminal_node.next;
} }
@ -1162,7 +1178,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Get the tail node. /// Get the tail node.
//************************************************************************* //*************************************************************************
inline Node& get_tail() Node& get_tail()
{ {
return *terminal_node.previous; return *terminal_node.previous;
} }
@ -1170,7 +1186,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Get the tail node. /// Get the tail node.
//************************************************************************* //*************************************************************************
inline const Node& get_tail() const const Node& get_tail() const
{ {
return *terminal_node.previous; return *terminal_node.previous;
} }
@ -1186,8 +1202,8 @@ namespace etl
node_pool[i].mark_as_free(); node_pool[i].mark_as_free();
} }
next_free = 0; next_free = 0;
count = 0; current_size = 0;
join(terminal_node, terminal_node); join(terminal_node, terminal_node);
} }
}; };

View File

@ -30,12 +30,16 @@ SOFTWARE.
#define __ETL_IQUEUE__ #define __ETL_IQUEUE__
#define __ETL_IN_IQUEUE_H__ #define __ETL_IN_IQUEUE_H__
#include <cstddef> #include <stddef.h>
#include "queue_base.h" #include "queue_base.h"
#include "type_traits.h" #include "type_traits.h"
#include "parameter_type.h" #include "parameter_type.h"
#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif
namespace etl namespace etl
{ {
//*************************************************************************** //***************************************************************************
@ -69,79 +73,43 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the front of the queue.<br> /// Gets a reference to the item at the front of the queue.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.<br>
/// 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. /// \return A reference to the item at the front of the queue.
//************************************************************************* //*************************************************************************
reference front() reference front()
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw queue_empty();
}
#endif
return buffer[out]; return buffer[out];
} }
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the front of the queue.<br> /// Gets a const reference to the item at the front of the queue.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.<br>
/// 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. /// \return A const reference to the item at the front of the queue.
//************************************************************************* //*************************************************************************
const_reference front() const const_reference front() const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw queue_empty();
}
#endif
return buffer[out]; return buffer[out];
} }
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the back of the queue.<br> /// Gets a reference to the item at the back of the queue.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.<br>
/// 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. /// \return A reference to the item at the back of the queue.
//************************************************************************* //*************************************************************************
reference back() reference back()
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw queue_empty();
}
#endif
return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; return buffer[in == 0 ? MAX_SIZE - 1 : in - 1];
} }
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the back of the queue.<br> /// Gets a const reference to the item at the back of the queue.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::queue_empty if the queue is empty.<br>
/// 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. /// \return A const reference to the item at the back of the queue.
//************************************************************************* //*************************************************************************
const_reference back() const const_reference back() const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw queue_empty();
}
#endif
return buffer[in == 0 ? MAX_SIZE - 1 : in - 1]; return buffer[in == 0 ? MAX_SIZE - 1 : in - 1];
} }
//************************************************************************* //*************************************************************************
/// Adds an item to the queue. /// 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. /// otherwise does nothing if full.
///\param item The item to push to the queue. ///\param item The item to push to the queue.
//************************************************************************* //*************************************************************************
@ -153,11 +121,15 @@ namespace etl
in = (in == (MAX_SIZE - 1)) ? 0 : in + 1; in = (in == (MAX_SIZE - 1)) ? 0 : in + 1;
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
#ifdef ETL_THROW_EXCEPTIONS
{ {
throw queue_full(); throw queue_full();
} }
#else
{
error_handler::error(forward_list_full());
}
#endif #endif
} }
@ -165,7 +137,7 @@ namespace etl
/// Allows a possibly more efficient 'push' by moving to the next input item /// Allows a possibly more efficient 'push' by moving to the next input item
/// and returning a reference to it. /// and returning a reference to it.
/// This may eliminate a copy by allowing direct construction in-place.<br> /// This may eliminate a copy by allowing direct construction in-place.<br>
/// 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. /// otherwise does nothing if full.
/// \return A reference to the position to 'push' to. /// \return A reference to the position to 'push' to.
//************************************************************************* //*************************************************************************
@ -178,11 +150,15 @@ namespace etl
in = (in == (MAX_SIZE - 1)) ? 0 : in + 1; in = (in == (MAX_SIZE - 1)) ? 0 : in + 1;
++current_size; ++current_size;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
#ifdef ETL_THROW_EXCEPTIONS
{ {
throw queue_full(); throw queue_full();
} }
#else
{
error_handler::error(forward_list_full());
}
#endif #endif
return buffer[next]; return buffer[next];

View File

@ -30,12 +30,16 @@ SOFTWARE.
#define __ETL_ISTACK__ #define __ETL_ISTACK__
#define __ETL_IN_ISTACK_H__ #define __ETL_IN_ISTACK_H__
#include <cstddef> #include <stddef.h>
#include "stack_base.h" #include "stack_base.h"
#include "type_traits.h" #include "type_traits.h"
#include "parameter_type.h" #include "parameter_type.h"
#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif
namespace etl namespace etl
{ {
//*************************************************************************** //***************************************************************************
@ -69,25 +73,16 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a reference to the item at the top of the stack.<br> /// Gets a reference to the item at the top of the stack.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_empty if the stack is empty.<br>
/// 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. /// \return A reference to the item at the top of the stack.
//************************************************************************* //*************************************************************************
reference top() reference top()
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw stack_empty();
}
#endif
return buffer[top_index]; return buffer[top_index];
} }
//************************************************************************* //*************************************************************************
/// Adds an item to the stack. /// 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. /// otherwise does nothing if full.
///\param item The item to push to the stack. ///\param item The item to push to the stack.
//************************************************************************* //*************************************************************************
@ -98,11 +93,15 @@ namespace etl
top_index = current_size++; top_index = current_size++;
buffer[top_index] = item; buffer[top_index] = item;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
#ifdef ETL_THROW_EXCEPTIONS
{ {
throw stack_full(); throw stack_full();
} }
#else
{
error_handler::error(stack_full());
}
#endif #endif
} }
@ -110,7 +109,7 @@ namespace etl
/// Allows a possibly more efficient 'push' by moving to the next input item /// Allows a possibly more efficient 'push' by moving to the next input item
/// and returning a reference to it. /// and returning a reference to it.
/// This may eliminate a copy by allowing direct construction in-place.<br> /// This may eliminate a copy by allowing direct construction in-place.<br>
/// 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. /// otherwise does nothing if full.
/// \return A reference to the position to 'push' to. /// \return A reference to the position to 'push' to.
//************************************************************************* //*************************************************************************
@ -120,11 +119,15 @@ namespace etl
{ {
top_index = current_size++; top_index = current_size++;
} }
#ifdef ETL_USE_EXCEPTIONS
else else
#ifdef ETL_THROW_EXCEPTIONS
{ {
throw stack_full(); throw stack_full();
} }
#else
{
error_handler::error(stack_full());
}
#endif #endif
return buffer[top_index]; return buffer[top_index];
@ -132,19 +135,10 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Gets a const reference to the item at the top of the stack.<br> /// Gets a const reference to the item at the top of the stack.<br>
/// If ETL_USE_EXCEPTIONS is defined, throws an etl::stack_empty if the stack is empty.<br>
/// 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. /// \return A const reference to the item at the top of the stack.
//************************************************************************* //*************************************************************************
const_reference top() const const_reference top() const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (empty())
{
throw stack_empty();
}
#endif
return buffer[top_index]; return buffer[top_index];
} }

View File

@ -33,12 +33,16 @@ SOFTWARE.
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <cstddef> #include <stddef.h>
#include "vector_base.h" #include "vector_base.h"
#include "type_traits.h" #include "type_traits.h"
#include "parameter_type.h" #include "parameter_type.h"
#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif
namespace etl namespace etl
{ {
//*************************************************************************** //***************************************************************************
@ -198,19 +202,22 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Resizes the vector. /// 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. /// maximum then a vector_full is thrown.
///\param newSize The new size. ///\param newSize The new size.
///\param value The value to fill new elements with. Default = default contructed value. ///\param value The value to fill new elements with. Default = default contructed value.
//********************************************************************* //*********************************************************************
void resize(size_t newSize, T value = T()) void resize(size_t newSize, T value = T())
{ {
#ifdef ETL_USE_EXCEPTIONS
if (newSize > MAX_SIZE) if (newSize > MAX_SIZE)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
} #else
error_handler::error(vector_full());
#endif #endif
}
if (newSize > current_size) if (newSize > current_size)
{ {
@ -242,36 +249,42 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Returns a reference to the value at index 'i' /// 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. ///\param i The index.
///\return A reference to the value at index 'i' ///\return A reference to the value at index 'i'
//********************************************************************* //*********************************************************************
reference at(size_t i) reference at(size_t i)
{ {
#ifdef ETL_USE_EXCEPTIONS
if (i >= current_size) if (i >= current_size)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw vector_out_of_bounds(); throw vector_out_of_bounds();
} #else
error_handler::error(vector_out_of_bounds());
#endif #endif
}
return p_buffer[i]; return p_buffer[i];
} }
//********************************************************************* //*********************************************************************
/// Returns a const reference to the value at index '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. ///\param i The index.
///\return A const reference to the value at index 'i' ///\return A const reference to the value at index 'i'
//********************************************************************* //*********************************************************************
const_reference at(size_t i) const const_reference at(size_t i) const
{ {
#ifdef ETL_USE_EXCEPTIONS
if (i >= current_size) if (i >= current_size)
{ {
#ifdef ETL_THROW_EXCEPTIONS
throw vector_out_of_bounds(); throw vector_out_of_bounds();
} #else
error_handler::error(vector_out_of_bounds());
#endif #endif
}
return p_buffer[i]; return p_buffer[i];
} }
@ -332,8 +345,8 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Assigns values to the vector. /// 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.
/// If ETL_USE_EXCEPTIONS is defined, throws vector_iterator if the iterators are reversed. /// If ETL_THROW_EXCEPTIONS is defined, throws vector_iterator if the iterators are reversed.
///\param first The iterator to the first element. ///\param first The iterator to the first element.
///\param last The iterator to the last element + 1. ///\param last The iterator to the last element + 1.
//********************************************************************* //*********************************************************************
@ -344,14 +357,18 @@ namespace etl
if (count < 0) if (count < 0)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_iterator(); throw vector_iterator();
#else
error_handler::error((vector_iterator));
#endif #endif
} }
else if (static_cast<size_t>(count) > MAX_SIZE) else if (static_cast<size_t>(count) > MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else
@ -364,7 +381,7 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Assigns values to the vector. /// 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 n The number of elements to add.
///\param value The value to insert for each element. ///\param value The value to insert for each element.
//********************************************************************* //*********************************************************************
@ -372,8 +389,10 @@ namespace etl
{ {
if (n > MAX_SIZE) if (n > MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else
@ -385,15 +404,17 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Inserts a value at the end of the vector. /// 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. ///\param value The value to add.
//********************************************************************* //*********************************************************************
void push_back(parameter_t value) void push_back(parameter_t value)
{ {
if (current_size == MAX_SIZE) if (current_size == MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else
@ -404,7 +425,7 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Inserts a value to the vector. /// 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 position The position to insert at.
///\param value The value to insert. ///\param value The value to insert.
//********************************************************************* //*********************************************************************
@ -419,8 +440,10 @@ namespace etl
{ {
if ((current_size + 1) > MAX_SIZE) if ((current_size + 1) > MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else
@ -436,7 +459,7 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Inserts 'n' values to the vector. /// 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 position The position to insert at.
///\param n The number of elements to add. ///\param n The number of elements to add.
///\param value The value to insert. ///\param value The value to insert.
@ -445,8 +468,10 @@ namespace etl
{ {
if ((current_size + n) > MAX_SIZE) if ((current_size + n) > MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else
@ -459,7 +484,7 @@ namespace etl
//********************************************************************* //*********************************************************************
/// Inserts a range of values to the vector. /// 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 position The position to insert at.
///\param first The first element to add. ///\param first The first element to add.
///\param last The last + 1 element to add. ///\param last The last + 1 element to add.
@ -471,8 +496,10 @@ namespace etl
if ((current_size + count) > MAX_SIZE) if ((current_size + count) > MAX_SIZE)
{ {
#ifdef ETL_USE_EXCEPTIONS #ifdef ETL_THROW_EXCEPTIONS
throw vector_full(); throw vector_full();
#else
error_handler::error(vector_full());
#endif #endif
} }
else else

8
list.h
View File

@ -29,7 +29,7 @@ SOFTWARE.
#ifndef __ETL_LIST__ #ifndef __ETL_LIST__
#define __ETL_LIST__ #define __ETL_LIST__
#include <cstddef> #include <stddef.h>
#include "ilist.h" #include "ilist.h"
#include "container.h" #include "container.h"
@ -66,7 +66,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Default constructor. /// Default constructor.
//************************************************************************* //*************************************************************************
inline list() list()
: ilist<T>(&node_pool[0], MAX_SIZE) : ilist<T>(&node_pool[0], MAX_SIZE)
{ {
} }
@ -74,7 +74,7 @@ namespace etl
//************************************************************************* //*************************************************************************
/// Construct from size and value. /// Construct from size and value.
//************************************************************************* //*************************************************************************
inline explicit list(size_t initialSize, typename ilist<T>::parameter_t value = T()) explicit list(size_t initialSize, typename ilist<T>::parameter_t value = T())
: ilist<T>(&node_pool[0], MAX_SIZE) : ilist<T>(&node_pool[0], MAX_SIZE)
{ {
ilist<T>::assign(initialSize, value); ilist<T>::assign(initialSize, value);
@ -175,7 +175,7 @@ namespace etl
// Swap the data. // Swap the data.
std::swap_ranges(etl::begin(node_pool), etl::end(node_pool), etl::begin(other.node_pool)); 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->next_free, other.next_free);
std::swap(this->count, other.count); std::swap(this->current_size, other.current_size);
} }
private: private:

View File

@ -33,12 +33,11 @@ SOFTWARE.
#ifndef __ETL_LIST_BASE__ #ifndef __ETL_LIST_BASE__
#define __ETL_LIST_BASE__ #define __ETL_LIST_BASE__
#include <cstddef> #include <stddef.h>
#include "exception.h" #include "exception.h"
namespace etl namespace etl
{ {
#ifdef ETL_USE_EXCEPTIONS
//*************************************************************************** //***************************************************************************
/// Exception for the list. /// Exception for the list.
///\ingroup list ///\ingroup list
@ -62,7 +61,7 @@ namespace etl
public: public:
list_full() list_full()
: list_exception("List full") : list_exception("list: full")
{ {
} }
}; };
@ -76,11 +75,10 @@ namespace etl
public: public:
list_iterator() list_iterator()
: list_exception("Iterator problem") : list_exception("list: iterator problem")
{ {
} }
}; };
#endif
//*************************************************************************** //***************************************************************************
/// The base class for all lists. /// The base class for all lists.
@ -97,7 +95,7 @@ namespace etl
//************************************************************************* //*************************************************************************
size_type size() const size_type size() const
{ {
return count; return current_size;
} }
//************************************************************************* //*************************************************************************
@ -113,7 +111,7 @@ namespace etl
//************************************************************************* //*************************************************************************
bool empty() const bool empty() const
{ {
return count == 0; return current_size == 0;
} }
//************************************************************************* //*************************************************************************
@ -121,7 +119,16 @@ namespace etl
//************************************************************************* //*************************************************************************
bool full() const 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: protected:
@ -131,14 +138,14 @@ namespace etl
//************************************************************************* //*************************************************************************
list_base(size_type max_size) list_base(size_type max_size)
: next_free(0), : next_free(0),
count(0), current_size(0),
MAX_SIZE(max_size) MAX_SIZE(max_size)
{ {
} }
size_type next_free; ///< The index of the next free node. 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. const size_type MAX_SIZE; ///< The maximum size of the list.
}; };
} }

View File

@ -29,7 +29,7 @@ SOFTWARE.
#ifndef __ETL_QUEUE__ #ifndef __ETL_QUEUE__
#define __ETL_QUEUE__ #define __ETL_QUEUE__
#include <cstddef> #include <stddef.h>
#include "iqueue.h" #include "iqueue.h"
#include "container.h" #include "container.h"

View File

@ -33,13 +33,12 @@ SOFTWARE.
#ifndef __ETL_QUEUE_BASE__ #ifndef __ETL_QUEUE_BASE__
#define __ETL_QUEUE_BASE__ #define __ETL_QUEUE_BASE__
#include <cstddef> #include <stddef.h>
#include "exception.h" #include "exception.h"
namespace etl namespace etl
{ {
#ifdef ETL_USE_EXCEPTIONS
//*************************************************************************** //***************************************************************************
/// The base class for queue exceptions. /// The base class for queue exceptions.
///\ingroup queue ///\ingroup queue
@ -63,26 +62,11 @@ namespace etl
public: public:
queue_full() 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. /// The base class for all queues.
///\ingroup queue ///\ingroup queue
@ -135,6 +119,15 @@ namespace etl
return current_size == MAX_SIZE; 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. /// Clears the queue to the empty state.
//************************************************************************* //*************************************************************************

View File

@ -29,7 +29,7 @@ SOFTWARE.
#ifndef __ETL_STACK__ #ifndef __ETL_STACK__
#define __ETL_STACK__ #define __ETL_STACK__
#include <cstddef> #include <stddef.h>
#include <algorithm> #include <algorithm>
#include "istack.h" #include "istack.h"

View File

@ -33,13 +33,12 @@ SOFTWARE.
#ifndef __ETL_STACK_BASE__ #ifndef __ETL_STACK_BASE__
#define __ETL_STACK_BASE__ #define __ETL_STACK_BASE__
#include <cstddef> #include <stddef.h>
#include "exception.h" #include "exception.h"
namespace etl namespace etl
{ {
#ifdef ETL_USE_EXCEPTIONS
//*************************************************************************** //***************************************************************************
///\ingroup stack ///\ingroup stack
/// The base class for stack exceptions. /// The base class for stack exceptions.
@ -63,26 +62,11 @@ namespace etl
public: public:
stack_full() 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 ///\ingroup stack
/// A fixed capacity stack written in the STL style. /// A fixed capacity stack written in the STL style.
@ -136,6 +120,15 @@ namespace etl
return MAX_SIZE; 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. /// Clears the stack to the empty state.
//************************************************************************* //*************************************************************************