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

Factory.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Chris Bielow $
00032 // $Authors: Chris Bielow $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_CONCEPT_FACTORY_H
00036 #define OPENMS_CONCEPT_FACTORY_H
00037 
00038 #include <OpenMS/CONCEPT/Exception.h>
00039 #include <OpenMS/CONCEPT/FactoryBase.h>
00040 #include <OpenMS/CONCEPT/SingletonRegistry.h>
00041 #include <OpenMS/DATASTRUCTURES/String.h>
00042 
00043 #include <map>
00044 #include <iostream>
00045 #include <typeinfo>
00046 
00047 namespace OpenMS
00048 {
00049   class String;
00050 
00061   template <typename FactoryProduct>
00062   class Factory :
00063     public FactoryBase
00064   {
00065     friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
00066 
00067 private:
00069     typedef FactoryProduct * (*FunctionType)();
00070     typedef std::map<String, FunctionType> Map;
00071     typedef typename Map::const_iterator MapIterator;
00072     typedef Factory<FactoryProduct> FactoryType;
00073 
00075     virtual ~Factory(){}
00076 
00078     Factory()
00079     {
00080     }
00081 
00083     static Factory * instance_()
00084     {
00085       if (!instance_ptr_)
00086       {
00087         // name of this Factory
00088         String myName = typeid(FactoryType).name();
00089 
00090         //check if an instance of this kind of Factory already registered
00091         if (!SingletonRegistry::isRegistered(myName))
00092         {
00093           // if not registered yet ... add it
00094           instance_ptr_ = new Factory();
00095           // now, attention as ORDER of commands is important here:
00096           // first register the Factory
00097           SingletonRegistry::registerFactory(myName, instance_ptr_);
00098           // because this call, might use another instance of this factory, but we want the other instance to register the children with "US"
00099           FactoryProduct::registerChildren();
00100         }
00101         else
00102         {
00103           // get instance of this factory from registry
00104           instance_ptr_ = static_cast<FactoryType *>(SingletonRegistry::getFactory(myName));
00105         }
00106       }
00107       return instance_ptr_;
00108     }
00109 
00110 public:
00111 
00113     static FactoryProduct * create(const String & name)
00114     {
00115       MapIterator it = instance_()->inventory_.find(name);
00116       if (it != instance_()->inventory_.end())
00117       {
00118         return (*(it->second))();
00119       }
00120       else
00121       {
00122         throw Exception::InvalidValue(__FILE__, __LINE__, __PRETTY_FUNCTION__, "This FactoryProduct is not registered!", name.c_str());
00123       }
00124     }
00125 
00132     static void registerProduct(const String & name, const FunctionType creator)
00133     {
00134       instance_()->inventory_[name] = creator;
00135     }
00136 
00138     static bool isRegistered(const String & name)
00139     {
00140       if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
00141       {
00142         return true;
00143       }
00144       return false;
00145     }
00146 
00148     static std::vector<String> registeredProducts()
00149     {
00150       std::vector<String> list;
00151       for (MapIterator it = instance_()->inventory_.begin(); it != instance_()->inventory_.end(); ++it)
00152       {
00153         list.push_back(it->first);
00154       }
00155       return list;
00156     }
00157 
00158 private:
00159 
00160     Map inventory_;
00161     static Factory * instance_ptr_;
00162   };
00163 
00164   template <typename FactoryProduct>
00165   Factory<FactoryProduct> * Factory<FactoryProduct>::instance_ptr_ = 0;
00166 
00167 }
00168 #endif //OPENMS_CONCEPT_FACTORY_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:39 using doxygen 1.7.1