Blender
V4.3
source
blender
editors
space_spreadsheet
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
5
#include "
spreadsheet_cache.hh
"
6
7
namespace
blender::ed::spreadsheet
{
8
9
void
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
16
SpreadsheetCache::Value
*
SpreadsheetCache::lookup
(
const
Key
&key)
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
27
SpreadsheetCache::Value
&
SpreadsheetCache::lookup_or_add
(
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
40
void
SpreadsheetCache::set_all_unused
()
41
{
42
for
(std::unique_ptr<Key> &key : keys_) {
43
key->is_used =
false
;
44
}
45
}
46
47
void
SpreadsheetCache::remove_all_unused
()
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
blender::FunctionRef
Definition
BLI_function_ref.hh:74
blender::ed::spreadsheet::SpreadsheetCache::Key
Definition
spreadsheet_cache.hh:23
blender::ed::spreadsheet::SpreadsheetCache::Key::is_used
bool is_used
Definition
spreadsheet_cache.hh:27
blender::ed::spreadsheet::SpreadsheetCache::Value
Definition
spreadsheet_cache.hh:40
blender::ed::spreadsheet::SpreadsheetCache::set_all_unused
void set_all_unused()
Definition
spreadsheet_cache.cc:40
blender::ed::spreadsheet::SpreadsheetCache::lookup
Value * lookup(const Key &key)
Definition
spreadsheet_cache.cc:16
blender::ed::spreadsheet::SpreadsheetCache::add
void add(std::unique_ptr< Key > key, std::unique_ptr< Value > value)
Definition
spreadsheet_cache.cc:9
blender::ed::spreadsheet::SpreadsheetCache::lookup_or_add
Value & lookup_or_add(std::unique_ptr< Key > key, FunctionRef< std::unique_ptr< Value >()> create_value)
Definition
spreadsheet_cache.cc:27
blender::ed::spreadsheet::SpreadsheetCache::remove_all_unused
void remove_all_unused()
Definition
spreadsheet_cache.cc:47
blender::ed::spreadsheet
Definition
ED_space_api.hh:45
spreadsheet_cache.hh
Generated on Thu Feb 6 2025 07:36:39 for Blender by
doxygen
1.11.0