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