1. 程式人生 > >基於stl序列容器實現的通用集合類 (執行緒安全版)

基於stl序列容器實現的通用集合類 (執行緒安全版)

{
 31    typedef U<T> Allocator;
 32    typedef C<T,Allocator>base;
 33    typedef STLCollection<T,ThreadModel,MutexPolicy,C,U> self;
 34
 35public:
 36    STLCollection()
 37    {
 38    }
 39    explicit STLCollection(const Allocator& al)
 40        :base(al)
 41    {
 42    }
 43    explicit
 STLCollection(size_t n)
 44        :base(n)
 45    {
 46    }
 47    STLCollection(size_t n,const T& t)
 48        :base(n,t)
 49    {
 50    }
 51    STLCollection(size_t n,const T& t,const Allocator& al)
 52        :base(n,t,al)
 53    {
 54    }
 55    STLCollection(const STLCollection& right)
 56        :base(right)
 57    {
 58    }
 59
 60    template<class InputIterator> 61    STLCollection(InputIterator first,InputIterator last)
 62        :base(first,last)
 63    {
 64    }
 65
 66    template<class InputIterator> 67    STLCollection(InputIterator first,InputIterator last,const Allocator
& al)
 68        :base(first,last,al)
 69    {
 70    }
 71    ~STLCollection()
 72    {
 73        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
 74    }
 75
 76public:
 77    usingbase::erase;
 78    usingbase::insert;
 79    usingbase::front;
 80    usingbase::back;
 81
 82    void add(const T& t,bool append =true)
 83    {
 84        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
 85        if (append)
 86            base::insert(base::end(),t);
 87        else 88            base::insert(base::begin(),t);
 89    }
 90    void insert(size_t index,const T& t)
 91    {
 92        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
 93        insert_impl(index,t,typename std::iterator_traits<typename base::iterator>::iterator_category());
 94    }
 95
 96    void erase(size_t index)
 97    {
 98        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
 99        erase_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
100    }
101    void erase(size_t beg,size_t end)
102    {
103        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
104        erase_impl(beg,end,typename std::iterator_traits<typename base::iterator>::iterator_category());
105    }
106    void erase(const T& val)
107    {
108        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
109        typename base::iterator it = std::find(base::begin(),base::end(),val);
110        if (it !=base::end()) base::erase(it);
111    }
112    template<class Predicate>113    void erase(const Predicate& Pred)
114    {
115        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
116        typename base::iterator it = std::find_if(base::begin(),base::end(),Pred);
117        if (it !=base::end()) base::erase(it);
118    }
119
120    voidset(size_t index,const T& t)
121    {
122        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
123        T* p =get(index);
124        if (p) *= t;
125    }
126
127    T*get(size_t index) 
128    {
129        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
130        return get_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
131    }
132    const T*get(size_t index) const133    {
134        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
135        return get_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
136    }
137
138    T* find(const T& val,size_t* index)
139    {
140        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
141
142        typename base::iterator it = std::find(base::begin(),base::end(),val);
143        if (it ==base::end()) return NULL;
144        if (index) *index = std::distance(base::begin(),it);
145        return&it;
146    }
147    const T* find(const T& val,size_t* index) const148    {
149        typename ThreadModel<self,MutexPolicy>::Lock guard(lock_);
150
151        typename base::const_iterator it = std::find(base::begin(),base::end(),val);
152        if (it