OpenWalnut  1.5.0dev
WSharedAssociativeContainer.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 WSHAREDASSOCIATIVECONTAINER_H
26 #define WSHAREDASSOCIATIVECONTAINER_H
27 
28 #include <utility>
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 associative containers (set, multiset, map, multimap).
36  */
37 template < typename T >
39 {
40 public:
41  // Some helpful typedefs
42 
43  /**
44  * A typedef for the correct const iterator useful to traverse this sequence container.
45  */
46  typedef typename T::const_iterator ConstIterator;
47 
48  /**
49  * A typedef for the correct iterator to traverse this sequence container.
50  */
51  typedef typename T::iterator Iterator;
52 
53  /**
54  * The type of the elements
55  */
56  typedef typename T::value_type value_type;
57 
58  /**
59  * The type of the key used in this associative container
60  */
61  typedef typename T::key_type key_type;
62 
63  /**
64  * Default constructor.
65  */
67 
68  /**
69  * Destructor.
70  */
72 
73  /**
74  * Clears the container.
75  */
76  void clear();
77 
78  /**
79  * Return true if the container is empty. The sense and non-sense of this method in a multi threaded environment is questionable.
80  *
81  * \return true if empty
82  */
83  bool empty() const;
84 
85  /**
86  * The current size of the container. 0 if empty. The sense and non-sense of this method in a multi threaded environment is questionable.
87  *
88  * \return the size.
89  */
90  size_t size() const;
91 
92  /**
93  * The maximum size of a container.
94  *
95  * \return the maximum size
96  */
97  size_t max_size() const;
98 
99  /**
100  * Count elements with a specific key. The sense and non-sense of this method in a multi threaded environment is questionable.
101  *
102  * \param x the key
103  *
104  * \return the count, 0 if none found.
105  */
106  size_t count( const key_type& x ) const;
107 
108  /**
109  * Erases the element with the specified key.
110  *
111  * \param x the key
112  *
113  * \return the number of elements erased
114  */
115  size_t erase( const key_type& x );
116 
117  /**
118  * Inserts the specified element.
119  *
120  * \param x the element to add
121  *
122  * \return a pair containing the Iterator pointing to the inserted element and the bool is true if the element not existed before.
123  */
124  std::pair< Iterator, bool > insert( const value_type& x );
125 
126 protected:
127 private:
128 };
129 
130 template < typename T >
132  WSharedObject< T >()
133 {
134  // init members
135 }
136 
137 template < typename T >
139 {
140  // clean up
141 }
142 
143 template < typename T >
145 {
147  w->get().clear();
148 }
149 
150 template < typename T >
152 {
154  return r->get().empty();
155 }
156 
157 template < typename T >
159 {
161  return r->get().size();
162 }
163 
164 template < typename T >
166 {
168  return r->get().max_size();
169 }
170 
171 template < typename T >
173 {
175  return r->get().count( x );
176 }
177 
178 template < typename T >
180 {
182  return w->get().erase( x );
183 }
184 
185 template < typename T >
186 std::pair< typename WSharedAssociativeContainer< T >::Iterator, bool > WSharedAssociativeContainer< T >::insert( const value_type& x )
187 {
189  return w->get().insert( x );
190 }
191 
192 #endif // WSHAREDASSOCIATIVECONTAINER_H
193 
This class provides a common interface for thread-safe access to associative containers (set,...
WSharedAssociativeContainer()
Default constructor.
void clear()
Clears the container.
T::iterator Iterator
A typedef for the correct iterator to traverse this sequence container.
bool empty() const
Return true if the container is empty.
T::key_type key_type
The type of the key used in this associative container.
std::pair< Iterator, bool > insert(const value_type &x)
Inserts the specified element.
T::value_type value_type
The type of the elements.
size_t erase(const key_type &x)
Erases the element with the specified key.
T::const_iterator ConstIterator
A typedef for the correct const iterator useful to traverse this sequence container.
virtual ~WSharedAssociativeContainer()
Destructor.
size_t max_size() const
The maximum size of a container.
size_t size() const
The current size of the container.
size_t count(const key_type &x) const
Count elements with a specific key.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:45
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:70
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.