xref: /aosp_15_r20/tools/asuite/aidegen/lib/singleton.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019 - The Android Open Source Project
3*c2e18aaaSAndroid Build Coastguard Worker#
4*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*c2e18aaaSAndroid Build Coastguard Worker#
8*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
9*c2e18aaaSAndroid Build Coastguard Worker#
10*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
15*c2e18aaaSAndroid Build Coastguard Worker
16*c2e18aaaSAndroid Build Coastguard Worker"""A meta class for singleton pattern."""
17*c2e18aaaSAndroid Build Coastguard Worker
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerclass Singleton(type):
20*c2e18aaaSAndroid Build Coastguard Worker    """A singleton metaclass.
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Worker    Class attributes:
23*c2e18aaaSAndroid Build Coastguard Worker        _instances: A dictionary keeps singletons' information, whose key is
24*c2e18aaaSAndroid Build Coastguard Worker        class and value is an instance of that class.
25*c2e18aaaSAndroid Build Coastguard Worker
26*c2e18aaaSAndroid Build Coastguard Worker    Usage:
27*c2e18aaaSAndroid Build Coastguard Worker        from aidegen.lib import singleton
28*c2e18aaaSAndroid Build Coastguard Worker
29*c2e18aaaSAndroid Build Coastguard Worker        class AClass(BaseClass, metaclass=singleton.Singleton):
30*c2e18aaaSAndroid Build Coastguard Worker            pass
31*c2e18aaaSAndroid Build Coastguard Worker    """
32*c2e18aaaSAndroid Build Coastguard Worker
33*c2e18aaaSAndroid Build Coastguard Worker    _instances = {}
34*c2e18aaaSAndroid Build Coastguard Worker
35*c2e18aaaSAndroid Build Coastguard Worker    def __call__(cls, *args, **kwds):
36*c2e18aaaSAndroid Build Coastguard Worker        """Initialize a singleton instance."""
37*c2e18aaaSAndroid Build Coastguard Worker        if cls not in cls._instances:
38*c2e18aaaSAndroid Build Coastguard Worker            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwds)
39*c2e18aaaSAndroid Build Coastguard Worker        return cls._instances[cls]
40