Blender V4.3
spreadsheet_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
9void SpreadsheetCache::add(std::unique_ptr<Key> key, std::unique_ptr<Value> value)
10{
11 key->is_used = true;
12 cache_map_.add_overwrite(*key, std::move(value));
13 keys_.append(std::move(key));
14}
15
17{
18 std::unique_ptr<Value> *value = cache_map_.lookup_ptr(key);
19 if (value == nullptr) {
20 return nullptr;
21 }
22 const Key &stored_cache_key = cache_map_.lookup_key(key);
23 stored_cache_key.is_used = true;
24 return value->get();
25}
26
28 std::unique_ptr<Key> key, FunctionRef<std::unique_ptr<Value>()> create_value)
29{
30 Value *value = this->lookup(*key);
31 if (value != nullptr) {
32 return *value;
33 }
34 std::unique_ptr<Value> new_value = create_value();
35 value = new_value.get();
36 this->add(std::move(key), std::move(new_value));
37 return *value;
38}
39
41{
42 for (std::unique_ptr<Key> &key : keys_) {
43 key->is_used = false;
44 }
45}
46
48{
49 /* First remove the keys from the map and free the values. */
50 cache_map_.remove_if([&](auto item) {
51 const Key &key = item.key;
52 return !key.is_used;
53 });
54
55 /* Then free the keys. */
56 for (int i = 0; i < keys_.size();) {
57 if (keys_[i]->is_used) {
58 i++;
59 }
60 else {
61 keys_.remove_and_reorder(i);
62 }
63 }
64}
65
66} // namespace blender::ed::spreadsheet
void add(std::unique_ptr< Key > key, std::unique_ptr< Value > value)
Value & lookup_or_add(std::unique_ptr< Key > key, FunctionRef< std::unique_ptr< Value >()> create_value)