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