last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 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 * \file cvrObject.h 00043 * Defines the parent class of most classes in the CVR-Lib 00044 * \author Pablo Alvarado 00045 * \date 01.04.99 00046 * 00047 * $Id: cvrObject.h,v 1.7 2005/03/29 13:09:52 doerfler Exp $ 00048 */ 00049 00050 #ifndef _CVR_OBJECT_H_ 00051 #define _CVR_OBJECT_H_ 00052 00053 #include <string> 00054 00055 /** 00056 * Macro used to simplify the efficient "computation" of the class name 00057 */ 00058 #undef _CVR_RETURN_CLASS_NAME 00059 #define _CVR_RETURN_CLASS_NAME \ 00060 static const std::string theName = this->buildName(); \ 00061 return theName; 00062 00063 /** 00064 * Namespace in which all classes, functions and constants of the CVR-Lib 00065 * are defined. 00066 */ 00067 namespace cvr { 00068 /** 00069 * Base class for most objects in the CVR-Lib. 00070 * 00071 * All classes inherited from cvr::object have three important methods: 00072 * - name() returns a const reference to a std::string with the name 00073 * of the class (see cvr::className for more information). 00074 * - clone() a new instance of the object is created and a pointer to 00075 * this instance is returned. The internal state of the instance is 00076 * a copy of the original. 00077 * - newInstance() a new instance of the same object class is created 00078 * using the default constructor. 00079 * 00080 * Note that there are many classes in the library which are not inherited 00081 * from this class, but most complex objects are (functors, classifiers, 00082 * containers, etc.). The simplest decision rule to decide if a class 00083 * should inherit from cvr::object is the question, should the objects be 00084 * "clonable"? 00085 * 00086 * The CVR-Libmakes extensive use of the C++ feature which allows to change 00087 * the type of overloaded virtual methods. This feature was not supported 00088 * in old compilers like Visual C++ 6.0 or early GCC compilers, and is a 00089 * reason why they are not supported. 00090 * 00091 * In all inherited classes the method name() has to be overloaded. 00092 * The content of those methods can be easily implemented with the 00093 * macro _CVR_RETURN_CLASS_NAME. For example: 00094 * 00095 * \code 00096 * const std::string& yourClass::name() { 00097 * _CVR_RETURN_CLASS_NAME 00098 * } 00099 * \endcode 00100 */ 00101 class object { 00102 public: 00103 /** 00104 * Default constructor. 00105 * 00106 * Initializes the complete library 00107 */ 00108 object(); 00109 00110 /** 00111 * Destructor 00112 */ 00113 virtual ~object(); 00114 00115 /** 00116 * Return the fully qualified name of this class. 00117 * 00118 * Each class has to overload this function and return its name. The 00119 * returned string is system-independent. It uses cvr::className to 00120 * generate the class name but uses an internal singleton to avoid 00121 * building the class name more than once. 00122 * 00123 * Note that the factories of the CVR-Libcan use the name given by 00124 * name() to create instances of a class at runtime (see cvr::factory). 00125 * 00126 * This method must be overloaded in all derived classes. You can 00127 * use the macro _CVR_RETURN_CLASS_NAME to produce the desired 00128 * behaviour. For example: 00129 * 00130 * \code 00131 * const std::string& yourClass::name() { 00132 * _CVR_RETURN_CLASS_NAME 00133 * } 00134 * \endcode 00135 * 00136 * The returned strings do not have any spaces, even if the 00137 * resulting name is not anymore C++ compliant. This rule is 00138 * easier to remember as a rule to specify when the spaces are 00139 * required! For example, a type "cvr::list< cvr::point<double> >" 00140 * will produce the name "cvr::list<cvr::point<double>>". 00141 * Remember that the space between the last two angle brackets "> >" is 00142 * in C++ necessary to avoid syntactic confusion with the 00143 * operator ">>". 00144 * 00145 * @return The fully qualified name of this class without any spaces. 00146 * 00147 * This method replaces the old method getTypeName() in older versions of 00148 * the CVR-Lib. Since the results are totally different, a new name 00149 * has been chosen to make this difference clear! 00150 */ 00151 virtual const std::string& name() const = 0; 00152 00153 /** 00154 * Clone method. 00155 * 00156 * You can create an identical instance of the current cvr::object using 00157 * this method. 00158 * 00159 * The difference with the method newInstance() is that here the internal 00160 * state is kept in the copies while newInstance() creates always an 00161 * object with the default parameters. 00162 * 00163 * Usually, the implementation of the overloaded method in an inherited 00164 * class will make use of the copy constructor: 00165 * \code 00166 * yourClass* yourClass::clone() const { 00167 * return new yourClass(*this); 00168 * } 00169 * \endcode 00170 * 00171 * @return a pointer to the new instance. 00172 * 00173 * \warning You have to take care of the memory management of the returned 00174 * instance and delete it when you don't need it any more. Otherwise you 00175 * will have some memory leaks. 00176 */ 00177 virtual object* clone() const = 0; 00178 00179 /** 00180 * New instance method. 00181 * 00182 * You can create a new instance of the current object, using the 00183 * default constructor. 00184 * 00185 * Note that the difference with the clone() method is that the latter 00186 * also copies the internal state of the object, while this method just 00187 * uses the default constructor. 00188 * 00189 * Usually, the implementation of the overloaded method in an inherited 00190 * class will make use of the default constructor: 00191 * \code 00192 * yourClass* yourClass::newInstance() const { 00193 * return new yourClass(); 00194 * } 00195 * \endcode 00196 * 00197 * @return a pointer to the new instance. 00198 * 00199 * \warning You have to take care of the memory management of the returned 00200 * instance and delete it when you don't need it any more. Otherwise you 00201 * will have some memory leaks. 00202 */ 00203 virtual object* newInstance() const = 0; 00204 00205 protected: 00206 00207 /** 00208 * Build for the name of the current class. 00209 * 00210 * Note that the same string is returned by the method name(), but 00211 * in a MUCH more efficient way. This method has to demangle the 00212 * output of typeid().name() which takes some time. The method 00213 * cvr::object::name() calls the buildName() only once and stores 00214 * the result in a static variable which is just returned in 00215 * successive calls, taking much less time. 00216 * 00217 * You should NEVER EVER use this method directly, use name() instead. 00218 */ 00219 virtual std::string buildName() const; 00220 }; 00221 00222 /** 00223 * Check if the given pointer is NULL 00224 */ 00225 inline bool isNull(const void* p) {return p == 0;} 00226 00227 /** 00228 * Check if the given pointer is not NULL 00229 */ 00230 inline bool notNull(const void* p) {return p != 0;} 00231 00232 } 00233 00234 #endif