latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiObjectFactory.h 00027 * authors ....: Jochen Wickel 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 28.6.2000 00030 * revisions ..: $Id: ltiObjectFactory.h,v 1.3 2006/02/07 18:09:32 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_OBJECT_FACTORY 00034 #define _LTI_OBJECT_FACTORY 00035 00036 #include "ltiObject.h" 00037 #include "ltiMutex.h" 00038 #include "ltiClassName.h" 00039 #include <map> 00040 #include <string> 00041 00042 namespace lti { 00043 00044 /** 00045 * This class defines a factory for objects. 00046 * The instantiation of this class will fail if the template 00047 * argument T is not a class which defines the clone() method. 00048 * Usually, you will use an instance of this class as a class variable 00049 * (C jargon: static class member). 00050 * This class is thread-safe. 00051 * 00052 * Please note that the C-type lists used in the constructor must be 00053 * null-terminated. 00054 */ 00055 template <class T> 00056 class objectFactory: public object { 00057 00058 public: 00059 /** 00060 * Constructor. Creates an object factory which is able to 00061 * handle the types whose names and prototypes are given in these 00062 * two tables. <code>names</code> is an array of (char*) which must be 00063 * terminated by a null pointer. <code>objs</code> is a corresponding array 00064 * of object pointers, and must also be null-terminated. 00065 * Example: 00066 * \code 00067 * const char* names[]={ "object1", "object2", "object3", 0 }; 00068 * const obj_type* prototypes[] = { 00069 * new object1(), new object2(), new object3(), 0 }; 00070 * objectFactor<obj_type> t(names,prototypes); 00071 * \endcode 00072 * @param names array of pointers to const char buffers. It must be 00073 * null-terminated 00074 * @param objs array of pointers to const objects. 00075 * It must be null-terminated. 00076 */ 00077 objectFactory(const char** names, 00078 const T* const* objs); 00079 00080 /** 00081 * Constructor. 00082 * Creates an object factory which is able to 00083 * handle the types whose prototypes are given in this 00084 * table. The parameter is a null-terminated array of 00085 * pointers. 00086 * 00087 * The names of the prototypes are determined via the 00088 * className functor. 00089 * 00090 * Example: 00091 * 00092 * \code 00093 * const obj_type* prototypes[] = { 00094 * new object1(), new object2(), new object3(), 0 }; 00095 * objectFactor<obj_type> t(prototypes); 00096 * \endcode 00097 * @param objs array of pointers to const objects. It must be 00098 * null-terminated. 00099 */ 00100 objectFactory(const T* const* objs); 00101 00102 /** 00103 * Destructor. 00104 */ 00105 virtual ~objectFactory(); 00106 00107 /** 00108 * returns the name of this type ("objectFactory") 00109 */ 00110 virtual const char* getTypeName() const; 00111 00112 /** 00113 * Creates a new instance of the class whose name is given 00114 * as parameter, if this class is known to the factory. 00115 * Otherwise, it returns null. 00116 * @param name name of the to-be instantiated class. 00117 * @return a new instance. 00118 */ 00119 virtual T* newInstance(const char *name) const; 00120 00121 /** 00122 * Creates a new instance of the class whose name is given 00123 * as parameter, if this class is known to the factory. 00124 * Otherwise, it returns null. 00125 * @param name name of the to-be instantiated class. 00126 * @return a new instance. 00127 */ 00128 virtual T* newInstance(const std::string& name) const { 00129 return newInstance(name.c_str()); 00130 } 00131 00132 private: 00133 // locks the table for concurrent accesses 00134 mutable mutex lock; 00135 00136 // definition for the map from class names to prototypes 00137 // this previously used a const string as key, which makes sense, 00138 // but unfortunately is not ANSI compliant 00139 typedef std::map<std::string, const T*> classMap; 00140 00141 // the the map from class names to prototypes 00142 classMap objMap; 00143 00144 // the functor which gets the default class names 00145 className cn; 00146 00147 }; 00148 00149 } 00150 00151 #include "ltiObjectFactory_template.h" 00152 00153 #endif