From 849d0c1c34d52e8acb8e622c60e4e51942a7ab9d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 1 Aug 2013 23:44:02 -0700 Subject: [PATCH] Return popped element in List --- modules/beast_core/containers/beast_List.h | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/modules/beast_core/containers/beast_List.h b/modules/beast_core/containers/beast_List.h index 38c9493281..3d352d05e7 100644 --- a/modules/beast_core/containers/beast_List.h +++ b/modules/beast_core/containers/beast_List.h @@ -315,6 +315,14 @@ public: typedef Element* pointer; typedef Element const* const_pointer; + /** Thrown when some members are called with an empty list. */ + struct empty_list_error : std::logic_error + { + empty_list_error () : std::logic_error ("empty list") + { + } + }; + class Node : Uncopyable { public: @@ -474,7 +482,7 @@ public: reference front () { if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); + Throw (empty_list_error (), __FILE__, __LINE__); return element_from (m_head.m_next); } @@ -488,7 +496,7 @@ public: const_reference front () const { if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); + Throw (empty_list_error (), __FILE__, __LINE__); return element_from (m_head.m_next); } @@ -502,7 +510,7 @@ public: reference back () { if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); + Throw (empty_list_error (), __FILE__, __LINE__); return element_from (m_tail.m_prev); } @@ -516,7 +524,7 @@ public: const_reference back () const { if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); + Throw (empty_list_error (), __FILE__, __LINE__); return element_from (m_tail.m_prev); } @@ -671,13 +679,13 @@ public: /** Remove the element at the beginning of the list. @invariant The list must not be empty. + @return A reference to the popped element. */ - void pop_front () + Element& pop_front () { - if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); - + Element& elem (front ()); erase (begin ()); + return elem; } /** Append an element at the end of the list. @@ -694,13 +702,13 @@ public: /** Remove the element at the end of the list. @invariant The list must not be empty. + @return A reference to the popped element. */ - void pop_back () + Element& pop_back () { - if (empty ()) - Throw (Error ().fail (__FILE__, __LINE__, Error::noMoreData)); - + Element& elem (back ()); erase (--end ()); + return elem; } /** Swap contents with another list.