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