Yi Kong | 8328301 | 2023-12-13 12:57:00 +0900 | [diff] [blame^] | 1 | """ |
| 2 | Objective-C runtime wrapper for use by LLDB Python formatters |
| 3 | |
| 4 | Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | See https://llvm.org/LICENSE.txt for license information. |
| 6 | SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | """ |
| 8 | import lldb.formatters.metrics |
| 9 | |
| 10 | |
| 11 | class Cache: |
| 12 | def __init__(self): |
| 13 | self.data = {} |
| 14 | self.statistics = lldb.formatters.metrics.Metrics() |
| 15 | self.statistics.add_metric("hit") |
| 16 | self.statistics.add_metric("miss") |
| 17 | |
| 18 | def look_for_key(self, key): |
| 19 | if key in self.data: |
| 20 | return True |
| 21 | return False |
| 22 | |
| 23 | def add_item(self, key, value, ok_to_replace=True): |
| 24 | if not (ok_to_replace) and self.look_for_key(key): |
| 25 | return False |
| 26 | self.data[key] = value |
| 27 | return True |
| 28 | |
| 29 | def get_value(self, key, default=None): |
| 30 | if self.look_for_key(key): |
| 31 | self.statistics.metric_hit("hit", key) |
| 32 | return self.data[key] |
| 33 | else: |
| 34 | self.statistics.metric_hit("miss", key) |
| 35 | return default |