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 #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  */
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  * The string to check against.
98  */
99  std::string m_check;
100  };
101 
102  /**
103  * This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName()
104  * method. This predicate can check against a defined name. Useful for searching. This partial specialization is for shared_ptr, which are a
105  * very common tool in OpenWalnut.
106  */
107  template< typename T >
108  class Name< boost::shared_ptr< T > >
109  {
110  public:
111  /**
112  * Creates instance. The specified string is used for checking.
113  *
114  * \param check the string to check against.
115  */
116  explicit Name( std::string check ):
117  m_check( check )
118  {
119  };
120 
121  /**
122  * Checks the instance of T against the string specified during construction.
123  *
124  * \param inst use getName of this instance of T
125  *
126  * \return true if m_checked == inst.getName()
127  */
128  bool operator()( const boost::shared_ptr< T >& inst )
129  {
130  return inst->getName() == m_check;
131  };
132 
133  private:
134  /**
135  * The string to check against.
136  */
137  std::string m_check;
138  };
139 
140  /**
141  * This class builds the base for wrapping around nearly every possible predicates like functors, classes with operator() and so on. It is
142  * especially useful to have an base class allowing predicate evaluation without knowing the exact predicate type. In multi-threaded
143  * environments, command queues are a common way to add/remove/replace items in a list. With this base class it is possible to provide
144  * predicates in such queues. The direct use of this class for std algorithms (find_if, remove_if, count_if, ... ) is not recommended as it
145  * simply is not needed.
146  *
147  * \tparam the type to evaluate the predicate for. Usually, this is the type of list elements.
148  */
149  template < typename T >
151  {
152  public:
153  /**
154  * Creates instance.
155  */
157  {
158  };
159 
160  /**
161  * Destructor.
162  */
164  {
165  };
166 
167  /**
168  * Checks the instance of T against an arbitrary predicate.
169  *
170  * \param inst the value to check against a predicate
171  *
172  * \return true if predicate evaluates to true
173  */
174  virtual bool operator()( T const& inst ) const = 0;
175  };
176 
177  /**
178  * The actual class implementing the predicate evaluation. The default predicate is a functor evaluating to true or false. For more details
179  * see \ref ArbitraryPredicateBase.
180  *
181  * \tparam T the type to check. This usually is the type of the elements in a list or similar.
182  * \tparam Predicate this is the predicate type. By default, it is a functor.
183  */
184  template < typename T, typename Predicate = boost::function1< bool, T > >
186  {
187  public:
188  /**
189  * Creates instance.
190  *
191  * \param predicate the predicate used for checking
192  */
193  explicit ArbitraryPredicate( Predicate predicate ):
194  ArbitraryPredicateBase< T >(),
195  m_predicate( predicate )
196  {
197  };
198 
199  /**
200  * Destructor.
201  */
203  {
204  };
205 
206  /**
207  * Checks the instance of T against an arbitrary predicate.
208  *
209  * \param inst the value to check against a predicate
210  *
211  * \return true if predicate evaluates to true
212  */
213  virtual bool operator()( T const& inst ) const
214  {
215  return m_predicate( inst );
216  };
217 
218  private:
219  /**
220  * The predicate to use for checking
221  */
222  Predicate m_predicate;
223  };
224 }
225 
226 #endif // WPREDICATEHELPER_H
227 
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...