GlobalFactory.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 ##
00005 # @file GlobalFactory.py
00006 # @brief generic Factory template class
00007 # @date $Date$
00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara
00009 #
00010 # Copyright (C) 2009
00011 #     Noriaki Ando
00012 #     Task-intelligence Research Group,
00013 #     Intelligent Systems Research Institute,
00014 #     National Institute of
00015 #         Advanced Industrial Science and Technology (AIST), Japan
00016 #     All rights reserved.
00017 #
00018 
00019 
00020 import OpenRTM_aist
00021 
00022 
00023 class Factory:
00024 
00025   FACTORY_OK     = 0
00026   FACTORY_ERROR  = 1
00027   ALREADY_EXISTS = 2
00028   NOT_FOUND      = 3
00029   INVALID_ARG    = 4
00030   UNKNOWN_ERROR  = 5
00031 
00032 
00033   def __init__(self):
00034     self._creators = {}
00035 
00036 
00037   ## bool hasFactory(const Identifier& id)
00038   def hasFactory(self, id):
00039     if not self._creators.has_key(id):
00040       return False
00041     return True
00042 
00043 
00044   ## std::vector<Identifier> getIdentifiers()
00045   def getIdentifiers(self):
00046     idlist = []
00047 
00048     for id in self._creators.keys():
00049       idlist.append(id)
00050     idlist.sort()
00051     return idlist
00052 
00053 
00054   ## ReturnCode addFactory(const Identifier& id,
00055   ##                       Creator creator,
00056   ##                       Destructor destructor)
00057   def addFactory(self, id, creator, destructor):
00058     if not creator:
00059       return self.INVALID_ARG
00060 
00061     if self._creators.has_key(id):
00062       return self.ALREADY_EXISTS
00063     
00064     self._creators[id] = creator
00065     return self.FACTORY_OK
00066 
00067 
00068   ## ReturnCode removeFactory(const Identifier& id)
00069   def removeFactory(self, id):
00070     if not self._creators.has_key(id):
00071       return self.NOT_FOUND
00072 
00073     del self._creators[id]
00074     return self.FACTORY_OK
00075 
00076 
00077   ## AbstractClass* createObject(const Identifier& id)
00078   def createObject(self, id):
00079     if not self._creators.has_key(id):
00080       print "Factory.createObject return None id: ", id
00081       return None
00082     return self._creators[id]()
00083 
00084 
00085   ## void deleteObject(const Identifier& id, AbstractClass*& obj)
00086   def deleteObject(self, obj, id=None):
00087     if id:
00088       if not self._creators.has_key(id):
00089         return
00090 
00091     del obj
00092     return
00093 
00094     
00095 gfactory = None
00096 
00097 class GlobalFactory(Factory):
00098   def __init__(self):
00099     Factory.__init__(self)
00100     pass
00101 
00102 
00103   def instance():
00104     global gfactory
00105     
00106     if gfactory is None:
00107       gfactory = GlobalFactory()
00108 
00109     return gfactory
00110 
00111   instance = staticmethod(instance)
00112 
00113 
00114   def __del__(self):
00115     pass


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28