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