Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

Factory.h

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile: Factory.h,v $
00010  * Date modified: $Date: 2003/04/24 19:09:53 $
00011  * Version:       $Revision: 1.7 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #ifndef _VPR_FACTORY_H
00043 #define _VPR_FACTORY_H
00044 
00045 #include <map>
00046 
00047 
00048 namespace vpr
00049 {
00050 
00051 template <class IdentifierType, class AbstractProduct>
00052 struct NullFactoryError
00053 {
00054    static AbstractProduct* onUnknownType(IdentifierType)
00055    {
00056       return NULL;
00057    }
00058 };
00059 
00061 template<class AbstractProduct, class ConcreteProduct>
00062 AbstractProduct* CreateProduct()
00063 { return (new ConcreteProduct); }
00064 
00079 template<
00080    class AbstractProduct,
00081    class IdentifierType,
00082    typename ProductCreator = AbstractProduct* (*)(),
00083       class FactoryErrorPolicy = NullFactoryError<IdentifierType, AbstractProduct>
00084 >
00085 class Factory : public FactoryErrorPolicy
00086 {
00087 public:
00088    bool registerCreator(const IdentifierType& id, ProductCreator creator)
00089    {
00090       // XXX: It would probably be better to use CreatorMap::value_type here.
00091       std::pair<const IdentifierType, ProductCreator> p = std::make_pair(id, creator);
00092       return mCreatorMap.insert(p).second;
00093    }
00094 
00095    bool unregisterCreator(const IdentifierType& id)
00096    {
00097       return (mCreatorMap.erase(id) == 1);   // return (num_erased == 1)
00098    }
00099    bool isRegistered(const IdentifierType& id)
00100    {
00101       return ( mCreatorMap.find(id) != mCreatorMap.end());
00102    }
00103 
00104    AbstractProduct* createObject(const IdentifierType& id)
00105    {
00106       typename CreatorMap::const_iterator i = mCreatorMap.find(id);
00107       if(i != mCreatorMap.end())
00108       {
00109          return (i->second)();
00110       }
00111 
00112       return onUnknownType(id);     // Calls template method from FactoryErrorPolicy<>
00113    }
00114 
00115 
00116 
00117 protected:
00118    typedef std::map<IdentifierType, ProductCreator> CreatorMap;
00119    CreatorMap mCreatorMap;
00120 };
00121 
00122 }; // namespace vpr
00123 
00124 
00125 #endif
00126 

Generated on Sun May 2 14:43:01 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002