OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WSharedSequenceContainer.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSHAREDSEQUENCECONTAINER_H
26 #define WSHAREDSEQUENCECONTAINER_H
27 
28 #include <algorithm>
29 
30 #include <boost/thread.hpp>
31 
32 #include "WSharedObject.h"
33 
34 /**
35  * This class provides a common interface for thread-safe access to sequence containers (list, vector, dequeue ).
36  * \param S the sequence container to use. Everything is allowed here which provides push_back and pop_back as well as size functionality.
37  */
38 template < typename S >
40 {
41 public:
42 
43  // Some helpful typedefs
44 
45  /**
46  * A typedef for the correct const iterator useful to traverse this sequence container.
47  */
48  typedef typename S::const_iterator ConstIterator;
49 
50  /**
51  * A typedef for the correct iterator to traverse this sequence container.
52  */
53  typedef typename S::iterator Iterator;
54 
55  /**
56  * The type of the elements
57  */
58  typedef typename S::value_type value_type;
59 
60  /**
61  * Default constructor.
62  */
64 
65  /**
66  * Destructor.
67  */
68  virtual ~WSharedSequenceContainer();
69 
70  //////////////////////////////////////////////////////////////////////////////////////////
71  // These methods implement common methods of all sequence containers. The list is not
72  // complete but should be enough for now.
73  // \NOTE: all methods using or returning iterators are NOT implemented here. Use the access
74  // Object (getAccessObject) to iterate.
75  //////////////////////////////////////////////////////////////////////////////////////////
76 
77  /**
78  * Adds a new element at the end of the container.
79  *
80  * \param x the new element.
81  */
82  void push_back( const typename S::value_type& x );
83 
84  /**
85  * Adds a new element at the beginning of the container.
86  *
87  * \param x the new element.
88  */
89  void push_front( const typename S::value_type& x );
90 
91  /**
92  * Removes an element from the end.
93  */
94  void pop_back();
95 
96  /**
97  * Clears the container.
98  */
99  void clear();
100 
101  /**
102  * The size of the container.
103  *
104  * \return the size.
105  *
106  * \note: be aware that the size can change at every moment after getting the size, since the read lock got freed. Better use
107  * access objects to lock the container and use size() on the container directly.
108  */
109  size_t size() const;
110 
111  /**
112  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
113  * Use iterators and read/write tickets for fast iteration.
114  *
115  * \param n the item index
116  *
117  * \return reference to element at the specified position
118  */
119  typename S::value_type& operator[]( size_t n );
120 
121  /**
122  * Get item at position n. Uses the [] operator of the underlying container. Please do not use this for iteration as it locks every access.
123  * Use iterators and read/write tickets for fast iteration.
124  *
125  * \param n the item index
126  *
127  * \return reference to element at the specified position
128  */
129  const typename S::value_type& operator[]( size_t n ) const;
130 
131  /**
132  * Get item at position n. Uses the at-method of the underlying container. Please do not use this for iteration as it locks every access.
133  * Use iterators and read/write tickets for fast iteration.
134  *
135  * \param n the item index
136  *
137  * \return reference to element at the specified position
138  */
139  typename S::value_type& at( size_t n );
140 
141  /**
142  * Get item at position n. Uses the at-method of the underlying container. Please do not use this for iteration as it locks every access.
143  * Use iterators and read/write tickets for fast iteration.
144  *
145  * \param n the item index
146  *
147  * \return reference to element at the specified position
148  */
149  const typename S::value_type& at( size_t n ) const;
150 
151  /**
152  * Searches and removes the specified element. If it is not found, nothing happens. It mainly is a comfortable forwarder for std::remove and
153  * S::erase.
154  *
155  * \param element the element to remove
156  */
157  void remove( const typename S::value_type& element );
158 
159  /**
160  * Erase the element at the specified position. Read your STL reference for more details.
161  *
162  * \param position where to erase
163  *
164  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
165  */
167 
168  /**
169  * Erase the specified range of elements. Read your STL reference for more details.
170  *
171  * \param first Iterators specifying a range within the vector to be removed: [first,last).
172  * \param last Iterators specifying a range within the vector to be removed: [first,last).
173  *
174  * \return A random access iterator pointing to the new location of the element that followed the last element erased by the function call.
175  */
178 
179  /**
180  * Replaces the specified old value by a new one. If the old one does not exist, nothing happens. This is a comfortable forwarder for
181  * std::replace.
182  *
183  * \param oldValue the old value to replace
184  * \param newValue the new value
185  */
186  void replace( const typename S::value_type& oldValue, const typename S::value_type& newValue );
187 
188  /**
189  * Counts the number of occurrences of the specified value inside the container. This is a comfortable forwarder for std::count.
190  *
191  * \param value the value to count
192  *
193  * \return the number of items found.
194  */
195  size_t count( const value_type& value );
196 
197  /**
198  * Resorts the container using the specified comparator from its begin to its end.
199  *
200  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
201  *
202  * \param comp the comparator
203  */
204  template < typename Comparator >
205  void sort( Comparator comp );
206 
207  /**
208  * Resorts the container using the specified comparator between [first,last) in ascending order.
209  *
210  * \param first the first element
211  * \param last the last element
212  * \param comp the comparator
213  */
214  template < typename Comparator >
215  void sort( typename WSharedSequenceContainer< S >::Iterator first, typename WSharedSequenceContainer< S >::Iterator last, Comparator comp );
216 
217  /**
218  * Searches the specified value in the range [first,last).
219  *
220  * \param first the first element
221  * \param last the last element
222  * \param value the value to search.
223  *
224  * \return the iterator pointing to the found element.
225  */
228  const typename S::value_type& value );
229 
230  /**
231  * Searches the specified value in the range [begin,end).
232  *
233  * \param value the value to search.
234  *
235  * \return the iterator pointing to the found element.
236  */
237  typename WSharedSequenceContainer< S >::ConstIterator find( const typename S::value_type& value );
238 
239 protected:
240 
241 private:
242 };
243 
244 template < typename S >
246  WSharedObject< S >()
247 {
248  // init members
249 }
250 
251 template < typename S >
253 {
254  // clean up
255 }
256 
257 template < typename S >
258 void WSharedSequenceContainer< S >::push_back( const typename S::value_type& x )
259 {
260  // Lock, if "a" looses focus -> look is freed
262  a->get().push_back( x );
263 }
264 
265 template < typename S >
266 void WSharedSequenceContainer< S >::push_front( const typename S::value_type& x )
267 {
268  // Lock, if "a" looses focus -> look is freed
270  a->get().insert( a->get().begin(), x );
271 }
272 
273 template < typename S >
275 {
276  // Lock, if "a" looses focus -> look is freed
278  a->get().pop_back();
279 }
280 
281 template < typename S >
283 {
284  // Lock, if "a" looses focus -> look is freed
286  a->get().clear();
287 }
288 
289 template < typename S >
291 {
292  // Lock, if "a" looses focus -> look is freed
294  size_t size = a->get().size();
295  return size;
296 }
297 
298 template < typename S >
299 typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n )
300 {
302  return const_cast< S& >( a->get() ).operator[]( n ); // read tickets return the handled object const. This is bad here although in most cases
303  // it is useful and needed.
304 }
305 
306 template < typename S >
307 const typename S::value_type& WSharedSequenceContainer< S >::operator[]( size_t n ) const
308 {
310  return a->get().operator[]( n );
311 }
312 
313 template < typename S >
314 typename S::value_type& WSharedSequenceContainer< S >::at( size_t n )
315 {
317  return const_cast< S& >( a->get() ).at( n ); // read tickets return the handled object const. This is bad here although in most cases it
318  // is useful and needed.
319 }
320 
321 template < typename S >
322 const typename S::value_type& WSharedSequenceContainer< S >::at( size_t n ) const
323 {
325  return a->get().at( n );
326 }
327 
328 template < typename S >
329 void WSharedSequenceContainer< S >::remove( const typename S::value_type& element )
330 {
331  // Lock, if "a" looses focus -> look is freed
333  a->get().erase( std::remove( a->get().begin(), a->get().end(), element ), a->get().end() );
334 }
335 
336 template < typename S >
338 {
339  // Lock, if "a" looses focus -> look is freed
341  return a->get().erase( position );
342 }
343 
344 template < typename S >
348 {
349  // Lock, if "a" looses focus -> look is freed
351  return a->get().erase( first, last );
352 }
353 
354 template < typename S >
355 void WSharedSequenceContainer< S >::replace( const typename S::value_type& oldValue, const typename S::value_type& newValue )
356 {
358  std::replace( a->get().begin(), a->get().end(), oldValue, newValue );
359 }
360 
361 template < typename S >
363 {
365  return std::count( a->get().begin(), a->get().end(), value );
366 }
367 
368 template < typename S >
369 template < typename Comparator >
370 void WSharedSequenceContainer< S >::sort( Comparator comp )
371 {
373  return std::sort( a->get().begin(), a->get().end(), comp );
374 }
375 
376 template < typename S >
377 template < typename Comparator >
380  Comparator comp )
381 {
382  return std::sort( first, last, comp );
383 }
384 
385 template < typename S >
389  const typename S::value_type& value )
390 {
391  return std::find( first, last, value );
392 }
393 
394 template < typename S >
396 {
398  return std::find( a->get().begin(), a->get().end(), value );
399 }
400 
401 #endif // WSHAREDSEQUENCECONTAINER_H
402