CVR-Lib last update 20 Sep 2009

cvrFactory.h

Go to the documentation of this file.
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 /**
00043  * \file   cvrFactory.h
00044  *         Contains the template type for CVR-Lib factories.
00045  * \author Gustavo Quiros
00046  * \date   08.07.2004
00047  *
00048  * $Id:
00049  */
00050 
00051 #ifndef _CVR_FACTORY_H_
00052 #define _CVR_FACTORY_H_
00053 
00054 #include "cvrObject.h"
00055 #include "cvrClassName.h"
00056 #include "cvrMacroSymbols.h"
00057 #include "cvrFactoryReferences.h"
00058 
00059 #include <map>
00060 #include <string>
00061 
00062 #include <typeinfo>
00063 
00064 /**
00065  * Helper macro to register a class in a factory at compile time. It must be
00066  * used in a global context (outside of any function or class), and should be
00067  * compiled only once, which means this must be in your .cpp file and never in
00068  * a header!
00069  *
00070  * Some scripts parse these entries in the cpp files of the library sources, so
00071  * please ensure that your registration occupies only one line.  You can use
00072  * this macro in your own code with no restrictions.
00073  *
00074  * Usage: _CVR_REGISTER_IN_FACTORY("base class name","class name");
00075  * Example: _CVR_REGISTER_IN_FACTORY(cvr::object,cvr::derived);
00076  */
00077 #define _CVR_REGISTER_IN_FACTORY(BASE,CLASS) \
00078 factory<BASE>::registration<CLASS> \
00079 _CVR_FACTORY_REGISTRATION_ ## BASE ## _ ## CLASS ## _;
00080 
00081 /**
00082  * Helper macro to register a class in a factory at compile time, where the
00083  * base class and the inherited one are both templates of a given type. It must
00084  * be used in a global context (outside of any function or class), and should
00085  * be compiled only once, which means this must be in your .cpp file and never
00086  * in a header!
00087  *
00088  * Some scripts parse these entries in the cpp files of the library sources, so
00089  * please ensure that your registration occupies only one line.  You can use
00090  * this macro in your own code with no restrictions.
00091  *
00092  * Usage: _CVR_REGISTER_IN_TEMPLATE_FACTORY("template type",
00093  *                                          "base class name","class name");
00094  * Example: _CVR_REGISTER_IN_FACTORY(float,cvr::parent,cvr::derived);
00095  */
00096 #define _CVR_REGISTER_IN_TEMPLATE_FACTORY(TYPE,BASE,CLASS) \
00097 factory< BASE<TYPE> >::registration< CLASS<TYPE> > \
00098 _CVR_FACTORY_REGISTRATION_ ## BASE ## _ ## CLASS ## _ ## TYPE ##;
00099 
00100 /**
00101  * Helper macro to register a class in a factory at compile time, under a given
00102  * name. It must be used in a global context (outside of any function or
00103  * class), and should be compiled only once, which means this must be in your
00104  * .cpp file and never in a header!
00105  *
00106  * Some scripts parse these entries in the cpp files of the library sources, so
00107  * please ensure that your registration occupies only one line.  You can use
00108  * this macro in your own code with no restrictions.
00109  *
00110  * The given name must not contain spaces, and you give it without any quotes.
00111  *
00112  * Usage: _CVR_REGISTER_IN_FACTORY_AS("base class name","class name");
00113  * Example: _CVR_REGISTER_IN_FACTORY_AS(object,cvr::object,cvr::derived);
00114  */
00115 #define _CVR_REGISTER_IN_FACTORY_AS(NAME,BASE,CLASS) \
00116 factory<BASE>::registration<CLASS> \
00117 _CVR_FACTORY_REGISTRATION_ ## NAME ## _ ## BASE ## _ ## CLASS ## _(# NAME);
00118 
00119 /**
00120  * Helper macro to register a class in a factory at compile time, under a given
00121  * name. It must be used in a global context (outside of any function or
00122  * class), and should be compiled only once, which means this must be in your
00123  * .cpp file and never in a header!
00124  *
00125  * This macro allows to provide a name for the static variable, in those cases
00126  * where nested classes, template classes or other type of classes do not allow
00127  * an automatic generation of the name
00128  *
00129  * Some scripts parse these entries in the cpp files of the library sources, so
00130  * please ensure that your registration occupies only one line.  You can use
00131  * this macro in your own code with no restrictions.
00132  *
00133  * The given name must not contain spaces, and you give it without any quotes.
00134  * 
00135  * Usage: _CVR_REGISTER_IN_FACTORY_AS("base class name","class name");
00136  * Example: _CVR_REGISTER_IN_FACTORY_AS(object,cvr::object,cvr::derived);
00137  */
00138 #define _CVR_REGISTER_IN_FACTORY_AS_VAR(NAME,BASE,CLASS,VAR) \
00139 factory<BASE>::registration<CLASS> \
00140 _CVR_FACTORY_REGISTRATION_ ## VAR ## _(# NAME);
00141 
00142 
00143 namespace cvr {
00144 
00145   /**
00146    * Factory of objects of base type T.
00147    *
00148    * There is a singleton instance of the factory for each base type T used,
00149    * accessible through the getFactory() static method. Objects can be
00150    * registered at compile time by the use of the _CVR_REGISTER_IN_FACTORY
00151    * macro, or dynamically at runtime with the registerObject() method. An
00152    * object is registered under a string name, usually the fully qualified
00153    * name of the class (obtained dynamically from RTTI using the cvr::object
00154    * method name(), or the cvr::className). However, registerObject() also
00155    * allows the registration of an object under an arbitrary name.
00156    *
00157    * If you use the macros _CVR_REGISTER_IN_FACTORY*, please be sure
00158    * that you write the registration in one single line, as those lines
00159    * are parsed automatically to force the necessary static references.
00160    *
00161    * New objects are created using the newInstance() methods. It takes a name,
00162    * and uses the object registered under this name as a template, calling its
00163    * clone method (which must be defined).
00164    *
00165    * Currently it is not allowed to register an object under an already
00166    * registered name (the names have to be unique), nor to de-register an
00167    * object.
00168    *
00169    * \section linkproblems Linkers and factory problems.
00170    *
00171    * The chosen factory architecture in the CVR-Lib allows to register objects
00172    * in the factory in a very flexible way.  However, most linkers eliminate
00173    * all references to classes that are not explicitly used, which affects
00174    * directly the factories, as the code generated by the compiler, which
00175    * should make the class registration in the factory is also removed.  To
00176    * solve this problem in a relatively standard form, there are two files
00177    * cvrFactoryReferences.h and cvrFactoryReferences.cpp which ensure that
00178    * whenever a factory is used, all registered objects in the factory are also
00179    * referenced and therefore not eliminated.
00180    *
00181    * Please note that you can register your own classes in the factories, but,
00182    * for them to work, you have to ensure that the linker will not eliminate
00183    * your classes.  This can be easily accomplished creating a dummy function
00184    * or class with references to your classes, which is exactly the task of
00185    * cvrFactoryReferences.*.
00186    *
00187    * The references in cvrFactoryReferences.cpp are created automatically by
00188    * the Makefile that compiles the library.
00189    */
00190   template <typename T>
00191   class factory {
00192     friend class registration;
00193   public:
00194     /**
00195      * Base type of the instances in this factory
00196      */
00197     typedef T base_type;
00198 
00199     /**
00200      * Registers an object under the given name.
00201      *
00202      * Note that exactly the given instance will be kept.  This means you
00203      * should never ever delete the given object.  This factory will take care
00204      * of that at the proper time.
00205      */
00206     void registerObject(const base_type* obj, const char *name);
00207 
00208     /**
00209      * Registers an object under the given name.
00210      *
00211      * Note that exactly the given instance will be kept.  This means you
00212      * should never ever delete the given object.  This factory will take care
00213      * of that at the proper time.
00214      */
00215     void registerObject(const base_type* obj, const std::string& name);
00216 
00217     /**
00218      * Registers an object under its class name (dynamically obtained)
00219      */
00220     void registerObject(const base_type* obj);
00221 
00222     /**
00223      * Creates a new instance, which is a "clone" the object registered
00224      * under the given name.
00225      *
00226      * Returns a null pointer if no such object is registered.
00227      *
00228      * \warning You have to take care of the memory management of the returned
00229      * instance, i.e. you have the remove the object with delete.
00230      */
00231     base_type* newInstance(const char *name) const;
00232 
00233     /**
00234      * Creates a new instance, which is a "clone" the object registered
00235      * under the given name.
00236      *
00237      * Returns a null pointer if no such object is registered.
00238      *
00239      * \warning You have to take care of the memory management of the returned
00240      * instance, i.e. you have the remove the object with delete.
00241      */
00242     base_type* newInstance(const std::string& name) const;
00243 
00244     /**
00245      * Singleton factory access
00246      */
00247     static factory& getFactory();
00248 
00249     /**
00250      * Get a list with all registered names.
00251      *
00252      * The map contains pairs of strings, where the first string contains the
00253      * registered name and the second one the name of the class as reported by
00254      * cvr::className.
00255      */
00256     void getRegisteredNames(std::map<std::string,std::string> & names) const;
00257 
00258     /**
00259      * Represents the registration of an object of type U, constructed with the
00260      * default parameterless constructor, and under its class name.
00261      */
00262     template <typename U>
00263     class registration {
00264     public:
00265       /**
00266        * Creates a new registration, thus registering an object of class U.
00267        */
00268       registration() {
00269         U* u=new U;
00270         base_type* tmp=getFactory().newInstance(u->name());
00271         if (isNull(tmp)) {
00272           getFactory().registerObject(u);
00273         } else {
00274           delete u;
00275           delete tmp;
00276         }
00277       }
00278 
00279       /**
00280        * Creates a new registration, thus registering an object of class U,
00281        * but under a given name, which is not necessarily the class name.
00282        */
00283       registration(const char* name) {
00284         base_type* tmp=getFactory().newInstance(name);
00285         if (isNull(tmp)) {
00286           U* u=new U;
00287           getFactory().registerObject(u,name);
00288         } else {
00289           delete tmp;
00290         }
00291       }
00292     };
00293 
00294   private:
00295     /**
00296      * The type of the object map
00297      */
00298     typedef std::map<std::string,const base_type*> object_map_type;
00299 
00300     /**
00301      * The object map. Maps names to instances.
00302      */
00303     object_map_type objectMap_;
00304 
00305     /**
00306      * Disable the default constructor.
00307      */
00308     factory();
00309 
00310     /**
00311      * Disable the copy constructor.
00312      */
00313     factory(const factory& f);
00314 
00315     /**
00316      * The compiler has to be able to destroy the factory
00317      */
00318     ~factory();
00319 
00320     /**
00321      * Disable the assignment operator.
00322      */
00323     factory& operator=(const factory& f);
00324 
00325     /**
00326      * Force static references to allow all library classes to be considered
00327      * even if the user of the library never makes an explicit reference to one
00328      * of them.
00329      */
00330     void ensureReferences() const;
00331   };
00332 
00333 }
00334 
00335 #include "cvrFactory_template.h"
00336 
00337 #endif

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8