forked from OSchip/llvm-project
[clang.py] Add CachedProperty decorator
It isn't used anywhere yet. llvm-svn: 162190
This commit is contained in:
parent
a3b7a802cc
commit
fbd4f4768c
|
@ -133,6 +133,31 @@ class TranslationUnitSaveError(Exception):
|
|||
|
||||
### Structures and Utility Classes ###
|
||||
|
||||
class CachedProperty(object):
|
||||
"""Decorator that lazy-loads the value of a property.
|
||||
|
||||
The first time the property is accessed, the original property function is
|
||||
executed. The value it returns is set as the new value of that instance's
|
||||
property, replacing the original method.
|
||||
"""
|
||||
|
||||
def __init__(self, wrapped):
|
||||
self.wrapped = wrapped
|
||||
try:
|
||||
self.__doc__ = wrapped.__doc__
|
||||
except:
|
||||
pass
|
||||
|
||||
def __get__(self, instance, instance_type=None):
|
||||
if instance is None:
|
||||
return self
|
||||
|
||||
value = self.wrapped(instance)
|
||||
setattr(instance, self.wrapped.__name__, value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class _CXString(Structure):
|
||||
"""Helper for transforming CXString results."""
|
||||
|
||||
|
|
Loading…
Reference in New Issue