Blender V4.3
spreadsheet_cache.hh
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#pragma once
6
7#include <atomic>
8
9#include "BLI_function_ref.hh"
10#include "BLI_map.hh"
11#include "BLI_vector.hh"
12
14
22 public:
23 class Key {
24 public:
25 virtual ~Key() = default;
26
27 mutable bool is_used = false;
28
29 virtual uint64_t hash() const = 0;
30
31 friend bool operator==(const Key &a, const Key &b)
32 {
33 return a.is_equal_to(b);
34 }
35
36 private:
37 virtual bool is_equal_to(const Key &other) const = 0;
38 };
39
40 class Value {
41 public:
42 virtual ~Value() = default;
43 };
44
45 private:
47 Map<std::reference_wrapper<const Key>, std::unique_ptr<Value>> cache_map_;
48
49 public:
50 /* Adding or looking up a key tags it as being used, so that it won't be removed. */
51 void add(std::unique_ptr<Key> key, std::unique_ptr<Value> value);
52 Value *lookup(const Key &key);
53 Value &lookup_or_add(std::unique_ptr<Key> key,
54 FunctionRef<std::unique_ptr<Value>()> create_value);
55
56 void set_all_unused();
57 void remove_all_unused();
58
59 template<typename T> T &lookup_or_add(std::unique_ptr<Key> key)
60 {
61 return dynamic_cast<T &>(
62 this->lookup_or_add(std::move(key), []() { return std::make_unique<T>(); }));
63 }
64};
65
66} // namespace blender::ed::spreadsheet
friend bool operator==(const Key &a, const Key &b)
T & lookup_or_add(std::unique_ptr< Key > key)
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)
local_group_size(16, 16) .push_constant(Type b
unsigned __int64 uint64_t
Definition stdint.h:90