OpenWalnut
1.2.5
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
src
core
common
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 >
38
class
WSharedAssociativeContainer
:
public
WSharedObject
< 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
*/
66
WSharedAssociativeContainer
();
67
68
/**
69
* Destructor.
70
*/
71
virtual
~WSharedAssociativeContainer
();
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 >
131
WSharedAssociativeContainer< T >::WSharedAssociativeContainer
():
132
WSharedObject
< T >()
133
{
134
// init members
135
}
136
137
template
<
typename
T >
138
WSharedAssociativeContainer< T >::~WSharedAssociativeContainer
()
139
{
140
// clean up
141
}
142
143
template
<
typename
T >
144
void
WSharedAssociativeContainer< T >::clear
()
145
{
146
typename
WSharedAssociativeContainer< T >::WriteTicket
w =
WSharedObject< T >::getWriteTicket
();
147
w->get().
clear
();
148
}
149
150
template
<
typename
T >
151
bool
WSharedAssociativeContainer< T >::empty
()
const
152
{
153
typename
WSharedAssociativeContainer< T >::ReadTicket
r =
WSharedObject< T >::getReadTicket
();
154
return
r->get().
empty
();
155
}
156
157
template
<
typename
T >
158
size_t
WSharedAssociativeContainer< T >::size
()
const
159
{
160
typename
WSharedAssociativeContainer< T >::ReadTicket
r =
WSharedObject< T >::getReadTicket
();
161
return
r->get().
size
();
162
}
163
164
template
<
typename
T >
165
size_t
WSharedAssociativeContainer< T >::max_size
()
const
166
{
167
typename
WSharedAssociativeContainer< T >::ReadTicket
r =
WSharedObject< T >::getReadTicket
();
168
return
r->get().
max_size
();
169
}
170
171
template
<
typename
T >
172
size_t
WSharedAssociativeContainer< T >::count
(
const
key_type
& x )
const
173
{
174
typename
WSharedAssociativeContainer< T >::ReadTicket
r =
WSharedObject< T >::getReadTicket
();
175
return
r->get().
count
( x );
176
}
177
178
template
<
typename
T >
179
size_t
WSharedAssociativeContainer< T >::erase
(
const
key_type
& x )
180
{
181
typename
WSharedAssociativeContainer< T >::WriteTicket
w =
WSharedObject< T >::getWriteTicket
();
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
{
188
typename
WSharedAssociativeContainer< T >::WriteTicket
w =
WSharedObject< T >::getWriteTicket
();
189
return
w->get().
insert
( x );
190
}
191
192
#endif // WSHAREDASSOCIATIVECONTAINER_H
193
Generated by
1.8.1.2