summaryrefslogtreecommitdiff
path: root/taurus/lib/taurus/core/util/singleton.py
blob: ba8edc136f425486c04a551bcbbd567e08ac7e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python

#############################################################################
##
## This file is part of Taurus
## 
## http://taurus-scada.org
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

"""This module contains a class which can be used as a super class for all
classes that need to implement the Singleton design pattern."""

__all__ = ["Singleton"]

__docformat__ = "restructuredtext"

class Singleton(object):
    """ This class allows Singleton objects
    The __new__ method is overriden to force Singleton behaviour.
    The Singleton is created for the lowest subclass.
    Usage::
    
        from taurus.core.util.singleton import Singleton
        
        class MyManager(Singleton):
        
            def init(self, *args, **kwargs):
                print "Singleton initialization"
    
    command line::
    
        >>> manager1 = MyManager()
        Singleton initialization
        
        >>> manager2 = MyManager()
        
        >>> print(manager1,manager2)
        <__main__.MyManager object at 0x9c2a0ec> 
        <__main__.MyManager object at 0x9c2a0ec>

    Notice that the two instances of manager point to the same object even
    though you *tried* to construct two instances of MyManager. 
    
    .. warning::
    
        although __new__ is overriden __init__ is still being called for 
        each instance=Singleton()
    """
    
    ## Singleton object
    _the_instance = None
    
    def __new__(cls, *p, **k):
        #srubio: added type checking
        if cls != type(cls._the_instance):
            cls._the_instance = object.__new__(cls)
            
            #srubio: added init_single check
            if 'init_single' in cls.__dict__: 
                cls._the_instance.init_single(*p,**k)
            else: cls._the_instance.init(*p,**k)
        return cls._the_instance
     
    def init(self, *p,**k):
        pass