]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/caching.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Common / caching.py
CommitLineData
b23414f6
JC
1## @file\r
2# help with caching in BaseTools\r
3#\r
4# Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>\r
5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
b23414f6
JC
7#\r
8\r
9## Import Modules\r
10#\r
11\r
12# for class function\r
13class cached_class_function(object):\r
14 def __init__(self, function):\r
15 self._function = function\r
16 def __get__(self, obj, cls):\r
17 def CallMeHere(*args,**kwargs):\r
18 Value = self._function(obj, *args,**kwargs)\r
19 obj.__dict__[self._function.__name__] = lambda *args,**kwargs:Value\r
20 return Value\r
21 return CallMeHere\r
22\r
23# for class property\r
24class cached_property(object):\r
25 def __init__(self, function):\r
26 self._function = function\r
27 def __get__(self, obj, cls):\r
28 Value = obj.__dict__[self._function.__name__] = self._function(obj)\r
29 return Value\r
30\r
31# for non-class function\r
32class cached_basic_function(object):\r
33 def __init__(self, function):\r
34 self._function = function\r
35 # wrapper to call _do since <class>.__dict__ doesn't support changing __call__\r
36 def __call__(self,*args,**kwargs):\r
37 return self._do(*args,**kwargs)\r
38 def _do(self,*args,**kwargs):\r
39 Value = self._function(*args,**kwargs)\r
40 self.__dict__['_do'] = lambda self,*args,**kwargs:Value\r
41 return Value\r