Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

SingletonRegistry.h (Maintainer: Clemens Gröpl, Chris Bielow)

Go to the documentation of this file.
00001   // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Clemens Gröpl, Chris Bielow  $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_CONCEPT_SINGLETONREGISTRY_H
00028 #define OPENMS_CONCEPT_SINGLETONREGISTRY_H
00029 
00030 
00031 
00032 #include <map>
00033 #include <iostream>
00034 #include <OpenMS/CONCEPT/FactoryBase.h>
00035 #include <OpenMS/DATASTRUCTURES/String.h>
00036 
00037 namespace OpenMS
00038 {
00039   class String;
00040 
00048   class SingletonRegistry
00049   {
00050     friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
00051 
00052   private:
00054     typedef std::map<String, FactoryBase*> Map;
00055     typedef Map::const_iterator MapIterator;
00056 
00058     virtual ~SingletonRegistry(){}
00059 
00061     SingletonRegistry(){}
00062 
00064     static SingletonRegistry* instance_()
00065     {
00066       if (!singletonRegistryInstance_)
00067       {
00068         singletonRegistryInstance_ = new SingletonRegistry();
00069       }
00070       return singletonRegistryInstance_;
00071     }
00072 
00073   public:
00074     
00076     static FactoryBase* getFactory(const String& name)
00077     {
00078       MapIterator it = instance_()->inventory_.find(name);
00079       if (it != instance_()->inventory_.end())
00080       {
00081         return it->second;
00082       }
00083       else 
00084       {
00085         throw Exception::InvalidValue(__FILE__, __LINE__, __PRETTY_FUNCTION__, "This Factory is not registered with SingletonRegistry!",name.c_str());
00086       }
00087     }
00088     
00095     static void registerFactory(const String& name, FactoryBase* instance)
00096     {
00097       instance_()->inventory_[name] = instance;
00098     }
00099     
00101     static bool isRegistered(String name)
00102     {
00103       if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
00104       {
00105         return true;
00106       }
00107       return false;
00108     }
00109     
00110   private:
00111 
00112     Map inventory_;
00113     static SingletonRegistry* singletonRegistryInstance_;
00114   };
00115   
00116   
00117 }
00118 #endif //OPENMS_CONCEPT_SINGLETONREGISTRY_H

Generated Tue Apr 1 15:36:38 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1