OpenWalnut
1.2.5
|
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 /** 00098 * The string to check against. 00099 */ 00100 std::string m_check; 00101 }; 00102 00103 /** 00104 * This class tests against the getName() method of the instances of type T. Many, many, many many many classes in OpenWalnut provide a getName() 00105 * method. This predicate can check against a defined name. Useful for searching. This partial specialization is for shared_ptr, which are a 00106 * very common tool in OpenWalnut. 00107 */ 00108 template< typename T > 00109 class Name< boost::shared_ptr< T > > 00110 { 00111 public: 00112 /** 00113 * Creates instance. The specified string is used for checking. 00114 * 00115 * \param check the string to check against. 00116 */ 00117 explicit Name( std::string check ): 00118 m_check( check ) 00119 { 00120 }; 00121 00122 /** 00123 * Checks the instance of T against the string specified during construction. 00124 * 00125 * \param inst use getName of this instance of T 00126 * 00127 * \return true if m_checked == inst.getName() 00128 */ 00129 bool operator()( const boost::shared_ptr< T >& inst ) 00130 { 00131 return inst->getName() == m_check; 00132 }; 00133 00134 private: 00135 00136 /** 00137 * The string to check against. 00138 */ 00139 std::string m_check; 00140 }; 00141 00142 /** 00143 * This class builds the base for wrapping around nearly every possible predicates like functors, classes with operator() and so on. It is 00144 * especially useful to have an base class allowing predicate evaluation without knowing the exact predicate type. In multi-threaded 00145 * environments, command queues are a common way to add/remove/replace items in a list. With this base class it is possible to provide 00146 * predicates in such queues. The direct use of this class for std algorithms (find_if, remove_if, count_if, ... ) is not recommended as it 00147 * simply is not needed. 00148 * 00149 * \tparam the type to evaluate the predicate for. Usually, this is the type of list elements. 00150 */ 00151 template < typename T > 00152 class ArbitraryPredicateBase 00153 { 00154 public: 00155 /** 00156 * Creates instance. 00157 */ 00158 ArbitraryPredicateBase() 00159 { 00160 }; 00161 00162 /** 00163 * Destructor. 00164 */ 00165 virtual ~ArbitraryPredicateBase() 00166 { 00167 }; 00168 00169 /** 00170 * Checks the instance of T against an arbitrary predicate. 00171 * 00172 * \param inst the value to check against a predicate 00173 * 00174 * \return true if predicate evaluates to true 00175 */ 00176 virtual bool operator()( T const& inst ) const = 0; 00177 }; 00178 00179 /** 00180 * The actual class implementing the predicate evaluation. The default predicate is a functor evaluating to true or false. For more details 00181 * see \ref ArbitraryPredicateBase. 00182 * 00183 * \tparam T the type to check. This usually is the type of the elements in a list or similar. 00184 * \tparam Predicate this is the predicate type. By default, it is a functor. 00185 */ 00186 template < typename T, typename Predicate = boost::function1< bool, T > > 00187 class ArbitraryPredicate: public ArbitraryPredicateBase< T > 00188 { 00189 public: 00190 /** 00191 * Creates instance. 00192 * 00193 * \param predicate the predicate used for checking 00194 */ 00195 explicit ArbitraryPredicate( Predicate predicate ): 00196 ArbitraryPredicateBase< T >(), 00197 m_predicate( predicate ) 00198 { 00199 }; 00200 00201 /** 00202 * Destructor. 00203 */ 00204 virtual ~ArbitraryPredicate() 00205 { 00206 }; 00207 00208 /** 00209 * Checks the instance of T against an arbitrary predicate. 00210 * 00211 * \param inst the value to check against a predicate 00212 * 00213 * \return true if predicate evaluates to true 00214 */ 00215 virtual bool operator()( T const& inst ) const 00216 { 00217 return m_predicate( inst ); 00218 }; 00219 00220 private: 00221 00222 /** 00223 * The predicate to use for checking 00224 */ 00225 Predicate m_predicate; 00226 }; 00227 } 00228 00229 #endif // WPREDICATEHELPER_H 00230