Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 1 | """ |
| 2 | Objective-C runtime wrapper for use by LLDB Python formatters |
| 3 | |
| 4 | part of The LLVM Compiler Infrastructure |
| 5 | This file is distributed under the University of Illinois Open Source |
| 6 | License. See LICENSE.TXT for details. |
| 7 | """ |
Enrico Granata | 28399ad | 2012-04-25 01:39:27 +0000 | [diff] [blame] | 8 | import lldb.formatters.metrics |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 9 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 11 | class Cache: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 12 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | def __init__(self): |
| 14 | self.data = {} |
| 15 | self.statistics = lldb.formatters.metrics.Metrics() |
| 16 | self.statistics.add_metric('hit') |
| 17 | self.statistics.add_metric('miss') |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 18 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | def look_for_key(self, key): |
| 20 | if key in self.data: |
| 21 | return True |
| 22 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | def add_item(self, key, value, ok_to_replace=True): |
| 25 | if not(ok_to_replace) and self.look_for_key(key): |
| 26 | return False |
| 27 | self.data[key] = value |
| 28 | return True |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | def get_value(self, key, default=None): |
| 31 | if self.look_for_key(key): |
| 32 | self.statistics.metric_hit('hit', key) |
| 33 | return self.data[key] |
| 34 | else: |
| 35 | self.statistics.metric_hit('miss', key) |
| 36 | return default |