Blender V4.3
BLI_compute_context.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
37#include <optional>
38
39#include "BLI_array.hh"
41#include "BLI_stack.hh"
42#include "BLI_string_ref.hh"
44
45namespace blender {
46
54
55 uint64_t hash() const
56 {
57 return v1;
58 }
59
61
62 void mix_in(const void *data, int64_t len);
63
64 friend std::ostream &operator<<(std::ostream &stream, const ComputeContextHash &hash);
65};
66
74 private:
78 const char *static_type_;
82 const ComputeContext *parent_ = nullptr;
83
84 protected:
90
91 public:
93 : static_type_(static_type), parent_(parent)
94 {
95 if (parent != nullptr) {
96 hash_ = parent_->hash_;
97 }
98 }
99 virtual ~ComputeContext() = default;
100
102 {
103 return hash_;
104 }
105
106 const char *static_type() const
107 {
108 return static_type_;
109 }
110
111 const ComputeContext *parent() const
112 {
113 return parent_;
114 }
115
119 void print_stack(std::ostream &stream, StringRef name) const;
120
124 virtual void print_current_in_line(std::ostream &stream) const = 0;
125
126 friend std::ostream &operator<<(std::ostream &stream, const ComputeContext &compute_context);
127};
128
135 private:
136 LinearAllocator<> allocator_;
138 std::optional<Vector<destruct_ptr<ComputeContext>>> old_contexts_;
139
140 public:
146 {
147 if (!old_contexts_.has_value()) {
148 old_contexts_.emplace();
149 }
150 }
151
152 bool is_empty() const
153 {
154 return contexts_.is_empty();
155 }
156
157 const ComputeContext *current() const
158 {
159 if (contexts_.is_empty()) {
160 return nullptr;
161 }
162 return contexts_.peek().get();
163 }
164
166 {
167 BLI_assert(!contexts_.is_empty());
168 return this->current()->hash();
169 }
170
171 template<typename T, typename... Args> void push(Args &&...args)
172 {
173 const ComputeContext *current = this->current();
174 destruct_ptr<T> context = allocator_.construct<T>(current, std::forward<Args>(args)...);
175 contexts_.push(std::move(context));
176 }
177
178 void pop()
179 {
180 auto context = contexts_.pop();
181 if (old_contexts_) {
182 old_contexts_->append(std::move(context));
183 }
184 }
185
187 void pop_until(const ComputeContext *context)
188 {
189 while (!contexts_.is_empty()) {
190 if (contexts_.peek().get() == context) {
191 return;
192 }
193 this->pop();
194 }
195 /* Should have found the context above if it's not null. */
196 BLI_assert(context == nullptr);
197 }
198};
199
200} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_STRUCT_EQUALITY_OPERATORS_2(Type, m1, m2)
const ComputeContextHash hash() const
void pop_until(const ComputeContext *context)
const ComputeContext * current() const
const ComputeContext * parent() const
void print_stack(std::ostream &stream, StringRef name) const
friend std::ostream & operator<<(std::ostream &stream, const ComputeContext &compute_context)
virtual ~ComputeContext()=default
ComputeContext(const char *static_type, const ComputeContext *parent)
const ComputeContextHash & hash() const
const char * static_type() const
virtual void print_current_in_line(std::ostream &stream) const =0
destruct_ptr< T > construct(Args &&...args)
bool is_empty() const
Definition BLI_stack.hh:308
void push(const T &value)
Definition BLI_stack.hh:213
int len
#define T
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90
void mix_in(const void *data, int64_t len)
friend std::ostream & operator<<(std::ostream &stream, const ComputeContextHash &hash)