00001 /*---------------------------------------------------------------------* 00002 | Library QgarLib, graphics analysis and recognition | 00003 | Copyright (C) 2002 Qgar Project, LORIA | 00004 | | 00005 | This library is free software; you can redistribute it and/or | 00006 | modify it under the terms of the GNU Lesser General Public | 00007 | License version 2.1, as published by the Free Software Foundation. | 00008 | | 00009 | This library is distributed in the hope that it will be useful, | 00010 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00011 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 00012 | See the GNU Lesser General Public License for more details. | 00013 | | 00014 | The GNU Lesser General Public License is included in the file | 00015 | LICENSE.LGPL, in the root directory of the Qgar packaging. See | 00016 | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. | 00017 | To receive a paper copy, write to the Free Software Foundation, | 00018 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. | 00019 | | 00020 | Contact Project Qgar for any information: | 00021 | LORIA - équipe Qgar | 00022 | B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France | 00023 | email: qgar-contact@loria.fr | 00024 | http://www.qgar.org/ | 00025 *---------------------------------------------------------------------*/ 00026 00027 00028 #ifndef __STL_H_INCLUDED__ 00029 #define __STL_H_INCLUDED__ 00030 00031 00032 /** 00033 * @file stl.H 00034 * @brief Extensions to the STL library. 00035 * 00036 * They include a set of useful STL-based definitions 00037 * of structures and functions. 00038 * 00039 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Philippe Dosch">Philippe Dosch</a> 00040 * @date September 2, 2002 15:56 00041 * @since Qgar 2.1 00042 */ 00043 00044 00045 // For RCS/CVS use: Do not delete 00046 /* $Id: stl.H,v 1.3 2005/10/14 17:05:49 masini Exp $ */ 00047 00048 00049 00050 // STL 00051 #include <functional> 00052 00053 00054 00055 namespace qgar 00056 { 00057 00058 00059 /** 00060 * @ingroup GLOB_STL 00061 * 00062 * @struct qstlDeleteObject stl.H "qgarlib/stl.H" 00063 * 00064 * @brief Struct designed to delete objects. 00065 * 00066 * This struct is designed to delete objects, handled by a pointer, 00067 * through STL algorithms. Below is a typical use of this struct: 00068 * \code 00069 * #include <list> 00070 * #include <algorithm> 00071 * #include <qgarlib/Object.H> 00072 * #include <qgarlib/stl.H> 00073 * 00074 * list<Object*> myList; 00075 * 00076 * ... 00077 * 00078 * // Clean up 00079 * for_each(myList.begin(), myList.end(), qstlDeleteObject()); 00080 * \endcode 00081 * 00082 * For further information, see item #7 in 00083 * [<a href="Bibliography.html#Meyer-2001">Meyer, 2001</a>]. 00084 * 00085 * @author Philippe Dosch 00086 * @date September 2, 2002 15:56 00087 * @since Qgar 2.1 00088 */ 00089 struct qstlDeleteObject 00090 { 00091 00092 /** 00093 * @brief Deletes an object via a passed pointer. 00094 * 00095 * @param ptr pointer to the object to be deleted 00096 */ 00097 template<typename T> 00098 void operator()(const T* ptr) const 00099 { 00100 delete ptr; 00101 } 00102 00103 }; // Struct qstlDeleteObject 00104 00105 00106 00107 /** 00108 * @ingroup GLOB_STL 00109 * 00110 * @struct qstlCloneObject stl.H "qgarlib/stl.H" 00111 * 00112 * @brief Struct designed to clone objects. 00113 * 00114 * This struct is designed to clone objects, handled by a pointer, 00115 * through STL algorithms. Below is a typical use of this struct: 00116 * \code 00117 * #include <list> 00118 * #include <algorithm> 00119 * #include <qgarlib/Object.H> 00120 * #include <qgarlib/stl.H> 00121 * 00122 * list<Object*> myList; 00123 * list<Object*> myCopyList; 00124 * 00125 * ... 00126 * 00127 * // Make a copy of the list 00128 * transform(myList.begin(), myList.end(), 00129 * back_inserter(myCopyList), qstlCloneObject()); 00130 * 00131 * \endcode 00132 * 00133 * For further information, see 00134 * [<a href="Bibliography.html#Meyer-2001">Meyer, 2001</a>]. 00135 * 00136 * @author Philippe Dosch (copy) 00137 * @date September 2, 2002 15:56 00138 * @since Qgar 2.1 00139 */ 00140 struct qstlCloneObject 00141 { 00142 00143 /** 00144 * @brief Duplicates a passed object using its copy constructor. 00145 * 00146 * @param t object to be duplicated 00147 */ 00148 template<typename T> 00149 T* operator()(const T* t) const 00150 { 00151 return new T(*t); 00152 } 00153 00154 }; // Struct qstlCloneObject 00155 00156 00157 00158 /** 00159 * @ingroup GLOB_STL 00160 * 00161 * @class mem_ptr_fun_t stl.H "qgarlib/stl.H" 00162 * 00163 * @brief Adaptor for member functions to a unary_function. 00164 * 00165 * <p> 00166 * qgar::mem_fun_ptr_t is an adaptor for member functions that take one 00167 * argument. If T is some class with a member Result T::f(Arg) (Result 00168 * type can be void, and t an instance of T. Then the 00169 * mem_ptr_fun_t<result, T, Arg> is a valid STL functor that makes 00170 * possible the call of t->f() as if it was a normal function. 00171 * </p> 00172 * <p> 00173 * If F is a qgar::mem_ptr_fun_t that was constructed to use the member 00174 * function T::f, and if t a pointer to an instance of T (T*) and a 00175 * is a value of type Arg, then the expression F(a) is equivalent to 00176 * the expression t->f(a). The difference is simply that F can be 00177 * passed to STL algorithms whose arguments must be function 00178 * objects. 00179 * </p> 00180 * <p> 00181 * It is usually rather inconvienient to use the constructor of 00182 * mem_ptr_fun_t directly. It is better to use the helper function 00183 * qst_mem_ptr_fun instead. 00184 * </p> 00185 * 00186 * @see qgar::qstl_mem_ptr_fun() 00187 * 00188 * @author Jan Rendek 00189 * @date May 28, 2003 16:11 00190 * @since Qgar 2.1 00191 */ 00192 template<typename Result, typename X, typename Arg> 00193 class mem_ptr_fun_t 00194 00195 : public std::unary_function<Arg, Result> 00196 00197 { 00198 00199 public: 00200 00201 /** 00202 * @brief Creates an adapter to a unary member function of an 00203 * instance of class <b>X</b>. 00204 * 00205 * @param inst instance of which the function member will be called 00206 * @param ptr pointer on the function member to be called 00207 */ 00208 mem_ptr_fun_t(X* inst, Result (X::*ptr)(Arg)) 00209 : _inst(inst), _ptr(ptr) 00210 { 00211 /* EMPTY */ 00212 } 00213 00214 /** 00215 * @brief Executes the unary member function passed on creation and 00216 * return the result. 00217 * 00218 * @param a parameter passed to the member function 00219 */ 00220 Result operator()(Arg a) 00221 { 00222 return (_inst->*_ptr)(a); 00223 } 00224 00225 private: 00226 /// Pointer to the instance whose member function is to be called. 00227 X* _inst; 00228 /// Pointer to the member function to be called. 00229 Result (X::*_ptr)(Arg); 00230 00231 }; // Class mem_ptr_fun_t 00232 00233 00234 00235 /** 00236 * @ingroup GLOB_STL 00237 * 00238 * @class const_mem_ptr_fun_t stl.H "qgarlib/stl.H" 00239 * 00240 * @brief Adaptor for const member functions to a unary_function. 00241 * 00242 * <p> 00243 * mem_fun_ptr_t is an adaptor for const member functions that take 00244 * one argument. If T is some class with a member Result T::f(Arg) 00245 * (Result type can be void, and t an instance of T. Then the 00246 * mem_ptr_fun_t<result, T, Arg> is a valid STL functor that makes 00247 * possible the call of t->f() as if it was a normal function. 00248 * </p> 00249 * <p> 00250 * If F is a mem_ptr_fun_t that was constructed to use the member 00251 * function T::f(Arg), and if t a pointer to an instance of T (T*) and 00252 * a is a value of type Arg, then the expression F(a) is equivalent to 00253 * the expression t->f(a). The difference is simply that F can be 00254 * passed to STL algorithms whose arguments must be function 00255 * objects. 00256 * </p> 00257 * <p> 00258 * It is usually rather inconvienient to use the constructor of 00259 * mem_ptr_fun_t directly. It is better to use the helper function 00260 * qst_mem_ptr_fun instead. 00261 * </p> 00262 * 00263 * @see qgar::mem_ptr_fun() 00264 * 00265 * @author Jan Rendek 00266 * @date May 28, 2003 16:11 00267 * @since Qgar 2.1 00268 */ 00269 template<typename Result, typename X, typename Arg> 00270 class const_mem_ptr_fun_t 00271 00272 : public std::unary_function<Arg, Result> 00273 00274 { 00275 00276 public: 00277 00278 /** 00279 * @brief Creates an adapter to a unary member function of an 00280 * instance of class X. 00281 * 00282 * @param inst instance of which the function member will be called 00283 * @param ptr pointer on the function member to be called 00284 */ 00285 const_mem_ptr_fun_t(const X * const inst, Result (X::*ptr)(Arg) const) 00286 : _inst(inst), _ptr(ptr) 00287 { 00288 /* EMPTY */ 00289 } 00290 00291 /** 00292 * @brief Executes the unary member function passed on creation and 00293 * return the result. 00294 * 00295 * @param a parameter passed to the member function 00296 */ 00297 Result operator()(Arg a) const 00298 { 00299 return (_inst->*_ptr)(a); 00300 } 00301 00302 private: 00303 00304 /// Pointer to the instance whose member function is to be called. 00305 X* _inst; 00306 00307 /// Pointer to the member function to be called. 00308 Result (X::*_ptr)(Arg) const; 00309 00310 }; // Class const_mem_ptr_fun_t 00311 00312 00313 00314 /** 00315 * @class mem_ptr_fun2_t stl.H "qgarlib/stl.H" 00316 * @ingroup GLOB_STL 00317 * @brief Adaptor for member functions to a binary_function. 00318 * 00319 * <p> 00320 * mem_fun2_ptr_t is an adaptor for member functions that take two 00321 * arguments. If T is some class with a member Result T::f(Arg1, Arg2) 00322 * (Result type can be void), and t an instance of T. Then the 00323 * mem_ptr_fun2_t<Result, T, Arg1, Arg2> is a valid STL functor that makes 00324 * possible the call of t->f() as if it was a normal function. 00325 * </p> 00326 * <p> 00327 * If F is a mem_ptr_fun_t that was constructed to use the member 00328 * function T::f, and if t a pointer to an instance of T (T*) and a 00329 * is a value of type Arg, then the expression F(a, b) is equivalent to 00330 * the expression t->f(a, b). The difference is simply that F can be 00331 * passed to STL algorithms whose arguments must be function 00332 * objects. 00333 * </p> 00334 * <p> 00335 * It is usually rather inconvienient to use the constructor of 00336 * mem_ptr_fun2_t directly. It is better to use the helper function 00337 * qst_mem_ptr_fun instead. 00338 * </p> 00339 * 00340 * @see qgar::qstl_mem_ptr_fun() 00341 * 00342 * @author Jan Rendek 00343 * @date June 03, 2003 08:41 00344 * @since Qgar 2.1 00345 */ 00346 template<typename Result, typename X, typename Arg1, typename Arg2> 00347 class mem_ptr_fun2_t 00348 00349 : public std::binary_function<Arg1, Arg2, Result> 00350 00351 { 00352 00353 public: 00354 00355 /** 00356 * @brief Creates an adapter to a binary member function 00357 * of an instance of class <b>X</b>. 00358 * 00359 * @param inst instance of which the function member will be called 00360 * @param ptr pointer on the function member to be called 00361 */ 00362 mem_ptr_fun2_t(X * inst, Result (X::*ptr)(Arg1, Arg2)) 00363 : _inst(inst), _ptr(ptr) 00364 { 00365 /* EMPTY */ 00366 } 00367 00368 /** 00369 * @brief Executes the binary member function passed on creation 00370 * and return the result. 00371 * 00372 * @param a first parameter passed to the member function 00373 * @param b second parameter passed to the member function 00374 */ 00375 Result operator()(Arg1 a, Arg2 b) 00376 { 00377 return (_inst->*_ptr)(a, b); 00378 } 00379 00380 private: 00381 00382 /// Pointer to the instance whose member function is to be called. 00383 X* _inst; 00384 00385 /// Pointer to the member function to be called. 00386 Result (X::*_ptr)(Arg1, Arg2); 00387 00388 }; // Class mem_ptr_fun2_t 00389 00390 00391 00392 /** 00393 * @ingroup GLOB_STL 00394 * 00395 * @class const_mem_ptr_fun2_t stl.H "qgarlib/stl.H" 00396 * 00397 * @brief Adaptor for member functions to a binary_function. 00398 * 00399 * <p> 00400 * const_mem_fun2_ptr_t is an adaptor for const member functions 00401 * that take two arguments. If T is some class with a member 00402 * Result T::f(Arg1, Arg2) (Result type can be void), and t an 00403 * instance of T. Then the const_mem_ptr_fun2_t<Result, T, Arg1, Arg2> 00404 * is a valid STL functor that makes possible the call of t->f() as 00405 * if it was a normal function. 00406 * </p> 00407 * <p> 00408 * If F is a const_mem_ptr_fun2_t that was constructed to use the member 00409 * function T::f, and if t a pointer to an instance of T (T*) and a 00410 * is a value of type Arg1, b a value of type Arg2, then the 00411 * expression F(a, b) is equivalent to the expression t->f(a, b). The 00412 * difference is simply that F can be passed to STL algorithms whose 00413 * arguments must be function objects. 00414 * </p> 00415 * <p> 00416 * It is usually rather unconvenient to use the constructor of 00417 * const_mem_ptr_fun2_t directly. It is better to use the helper function 00418 * qst_mem_ptr_fun instead. 00419 * </p> 00420 * 00421 * @see qgar::qstl_mem_ptr_fun() 00422 * 00423 * @author Jan Rendek 00424 * @date June 03, 2003 08:41 00425 * @since Qgar 2.1 00426 */ 00427 template<typename Result, typename X, typename Arg1, typename Arg2> 00428 class const_mem_ptr_fun2_t 00429 00430 : public std::binary_function<Arg1, Arg2, Result> 00431 00432 { 00433 00434 public: 00435 00436 /** 00437 * @brief Creates an adapter to a binary member function 00438 * of an instance of class X. 00439 * 00440 * @param inst instance of which the function member will be called 00441 * @param ptr pointer on the function member to be called 00442 */ 00443 const_mem_ptr_fun2_t(X * inst, Result (X::*ptr)(Arg1, Arg2) const) 00444 : _inst(inst), _ptr(ptr) 00445 { 00446 /* EMPTY */ 00447 } 00448 00449 00450 /** 00451 * @brief Executes the binary member function passed on creation 00452 * and return the result. 00453 * 00454 * @param a first parameter passed to the member function 00455 * @param b second parameter passed to the member function 00456 */ 00457 Result operator()(Arg1 a, Arg2 b) const 00458 { 00459 return (_inst->*_ptr)(a, b); 00460 } 00461 00462 private: 00463 00464 /// A pointer to the instance whose member function is to be called. 00465 X* _inst; 00466 00467 /// A pointer to the member function to be called. 00468 Result (X::*_ptr)(Arg1, Arg2) const; 00469 00470 }; // Class const_mem_ptr_fun2_t 00471 00472 00473 00474 /** 00475 * @ingroup GLOB_STL 00476 * 00477 * @brief Adapts a single argument member function 00478 * to an <b>unary_function</b>. 00479 * 00480 * @param inst instance whose function member is to be called 00481 * @param f pointer on the function member of inst to call 00482 * 00483 * @since Qgar 2.1 00484 */ 00485 template <typename Result, typename X, typename Arg> 00486 inline mem_ptr_fun_t<Result, X, Arg> 00487 qstl_mem_ptr_fun(X* inst, Result (X::*f)(Arg)) 00488 { 00489 return mem_ptr_fun_t<Result, X, Arg>(inst, f); 00490 } 00491 00492 00493 00494 /** 00495 * @ingroup GLOB_STL 00496 * 00497 * @brief Adapts a single argument const member function 00498 * to a <b>unary_function</b>. 00499 * 00500 * @param inst instance whose function member is to be called 00501 * @param f pointer on the function member of inst to call 00502 * 00503 * @since Qgar 2.1 00504 */ 00505 template <typename Result, typename X, typename Arg> 00506 inline const_mem_ptr_fun_t<Result, X, Arg> 00507 qstl_mem_ptr_fun(X* inst, Result (X::*f)(Arg) const) 00508 { 00509 return const_mem_ptr_fun_t<Result, X, Arg>(inst, f); 00510 } 00511 00512 00513 00514 /** 00515 * @ingroup GLOB_STL 00516 * 00517 * @brief Adapts a two arguments member function 00518 * to a <b>binary_function</b>. 00519 * 00520 * @param inst instance whose function member is to be called 00521 * @param f pointer on the function member of inst to call 00522 * 00523 * @since Qgar 2.1 00524 */ 00525 template <typename Result, typename X, typename Arg1, typename Arg2> 00526 inline mem_ptr_fun2_t<Result, X, Arg1, Arg2> 00527 qstl_mem_ptr_fun(X* inst, Result (X::*f)(Arg1, Arg2)) 00528 { 00529 return mem_ptr_fun2_t<Result, X, Arg1, Arg2>(inst, f); 00530 } 00531 00532 00533 00534 /** 00535 * @ingroup GLOB_STL 00536 * 00537 * @brief Adapts a two arguments member function 00538 * to a <b>binary_function</b>. 00539 * 00540 * @param inst instance whose function member is to be called 00541 * @param f pointer on the function member of inst to call 00542 * 00543 * @since Qgar 2.1 00544 */ 00545 template <typename Result, typename X, typename Arg1, typename Arg2> 00546 inline const_mem_ptr_fun2_t<Result, X, Arg1, Arg2> 00547 qstl_mem_ptr_fun(X* inst, Result (X::*f)(Arg1, Arg2) const) 00548 { 00549 return const_mem_ptr_fun2_t<Result, X, Arg1, Arg2>(inst, f); 00550 } 00551 00552 00553 00554 } // namespace qgar 00555 00556 00557 #endif /* __STL_H_INCLUDED__ */