Singleton.py
Go to the documentation of this file.
00001 #/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 ##
00005 # @file Singleton.h
00006 # @brief Singleton template class
00007 # @date $Date$
00008 # @author Noriaki Ando <n-ando@aist.go.jp>
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 import thread
00020 
00021 ##
00022 # @if jp
00023 # @class Singleton クラステンプレート
00024 #
00025 # このテンプレートは、任意のクラスを Singleton にするテンプレートである。
00026 # 以下のようにして使用する。
00027 #
00028 # class A(Singleton):
00029 #   def __init__(self):
00030 #     pass
00031 class Singleton(object):
00032   __lockObj = thread.allocate_lock()
00033   __instance = None
00034 
00035   def __new__(self, *args, **kargs):
00036     return self.instance(*args, **kargs)
00037 
00038 
00039   def __init__(self, *args, **kargs):
00040     self.instance(*args, **kargs)
00041 
00042 
00043   def instance(self, *args, **kargs):
00044     self.__lockObj.acquire()
00045     try:
00046       if self.__instance is None:
00047         self.__instance = object.__new__(self, *args, **kargs)
00048 
00049     finally:
00050       self.__lockObj.release()
00051 
00052     return self.__instance
00053 
00054   instance = classmethod(instance)
00055 


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