summaryrefslogtreecommitdiffstats
path: root/staslib/singleton.py
blob: f80fc0b3e511ea90f48768627a84d4c9cf46f767 (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
# Copyright (c) 2022, Dell Inc. or its subsidiaries.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See the LICENSE file for details.
#
# This file is part of NVMe STorage Appliance Services (nvme-stas).
#
# Authors: Martin Belanger <Martin.Belanger@dell.com>
#
'''Implementation of a singleton pattern'''


class Singleton(type):
    '''metaclass implementation of a singleton pattern'''

    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            # This variable declaration is required to force a
            # strong reference on the instance.
            instance = super(Singleton, cls).__call__(*args, **kwargs)
            cls._instances[cls] = instance
        return cls._instances[cls]

    def destroy(cls):
        '''Delete a singleton instance.

        This is to be invoked using the derived class Name.

        For example:

        class Child(Singleton):
            pass

        child1 = Child() # Instantiate singleton
        child2 = Child() # Get a reference to the singleton

        print(f'{child1 is child2}') # True

        Child.destroy()  # Delete the singleton

        print(f'{child1 is child2}') # Still True because child1 and child2 still hold reference to the singleton

        child1 = Child() # Instantiate a new singleton and assign to child1

        print(f'{child1 is child2}') # False
        '''
        cls._instances.pop(cls, None)