From: Christian P. <cp...@us...> - 2005-01-23 13:30:37
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18251/include/pclasses Modified Files: List.h Log Message: Drop own List-implementation. Inherit from std::list introducing locking. Index: List.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/List.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- List.h 22 Dec 2004 17:54:39 -0000 1.1.1.1 +++ List.h 23 Jan 2005 13:30:27 -0000 1.2 @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2004 by Christian Prochnow * + * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * @@ -18,242 +18,247 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifndef _P_List_h_ -#define _P_List_h_ - -#include <pclasses/LinkedItem.h> -#include <pclasses/Algorithm.h> +#ifndef P_List_h +#define P_List_h + +#include <pclasses/LockTraits.h> +#include <list> +#include <memory> namespace P { -//! List-iterator base class -class ListIteratorBase { - public: - ListIteratorBase(DLinkedItemBase* node) - : _node(node) - { } +//! STL-based list-iterator with locking-support +template < + typename _Tp, typename _Ref, typename _Ptr, + typename _MutexT, typename _LockT +> +struct _List_iterator: public std::_List_iterator<_Tp, _Ref, _Ptr> +{ + typedef std::_List_iterator<_Tp, _Ref, _Ptr> BaseType; - ListIteratorBase(const ListIteratorBase& iter) - : _node(iter._node) - { } + typedef typename BaseType::iterator iterator; + typedef typename BaseType::const_iterator const_iterator; - ~ListIteratorBase() - { } + typedef typename BaseType::value_type value_type; + typedef typename BaseType::pointer pointer; + typedef typename BaseType::reference reference; - void inc() - { _node = _node->next; } + _List_iterator(typename BaseType::_Node* __x, _MutexT& mtx) + : BaseType(__x), _lock(mtx) + { } - void dec() - { _node = _node->prev; } + _List_iterator(const BaseType& __i, _MutexT& mtx) + : BaseType(__i), _lock(mtx) + { } - DLinkedItemBase* node() const - { return _node; } + _List_iterator(const _List_iterator& __l) + : BaseType(__l), _lock(__l._lock) + { } - bool operator==(const ListIteratorBase& iter) const - { return (_node == iter._node); } + _LockT _lock; +}; - bool operator!=(const ListIteratorBase& iter) const - { return (_node != iter._node); } - private: - DLinkedItemBase* _node; -}; +//! STL-based list with optional locking +/*! + \todo add list operations [23.2.2.4] +*/ +template < + typename _Tp, + typename _MutexT = Traits::VoidMutex, + typename _Alloc = std::allocator<_Tp> +> +class list: protected std::list<_Tp, _Alloc> +{ + typedef std::list<_Tp, _Alloc> BaseType; -//! List-iterator -template <class Type, class RefType, class PtrType> -class ListIterator: public ListIteratorBase { public: - typedef DLinkedItem<Type> DLinkedItem; - - ListIterator(DLinkedItem* node) - : ListIteratorBase(node) - { } + typedef typename BaseType::value_type value_type; + typedef typename BaseType::pointer pointer; + typedef typename BaseType::const_pointer const_pointer; + typedef typename BaseType::reference reference; + typedef typename BaseType::const_reference const_reference; + typedef typename BaseType::size_type size_type; + typedef typename BaseType::difference_type difference_type; + typedef typename BaseType::allocator_type allocator_type; - ListIterator(const ListIterator& iter) - : ListIteratorBase(iter) - { } + typedef Traits::LockTraits<_MutexT> locktraits_type; + typedef typename locktraits_type::MutexType mutex_type; + typedef typename locktraits_type::ReadLock read_lock; + typedef typename locktraits_type::WriteLock write_lock; - ~ListIterator() - { } + typedef _List_iterator<_Tp,_Tp&,_Tp*, + mutex_type, write_lock> iterator; + typedef _List_iterator<_Tp,const _Tp&, + const _Tp*, mutex_type, read_lock> const_iterator; + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + typedef std::reverse_iterator<iterator> reverse_iterator; - RefType operator*() const - { return static_cast<DLinkedItem*>(node())->data; } + explicit + list(const allocator_type& __a = allocator_type()) + : BaseType(__a) { } - PtrType operator->() const - { return &(operator*()); } + list(size_type __n, const value_type& __value, + const allocator_type& __a = allocator_type()) + : BaseType(__a) + { insert(begin(), __n, __value); } - ListIterator& operator++() - { - inc(); - return *this; - } + explicit + list(size_type __n) + : BaseType(allocator_type()) + { insert(begin(), __n, value_type()); } - ListIterator operator++(int) - { - ListIterator tmp = *this; - inc(); - return tmp; - } + list(const list& __x) + : BaseType(__x.get_allocator()) + { insert(begin(), __x.begin(), __x.end()); } - ListIterator& operator--() + template <typename _InputIterator> + list(_InputIterator __first, _InputIterator __last, + const allocator_type& __a = allocator_type()) + : BaseType(__a) + { insert(begin(), __first, __last); } + + ~list() + { } + + list& operator=(const list& __x) { - dec(); + write_lock lck1(_mutex); + read_lock lck2(__x._mutex); + BaseType::operator=(__x); return *this; } - ListIterator operator--(int) - { - ListIterator tmp = *this; - dec(); - return tmp; + void assign(size_type __n, const value_type& __val) + { + write_lock lck(_mutex); + BaseType::assign(__n, __val); } -}; -//! List template class -template <class Type> -class List { - public: - typedef DLinkedItem<Type> DLinkedItem; - typedef ListIterator<Type,Type&,Type*> Iterator; - typedef ListIterator<Type,const Type&, const Type*> ConstIterator; - - List() throw() - : _data(new DLinkedItem()) + template <typename _InputIterator> + void assign(_InputIterator __first, _InputIterator __last) { - _data->prev = _data->next = _data; + write_lock lck(_mutex); + BaseType::assign(_first, _last); } - List(const List& l) - : _data(new DLinkedItem()) - { - _data->prev = _data->next = _data; - for(ConstIterator i = l.begin(); i != l.end(); i++) - append(*i); - } + allocator_type get_allocator() const + { return BaseType::get_allocator(); } - ~List() throw() + iterator begin() { - clear(); - delete _data; + typedef typename BaseType::_Node _Node; + _Node* n = static_cast<_Node*>(this->_M_node->_M_next); + return iterator(n, _mutex); } - void swap(List& b) throw() - { - DLinkedItem* tmp = static_cast<DLinkedItem*>(_data); - _data = b._data; - b._data = tmp; + const_iterator begin() const + { + typedef typename BaseType::_Node _Node; + _Node* n = static_cast<_Node*>(this->_M_node->_M_next); + return const_iterator(n, _mutex); } - void clear() throw() - { - DLinkedItemBase* curr = _data->next; - while(curr != _data) - { - DLinkedItemBase* tmp = curr->next; - delete static_cast<DLinkedItem*>(curr); - curr = tmp; - } + iterator end() + { return iterator(_M_node, _mutex); } - _data->prev = _data->next = _data; - } + const_iterator end() const + { return const_iterator(_M_node, _mutex); } - bool empty() const throw() - { return (_data->next == _data); } + bool empty() const + { + read_lock lck(_mutex); + return BaseType::empty(); + } - size_t count() const throw() - { return distance(begin(), end()); } - - Iterator begin() throw() - { return Iterator(static_cast<DLinkedItem*>(_data->next)); } + size_type size() const + { return std::distance(begin(), end()); } - ConstIterator begin() const throw() - { return ConstIterator(static_cast<DLinkedItem*>(_data->next)); } + size_type max_size() const + { return size_type(-1); } - Iterator end() throw() - { return Iterator(_data); } + void resize(size_type __new_size, const value_type& __x) + { + write_lock lck(_mutex); + BaseType::resize(__new_size, __x); + } - ConstIterator end() const throw() - { return ConstIterator(_data); } + void resize(size_type __new_size) + { + write_lock lck(_mutex); + BaseType::resize(__new_size, value_type()); + } - Type& front() throw() + value_type front() { return *begin(); } - const Type& front() const throw() + value_type front() const { return *begin(); } - Type& back() throw() + value_type back() { return *(--end()); } - const Type& back() const throw() + value_type back() const { return *(--end()); } - Iterator insert(Iterator pos, const Type& val) - { - DLinkedItem* tmp = - new DLinkedItem(static_cast<DLinkedItem*>(pos.node()->prev), - val, static_cast<DLinkedItem*>(pos.node())); - pos.node()->prev->next = tmp; - pos.node()->prev = tmp; - return Iterator(tmp); + void push_front(const value_type& __x) + { + write_lock lck(_mutex); + BaseType::push_front(__x); } - template <class InputIterator> - void insert(Iterator pos, InputIterator first, InputIterator last) - { - for(; first != last; ++first) - insert(pos, *first); + void push_back(const value_type& __x) + { + write_lock lck(_mutex); + BaseType::push_back(__x); } - Iterator prepend(const Type& val) - { return insert(begin(), val); } - - Iterator append(const Type& val) - { return insert(end(), val); } + void pop_front() + { erase(begin()); } - Iterator erase(Iterator pos) throw() + void pop_back() { - DLinkedItemBase* next = pos.node()->next; - DLinkedItemBase* prev = pos.node()->prev; - DLinkedItem* n = static_cast<DLinkedItem*>(pos.node()); - prev->next = next; - next->prev = prev; - delete n; - return Iterator(static_cast<DLinkedItem*>(next)); + iterator __tmp = end(); + erase(--__tmp); } - Iterator erase(Iterator first, Iterator last) throw() + iterator insert(iterator __position, const value_type& __x) + { return iterator(BaseType::insert(__position, __x), _mutex); } + + void insert(iterator __pos, size_type __n, const value_type& __x) + { BaseType::insert(__pos, __n, __x); } + + template<typename _InputIterator> + void insert(iterator __pos, _InputIterator __first, _InputIterator __last) + { BaseType::insert(__pos, __first, __last); } + + iterator erase(iterator __position) + { return iterator(BaseType::erase(__position), _mutex); } + + iterator erase(iterator __first, iterator __last) { - for(; first != last; ++first) - erase(first); - return last; + while (__first != __last) + erase(__first++); + return __last; } - List& operator=(const List& rhs) - { - List(rhs).swap(*this); - return *this; + void swap(list& __x) + { + write_lock lck1(_mutex); + write_lock lck2(__x._mutex); + BaseType::swap(__x); } - bool operator==(const List& rhs) const - { - ConstIterator endlh = end(), ilh = begin(); - ConstIterator endrh = rhs.end(), irh = rhs.begin(); - while(ilh != endlh && irh != endrh && *ilh == *irh) - { - ++ilh; - ++irh; - } - return (ilh == endlh && irh == endrh); + void clear() + { + write_lock lck(_mutex); + BaseType::clear(); } private: - mutable DLinkedItem* _data; + mutable mutex_type _mutex; }; -template <class Type> -inline void swap(List<Type>& a, List<Type>& b) throw() -{ a.swap(b); } - } // !namespace P #endif |