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
WPredicateHelper.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 WPREDICATEHELPER_H
26
#define WPREDICATEHELPER_H
27
28
#include <string>
29
30
#include <boost/shared_ptr.hpp>
31
#include <boost/function.hpp>
32
33
/**
34
* This namespace contains some useful helper classes which use some common class methods as predicate. This is especially useful and handy if
35
* std containers are used with OpenWalnut's classes. The predicate helper classes allow easy use of std::count_if, std::find_if and so on.
36
*/
37
namespace
WPredicateHelper
38
{
39
/**
40
* Predicate which is always true. Useful if you want to ignore something all the time.
41
*
42
* @tparam T the value type to check
43
*
44
* \return always true.
45
*/
46
template
<
typename
T >
47
bool
alwaysTrue
(
const
T&
/* obj */
)
48
{
49
return
true
;
50
}
51
52
/**
53
* Predicate which is always false. Useful if you want to ignore something all the time.
54
*
55
* @tparam T the value type to check
56
*
57
* \return always false.
58
*/
59
template
<
typename
T >
60
bool
alwaysFalse
(
const
T&
/* obj */
)
61
{
62
return
false
;
63
}
64
65
/**
66
* This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName()
67
* method. This predicate can check against a defined name. Useful for searching.
68
*/
69
template
<
typename
T >
70
class
Name
71
{
72
public
:
73
/**
74
* Creates instance. The specified string is used for checking.
75
*
76
* \param check the string to check against.
77
*/
78
explicit
Name
( std::string check ):
79
m_check
( check )
80
{
81
};
82
83
/**
84
* Checks the instance of T against the string specified during construction.
85
*
86
* \param inst use getName of this instance of T
87
*
88
* \return true if m_checked == inst.getName()
89
*/
90
bool
operator()
(
const
T& inst )
91
{
92
return
inst.getName() ==
m_check
;
93
};
94
95
private
:
96
97
/**
98
* The string to check against.
99
*/
100
std::string
m_check
;
101
};
102
103
/**
104
* This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName()
105
* method. This predicate can check against a defined name. Useful for searching. This partial specialization is for shared_ptr, which are a
106
* very common tool in OpenWalnut.
107
*/
108
template
<
typename
T >
109
class
Name
< boost::shared_ptr< T > >
110
{
111
public
:
112
/**
113
* Creates instance. The specified string is used for checking.
114
*
115
* \param check the string to check against.
116
*/
117
explicit
Name
( std::string check ):
118
m_check
( check )
119
{
120
};
121
122
/**
123
* Checks the instance of T against the string specified during construction.
124
*
125
* \param inst use getName of this instance of T
126
*
127
* \return true if m_checked == inst.getName()
128
*/
129
bool
operator()
(
const
boost::shared_ptr< T >& inst )
130
{
131
return
inst->getName() ==
m_check
;
132
};
133
134
private
:
135
136
/**
137
* The string to check against.
138
*/
139
std::string
m_check
;
140
};
141
142
/**
143
* This class builds the base for wrapping around nearly every possible predicates like functors, classes with operator() and so on. It is
144
* especially useful to have an base class allowing predicate evaluation without knowing the exact predicate type. In multi-threaded
145
* environments, command queues are a common way to add/remove/replace items in a list. With this base class it is possible to provide
146
* predicates in such queues. The direct use of this class for std algorithms (find_if, remove_if, count_if, ... ) is not recommended as it
147
* simply is not needed.
148
*
149
* \tparam the type to evaluate the predicate for. Usually, this is the type of list elements.
150
*/
151
template
<
typename
T >
152
class
ArbitraryPredicateBase
153
{
154
public
:
155
/**
156
* Creates instance.
157
*/
158
ArbitraryPredicateBase
()
159
{
160
};
161
162
/**
163
* Destructor.
164
*/
165
virtual
~ArbitraryPredicateBase
()
166
{
167
};
168
169
/**
170
* Checks the instance of T against an arbitrary predicate.
171
*
172
* \param inst the value to check against a predicate
173
*
174
* \return true if predicate evaluates to true
175
*/
176
virtual
bool
operator()
( T
const
& inst )
const
= 0;
177
};
178
179
/**
180
* The actual class implementing the predicate evaluation. The default predicate is a functor evaluating to true or false. For more details
181
* see \ref ArbitraryPredicateBase.
182
*
183
* \tparam T the type to check. This usually is the type of the elements in a list or similar.
184
* \tparam Predicate this is the predicate type. By default, it is a functor.
185
*/
186
template
<
typename
T,
typename
Predicate = boost::function1<
bool
, T > >
187
class
ArbitraryPredicate
:
public
ArbitraryPredicateBase
< T >
188
{
189
public
:
190
/**
191
* Creates instance.
192
*
193
* \param predicate the predicate used for checking
194
*/
195
explicit
ArbitraryPredicate
( Predicate predicate ):
196
ArbitraryPredicateBase
< T >(),
197
m_predicate
( predicate )
198
{
199
};
200
201
/**
202
* Destructor.
203
*/
204
virtual
~ArbitraryPredicate
()
205
{
206
};
207
208
/**
209
* Checks the instance of T against an arbitrary predicate.
210
*
211
* \param inst the value to check against a predicate
212
*
213
* \return true if predicate evaluates to true
214
*/
215
virtual
bool
operator()
( T
const
& inst )
const
216
{
217
return
m_predicate
( inst );
218
};
219
220
private
:
221
222
/**
223
* The predicate to use for checking
224
*/
225
Predicate
m_predicate
;
226
};
227
}
228
229
#endif // WPREDICATEHELPER_H
230
Generated by
1.8.1.2