/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

ElementMgr.h
Go to the documentation of this file.
00001 #ifndef LAF_ELEMENT_MGR_H
00002 #define LAF_ELEMENT_MGR_H
00003 
00004 #include "LafKernel/OptionParser.h"
00005 #include "LafKernel/LafException.h"
00006 #include <map>
00007 #include <list>
00008 #include <vector>
00009 #include <string>
00010 
00011 template<typename Element>
00012 class ElementMgr
00013 {
00014     public :
00015 
00016         const std::string& name() { return m_name; }
00017 
00018         static Element* get(const std::string& objName, bool create = false);
00019 
00020         template<typename ConcreteType>
00021         static ConcreteType* get(const std::string& objName, bool create = false);
00022 
00023         virtual bool initialize();
00024         virtual bool finalize();
00025 
00026 
00027     private :
00028 
00029         std::string   m_name;
00030 
00031 
00032     protected :
00033 
00034         ElementMgr(const std::string& name);
00035         virtual ~ElementMgr();
00036 
00037         static std::list<Element*>            m_elements;
00038 
00039         static std::map<std::string, Element*>  name2obj;
00040 
00041         typedef Element* (*ElementCreator)(const std::string&);
00042         typedef std::map<std::string, ElementCreator> Type2CreatorMap;
00043         static std::map<std::string, ElementCreator>  elementCreatorMap;
00044 
00045 
00046     private :
00047 
00048         ElementMgr();
00049         ElementMgr(const ElementMgr&);
00050         ElementMgr& operator=(const ElementMgr&);
00051 };
00052 
00053 template<typename Element>
00054 std::list<Element*>   ElementMgr<Element>::m_elements;
00055 
00056 template<typename Element>
00057 std::map<std::string, Element*>  ElementMgr<Element>::name2obj;
00058 
00059 template<typename Element>
00060 typename ElementMgr<Element>::Type2CreatorMap  ElementMgr<Element>::elementCreatorMap;
00061 
00062 template<typename Element>
00063 ElementMgr<Element>::ElementMgr(const std::string& name)
00064     : m_name(name)
00065 {
00066     std::vector<std::string> vNames;
00067     OptionParser::setOption(name, "Contents", vNames);
00068 
00069     for(std::vector<std::string>::iterator i=vNames.begin(); i!=vNames.end(); ++i) {
00070         std::string typName = *i;
00071         std::string objName = typName;
00072         std::string::size_type pseg = typName.find('/', 0);
00073         if ( pseg != std::string::npos ) {
00074             typName = typName.substr(0, pseg);
00075             objName = objName.substr(pseg+1, std::string::npos);
00076         }
00077 
00078         typename Type2CreatorMap::iterator j = elementCreatorMap.find(typName);
00079         if ( j == elementCreatorMap.end() ) {
00080             std::string msg = name + ": unknown content type " + typName;
00081             throw LafException(msg);
00082         }
00083         Element* obj = (j->second)(objName);
00084         m_elements.push_back(obj);
00085         name2obj[objName] = obj;
00086     }
00087 }
00088 
00089 template<typename Element>
00090 ElementMgr<Element>::~ElementMgr()
00091 {
00092     for(typename std::list<Element*>::iterator i=m_elements.begin(); i!=m_elements.end(); ++i) {
00093         delete (*i);
00094     }
00095 }
00096 
00097 template<typename Element>
00098 Element* ElementMgr<Element>::get(const std::string& objName, bool create)
00099 {
00100     typename std::map<std::string, Element*>::iterator i = name2obj.find(objName);
00101     if ( i != name2obj.end() ) {
00102         return (i->second);
00103     }
00104     if ( create ) {
00105         typename Type2CreatorMap::iterator j = elementCreatorMap.find(objName);
00106         if ( j != elementCreatorMap.end() ) {
00107             Element* obj = (j->second)(objName);
00108             m_elements.push_back(obj);
00109             name2obj[objName] = obj;
00110             return obj;
00111         }
00112     }
00113     return (Element*)0;
00114 }
00115 
00116 template<typename Element> template<typename ConcreteType>
00117 ConcreteType* ElementMgr<Element>::get(const std::string& objName, bool create)
00118 {
00119     return dynamic_cast<ConcreteType*>(get(objName, create));
00120 }
00121 
00122 template<typename Element>
00123 bool ElementMgr<Element>::initialize()
00124 {
00125     for(typename std::list<Element*>::iterator i=m_elements.begin(); i!=m_elements.end(); ++i) {
00126         if ( ! (*i)->initialize() ) return false;
00127     }
00128     return true;
00129 }
00130 
00131 template<typename Element>
00132 bool ElementMgr<Element>::finalize()
00133 {
00134     for(typename std::list<Element*>::iterator i=m_elements.begin(); i!=m_elements.end(); ++i) {
00135         if ( ! (*i)->finalize() ) return false;
00136     }
00137     return true;
00138 }
00139 
00140 #endif
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:50:18 for LafKernel by doxygen 1.7.4