last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2005 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrParametersManager.h 00044 * Contains the class cvr::parametersManager, which provides a standard 00045 * functionality for all classes that manage parameters (e.g. functors, 00046 * viewers, painters, classifiers, etc.) 00047 * \author Pablo Alvarado 00048 * \date 06.01.2005 00049 * 00050 * $Id: cvrParametersManager.h,v 1.7 2007/10/07 03:17:00 alvarado Exp $ 00051 */ 00052 00053 #ifndef _CVR_PARAMETERS_MANAGER_H_ 00054 #define _CVR_PARAMETERS_MANAGER_H_ 00055 00056 #include "cvrIoObject.h" 00057 #include "cvrInvalidParametersException.h" 00058 #include "cvrIoHandler.h" 00059 #include "cvrStatus.h" 00060 00061 namespace cvr { 00062 00063 /** 00064 * Base class for all classes with parameters. 00065 * 00066 * Many classes in the CVR-Libmake use of internal parameter 00067 * objects. This has proven to be a very flexible concept, not only 00068 * useful in the serialization of the configuration of algorithms 00069 * used, but also extremely practical in the reuse of functors and 00070 * parameterizable objects within other parameterizable objects. 00071 * 00072 * The parameters can then be set in this classes with help of 00073 * several methods, where the setParameters() is the most important. 00074 * 00075 * @see parametersManager::parameters 00076 * 00077 * \ingroup gInterfaces 00078 */ 00079 class parametersManager { 00080 public: 00081 00082 /** 00083 * Base class for all lti parameter objects 00084 */ 00085 class parameters : public ioObject { 00086 public: 00087 /** 00088 * Default constructor 00089 */ 00090 parameters(); 00091 00092 /** 00093 * Copy constructor 00094 */ 00095 parameters(const parameters& other); 00096 00097 /** 00098 * Destructor 00099 */ 00100 virtual ~parameters(); 00101 00102 /** 00103 * Copy data of "other" parameters 00104 */ 00105 parameters& copy(const parameters& other); 00106 00107 /** 00108 * Returns the name of this class 00109 */ 00110 virtual const std::string& name() const = 0; 00111 00112 /** 00113 * Returns a pointer to a clone of the parameters. 00114 */ 00115 virtual parameters* clone() const = 0; 00116 00117 /** 00118 * Returns a pointer to a clone of the parameters. 00119 */ 00120 virtual parameters* newInstance() const = 0; 00121 00122 /** 00123 * Write the parameters in the given ioHandler 00124 * @param handler the ioHandler to be used 00125 * @param complete if true (the default) the enclosing begin/end will 00126 * be also written, otherwise only the data block will be written. 00127 * @return true if write was successful 00128 */ 00129 virtual bool write(ioHandler& handler, 00130 const bool complete=true) const; 00131 00132 /** 00133 * Read the parameters from the given ioHandler 00134 * @param handler the ioHandler to be used 00135 * @param complete if true (the default) the enclosing begin/end will 00136 * be also written, otherwise only the data block will be written. 00137 * @return true if write was successful 00138 */ 00139 virtual bool read(ioHandler& handler,const bool complete=true); 00140 00141 protected: 00142 /** 00143 * Copy data of "other" parameters 00144 */ 00145 parameters& operator=(const parameters& other); 00146 }; 00147 00148 /** 00149 * Default constructor 00150 */ 00151 parametersManager(); 00152 00153 /** 00154 * Copy constructor 00155 */ 00156 parametersManager(const parametersManager& other); 00157 00158 /** 00159 * Destructor 00160 */ 00161 virtual ~parametersManager(); 00162 00163 /** 00164 * Set parametersManager's parameters. 00165 * 00166 * This member makes a copy of \a theParam: the parametersManager 00167 * will keep its own copy of the parameters! 00168 * 00169 * Additionally, the updateParameters will be called, which in 00170 * some classes is used to initialize some Look-Up Tables, or 00171 * filters, etc., in order to improve the execution times later in 00172 * the apply methods. 00173 * 00174 * Since the CVR-Libfollows a deep-copy philosophy, if you copy 00175 * the functor or other from parametersManager inherited class, 00176 * the copy will create an exact copy of the parameters, no matter 00177 * if they have been specified with useParameters() or with 00178 * setParameters(). 00179 * 00180 * You should always prefer this method over useParameters(). The 00181 * latter one is used only in very special cases, when the user 00182 * needs to control an instance of parameters outside the 00183 * functor/classifier or other inherited class, and he/she wants 00184 * to take care of the memory management. 00185 * 00186 * This method should never be overloaded. If you want to make some 00187 * precomputations when setting the parameters, please overload 00188 * updateParameters() instead. 00189 * 00190 * @see useParameters(),updateParameters() 00191 * 00192 * @return true if successful, false otherwise 00193 */ 00194 virtual bool setParameters(const parameters& theParam); 00195 00196 /** 00197 * Use the given parameters exactly as they are. 00198 * 00199 * The difference of this method with setParameters() is that the 00200 * parameters will \b not be copied. They will be used exactly as 00201 * they are given. It is therefore your responsability to ensure 00202 * that the parameters instance you give exists as long as the 00203 * parameterManager instace is being used. 00204 * 00205 * Of course, if the parameters content is changed while the 00206 * derived functor/classifier makes its computations, 00207 * unpredictable behaviour has to be expected. It is also your 00208 * responsibility to take care of all multi-threading details. 00209 * 00210 * Since the CVR-Libfollows a deep-copy philosophy, if you copy 00211 * the classes inherited from parametersManager, the copy will 00212 * create an exact copy of the parameters, no matter if they have 00213 * been specified with useParameters() or with setParameters(). 00214 * 00215 * The manager will not administrate the instance given through 00216 * this method. If you create it with new, you have to take care 00217 * about the deletion. 00218 * 00219 * If you are not sure what to use, always prefer setParameters() 00220 * since it is more secure than useParameters(), as the 00221 * parametersManager will take care of all memory issues. 00222 * 00223 * This method always calls updateParameters(). 00224 * 00225 * \warning Use this method only if you really know what you are doing. 00226 * If you are not sure, use setParameters() instead. 00227 * 00228 * Note that the given parameters instance can be altered in the 00229 * derived classes, specially if some non-const shortcuts are used 00230 * or the read methods are called (another reason to prefer 00231 * setParameters()). 00232 * 00233 * @return true if successful, false otherwise 00234 */ 00235 virtual bool useParameters(parameters& theParam); 00236 00237 /** 00238 * Use the given parameters as they are but take control of the memory 00239 * management. 00240 * 00241 * This method is very similar to useParameters() and therefore you must 00242 * pay attention to all warnings specified in the documentation of that 00243 * method. The main difference is that this method takes control of the 00244 * memory management of the given parameters instance. In other words, 00245 * this method is used in extremely time critical situations when you must 00246 * avoid that the parameters are copied, but you also want this manager 00247 * to administrate the memory allocation for the given object. 00248 * 00249 * This method always calls updateParameters(), task that will determine if 00250 * here it is reported success or not. This means, even if the method 00251 * returns false, the instance will have acquired control of the parameters 00252 * instance and you must not remove the given instance yourself. 00253 * 00254 * \warning Use this method only if you really know what you are doing. 00255 * If you are not sure, use setParameters() instead. 00256 * 00257 * @return true if successful, false otherwise 00258 */ 00259 virtual bool attachParameters(parameters& theParam); 00260 00261 00262 /** 00263 * Update parameters. 00264 * 00265 * Some functors (or other classes inherited from 00266 * parametersManagers) make some pre-computations when the 00267 * parameters are set. For example, they initialize look-up 00268 * tables, build some filter kernels, initialize some data 00269 * structures, etc. This job is always done by updateParameters(). 00270 * 00271 * In principle, you only need to overload this method if you need such 00272 * initializations, as setParameters() and useParameters() will always 00273 * call this method. 00274 * 00275 * For disobedient people, who ignore the recomendation of using 00276 * setParameters() over useParameters(), this method should be 00277 * called if attributes of the parameters instance are changed, 00278 * before calling the apply() method. Otherwise, the internal 00279 * functor state will be inconsistent with the parameters. 00280 * 00281 * The useParameters() interface is provided to solve some 00282 * critical efficiency issues in very complex functor structures 00283 * and helping in the implementation of GUI based tools, in which you maybe 00284 * want to change parameters on-line in an efficient way. 00285 * 00286 * \warning If you call an apply() method of a functor or other 00287 * parameter dependent methods in classifiers, in which you 00288 * changed the attributes of the parameters instance without 00289 * updating them, you have to expect an unpredictible behaviour: 00290 * segmentation fault, system crash, blue screens, etc. can occur 00291 * as the sizes of internal structures may not be consistent with 00292 * the given parameters. This problem does not exist if you 00293 * indicate the parameters to be used with setParameters(), as the 00294 * parametersManager will manage an instance of its own. 00295 * 00296 * A typical updateParameters() reimplementation will look something like 00297 * \code 00298 * bool yourClass::updateParameters() { 00299 * // get the set of parameters just indicated by the user 00300 * const parameters& par = getParameters(); 00301 * 00302 * // do whatever you need to initialize with par 00303 * bool success=true; 00304 * ... 00305 * // return true only if everything was ok. 00306 * return success; 00307 * } 00308 * \endcode 00309 * 00310 * The default implementation just returns true if the parameters have been 00311 * set before, which is always the normal case. 00312 */ 00313 virtual bool updateParameters(); 00314 00315 /** 00316 * Returns current parameters. 00317 */ 00318 const parameters& getParameters() const; 00319 00320 /** 00321 * Returns true if the parameters are valid 00322 */ 00323 virtual bool validParameters() const; 00324 00325 /** 00326 * Copy data of "other" parametersManager. 00327 * Please note that the status string will _NOT_ be copied! 00328 */ 00329 parametersManager& copy(const parametersManager& other); 00330 00331 /** 00332 * Write the parametersManager in the given ioHandler. The default 00333 * implementation is to write just the parameters object. 00334 * 00335 * @param handler the ioHandler to be used 00336 * @param complete if true (the default) the enclosing begin/end will 00337 * be also written, otherwise only the data block will be written. 00338 * @return true if write was successful 00339 */ 00340 virtual bool write(ioHandler& handler, 00341 const bool complete=true) const; 00342 00343 /** 00344 * Read the parametersManager from the given ioHandler. 00345 * 00346 * The default implementation is to read just the parameters object. 00347 * 00348 * Since this virtual method needs to know the exact type of the 00349 * parameters to call the proper read method, it will just assume 00350 * that the current parametersManager instance has a valid, consistent 00351 * parameter set. If this is not the case, you need to 00352 * reimplement the read method to set first a dummy parameter 00353 * object or the correct type. 00354 * 00355 * @param handler the ioHandler to be used 00356 * @param complete if true (the default) the enclosing begin/end will 00357 * be also written, otherwise only the data block will be written. 00358 * @return true if write was successful 00359 */ 00360 virtual bool read(ioHandler& handler,const bool complete=true); 00361 00362 protected: 00363 /** 00364 * Returns current parameters. (non const! -> protected) 00365 */ 00366 parameters& getParameters(); 00367 00368 private: 00369 00370 /** 00371 * This is private to avoid default implementation. 00372 * 00373 * alias for copy (MUST be reimplemented by each class or an 00374 * exception will be thrown!) 00375 */ 00376 parametersManager& operator=(const parametersManager& other); 00377 00378 /** 00379 * Current parameters. 00380 */ 00381 parameters* params_; 00382 00383 /** 00384 * Flag that indicates if the parametersManager owns the parameters or not 00385 */ 00386 bool ownParams_; 00387 00388 }; 00389 } // namespace cvr 00390 00391 #endif 00392