Atlas-C++
objectFactory.h
00001 // This file may be redistributed and modified only under the terms of
00002 // the GNU Lesser General Public License (See COPYING for details).
00003 // Copyright (C) 2000 Aloril
00004 // Copyright (C) 2005 Al Riddoch
00005 
00006 // $Id$
00007 
00008 #ifndef ATLAS_OBJECTS_OBJECTFACTORY_H
00009 #define ATLAS_OBJECTS_OBJECTFACTORY_H
00010 
00011 #include <Atlas/Objects/Root.h>
00012 #include <Atlas/Objects/SmartPtr.h>
00013 
00014 #include <string>
00015 #include <list>
00016 #include <map>
00017 
00018 namespace Atlas { namespace Objects {
00019 
00020 class NoSuchFactoryException : public Atlas::Exception
00021 {
00022   protected:
00023     std::string name;
00024   public:
00025     NoSuchFactoryException(const std::string& name) :
00026                Atlas::Exception("No factory for Objects type"), name(name) { }
00027     virtual ~NoSuchFactoryException() throw ();
00028     const std::string & getName() {
00029         return name;
00030     }
00031 };
00032 
00033 template <class T>
00034 static SmartPtr<RootData> factory(const std::string &, int)
00035 {
00036     SmartPtr<T> obj;
00037     return obj;
00038 }
00039 
00040 SmartPtr<RootData> generic_factory(const std::string & name, int no);
00041 SmartPtr<RootData> anonymous_factory(const std::string & name, int no);
00042 
00043 typedef Root (*FactoryMethod)(const std::string &, int);
00044 typedef std::map<const std::string, std::pair<FactoryMethod, int> > FactoryMap;
00045 
00046 class Factories 
00047 {
00048 public:
00049     friend class AddFactories;
00050 
00051     Factories();
00052     explicit Factories(const Factories &);
00053 
00054     bool hasFactory(const std::string& name);
00055     Root createObject(const std::string& name);
00056     Root createObject(const Atlas::Message::MapType & msg);
00057     std::list<std::string> getKeys();
00058     int addFactory(const std::string& name, FactoryMethod method);
00059 
00060     static Factories * instance();
00061 private:
00062     FactoryMap m_factories;
00063     static Factories * m_instance;
00064 
00065     void addFactory(const std::string& name, FactoryMethod method, int classno);
00066 };
00067     
00068 extern std::map<const std::string, Root> objectDefinitions;
00069 
00070 } } // namespace Atlas::Objects
00071 
00072 #endif // ATLAS_OBJECTS_OBJECTFACTORY_H

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.