Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

pgfactory.h

Go to the documentation of this file.
00001 /*
00002     ParaGUI - crossplatform widgetset
00003     Copyright (C) 2000-2004  Alexander Pipelka
00004  
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009  
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014  
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  
00019     Alexander Pipelka
00020     pipelka@teleweb.at
00021  
00022     Last Update:      $Author: mbickel $
00023     Update Date:      $Date: 2007-04-13 16:15:56 $
00024     Source File:      $Source: /home/cvspsrv/cvsroot/games/asc/source/libs/paragui/include/pgfactory.h,v $
00025     CVS/RCS Revision: $Revision: 1.2 $
00026     Status:           $State: Exp $
00027 */
00028 
00029 #ifndef PG_FACTORY_H
00030 #define PG_FACTORY_H
00031 
00032 #include <map>
00033 #include <string>
00034 
00035 #include "pgsingleton.h"
00036 
00042 class PG_Widget;
00043 
00044 template< class T, class PT = PG_Widget >
00045 class PG_FactoryObject {
00046 public:
00047         static T* CreateObject(PT* parent) {
00048                 return new T(parent);
00049         }
00050 
00051         static T* CreateObject0() {
00052                 return new T;
00053         }
00054 };
00055 
00056 template<class H>
00057 class PG_FactoryHolder : public PG_Singleton< PG_FactoryHolder<H> > {
00058 public:
00059 
00060         typedef PG_Widget* (*CREATEFN)(PG_Widget* parent);
00061 
00062         template< class T, class PT >
00063         static void RegisterClass(const H& classname) {
00064                 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T, PT>::CreateObject);
00065         }
00066 
00067         template< class T >
00068         static void RegisterClass(const H& classname) {
00069                 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T>::CreateObject);
00070         }
00071 
00072         template< class T >
00073         static void RegisterClass0(const H& classname) {
00074                 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T>::CreateObject0);
00075         }
00076 
00077         static PG_Widget* CreateObject(const H& classname, PG_Widget* parent = NULL) {
00078                 CREATEFN create = PG_Singleton< PG_FactoryHolder<H> >::GetInstance().creator_map[classname];
00079 
00080                 if(create == NULL) {
00081                         return NULL;
00082                 }
00083 
00084                 return create(parent);
00085         }
00086 
00087         static PG_Widget* CreateObject0(const H& classname, PG_Widget* parent = NULL) {
00088                 CREATEFN create = PG_Singleton< PG_FactoryHolder<H> >::GetInstance().creator_map[classname];
00089 
00090                 if(create == NULL) {
00091                         return NULL;
00092                 }
00093 
00094                 return create(parent);
00095         }
00096 
00097 protected:
00098 
00099         inline void RegisterCreateFn(const H& classname, CREATEFN fn) {
00100                 creator_map[classname] = fn;
00101         }
00102 
00103         std::map< H, CREATEFN > creator_map;
00104 
00105         friend class PG_Singleton< PG_FactoryHolder<H> >;
00106 
00107 };
00108 
00109 typedef PG_FactoryHolder<std::string> PG_Factory;
00110 
00111 #endif // PG_FACTORY_H

Generated on Tue Jun 24 01:27:47 2008 for Advanced Strategic Command by  doxygen 1.4.2