OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifndef WPREDICATEHELPER_H 00026 #define WPREDICATEHELPER_H 00027 00028 #include <string> 00029 00030 #include <boost/shared_ptr.hpp> 00031 #include <boost/function.hpp> 00032 00033 /** 00034 * This namespace contains some useful helper classes which use some common class methods as predicate. This is especially useful and handy if 00035 * 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. 00036 */ 00037 namespace WPredicateHelper 00038 { 00039 /** 00040 * Predicate which is always true. Useful if you want to ignore something all the time. 00041 * 00042 * @tparam T the value type to check 00043 * 00044 * \return always true. 00045 */ 00046 template< typename T > 00047 bool alwaysTrue( const T& /* obj */ ) 00048 { 00049 return true; 00050 } 00051 00052 /** 00053 * Predicate which is always false. Useful if you want to ignore something all the time. 00054 * 00055 * @tparam T the value type to check 00056 * 00057 * \return always false. 00058 */ 00059 template< typename T > 00060 bool alwaysFalse( const T& /* obj */ ) 00061 { 00062 return false; 00063 } 00064 00065 /** 00066 * This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName() 00067 * method. This predicate can check against a defined name. Useful for searching. 00068 */ 00069 template< typename T > 00070 class Name 00071 { 00072 public: 00073 /** 00074 * Creates instance. The specified string is used for checking. 00075 * 00076 * \param check the string to check against. 00077 */ 00078 explicit Name( std::string check ): 00079 m_check( check ) 00080 { 00081 }; 00082 00083 /** 00084 * Checks the instance of T against the string specified during construction. 00085 * 00086 * \param inst use getName of this instance of T 00087 * 00088 * \return true if m_checked == inst.getName() 00089 */ 00090 bool operator()( const T& inst ) 00091 { 00092 return inst.getName() == m_check; 00093 }; 00094 00095 private: 00096 /** 00097 * The string to check against. 00098 */ 00099 std::string m_check; 00100 }; 00101 00102 /** 00103 * This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName() 00104 * method. This predicate can check against a defined name. Useful for searching. This partial specialization is for shared_ptr, which are a 00105 * very common tool in OpenWalnut. 00106 */ 00107 template< typename T > 00108 class Name< boost::shared_ptr< T > > 00109 { 00110 public: 00111 /** 00112 * Creates instance. The specified string is used for checking. 00113 * 00114 * \param check the string to check against. 00115 */ 00116 explicit Name( std::string check ): 00117 m_check( check ) 00118 { 00119 }; 00120 00121 /** 00122 * Checks the instance of T against the string specified during construction. 00123 * 00124 * \param inst use getName of this instance of T 00125 * 00126 * \return true if m_checked == inst.getName() 00127 */ 00128 bool operator()( const boost::shared_ptr< T >& inst ) 00129 { 00130 return inst->getName() == m_check; 00131 }; 00132 00133 private: 00134 /** 00135 * The string to check against. 00136 */ 00137 std::string m_check; 00138 }; 00139 00140 /** 00141 * This class builds the base for wrapping around nearly every possible predicates like functors, classes with operator() and so on. It is 00142 * especially useful to have an base class allowing predicate evaluation without knowing the exact predicate type. In multi-threaded 00143 * environments, command queues are a common way to add/remove/replace items in a list. With this base class it is possible to provide 00144 * predicates in such queues. The direct use of this class for std algorithms (find_if, remove_if, count_if, ... ) is not recommended as it 00145 * simply is not needed. 00146 * 00147 * \tparam the type to evaluate the predicate for. Usually, this is the type of list elements. 00148 */ 00149 template < typename T > 00150 class ArbitraryPredicateBase 00151 { 00152 public: 00153 /** 00154 * Creates instance. 00155 */ 00156 ArbitraryPredicateBase() 00157 { 00158 }; 00159 00160 /** 00161 * Destructor. 00162 */ 00163 virtual ~ArbitraryPredicateBase() 00164 { 00165 }; 00166 00167 /** 00168 * Checks the instance of T against an arbitrary predicate. 00169 * 00170 * \param inst the value to check against a predicate 00171 * 00172 * \return true if predicate evaluates to true 00173 */ 00174 virtual bool operator()( T const& inst ) const = 0; 00175 }; 00176 00177 /** 00178 * The actual class implementing the predicate evaluation. The default predicate is a functor evaluating to true or false. For more details 00179 * see \ref ArbitraryPredicateBase. 00180 * 00181 * \tparam T the type to check. This usually is the type of the elements in a list or similar. 00182 * \tparam Predicate this is the predicate type. By default, it is a functor. 00183 */ 00184 template < typename T, typename Predicate = boost::function1< bool, T > > 00185 class ArbitraryPredicate: public ArbitraryPredicateBase< T > 00186 { 00187 public: 00188 /** 00189 * Creates instance. 00190 * 00191 * \param predicate the predicate used for checking 00192 */ 00193 explicit ArbitraryPredicate( Predicate predicate ): 00194 ArbitraryPredicateBase< T >(), 00195 m_predicate( predicate ) 00196 { 00197 }; 00198 00199 /** 00200 * Destructor. 00201 */ 00202 virtual ~ArbitraryPredicate() 00203 { 00204 }; 00205 00206 /** 00207 * Checks the instance of T against an arbitrary predicate. 00208 * 00209 * \param inst the value to check against a predicate 00210 * 00211 * \return true if predicate evaluates to true 00212 */ 00213 virtual bool operator()( T const& inst ) const 00214 { 00215 return m_predicate( inst ); 00216 }; 00217 00218 private: 00219 /** 00220 * The predicate to use for checking 00221 */ 00222 Predicate m_predicate; 00223 }; 00224 } 00225 00226 #endif // WPREDICATEHELPER_H 00227