Blender V5.3
deg_builder_stack.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_vector.hh"
12
13namespace blender {
14
15struct ID;
16struct bConstraint;
17struct bPoseChannel;
18struct ModifierData;
19
20namespace deg {
21
22/* This class keeps track of the builder calls nesting, allowing to unroll them back and provide a
23 * clue about how the builder made it to its current state.
24 *
25 * The tracing is based on the builder giving a trace clues to the stack. Typical usage is:
26 *
27 * void DepsgraphRelationBuilder::my_id_builder(ID *id)
28 * {
29 * if (built_map_.check_is_built_and_tag(id)) {
30 * return;
31 * }
32 *
33 * const BuilderStack::ScopedEntry stack_entry = stack_.trace(*id);
34 *
35 * ...
36 * }
37 */
39 public:
40 /* Entry of the backtrace.
41 * A cheap-to-construct wrapper which allows to gather a proper string representation whenever
42 * the stack is printed. */
43 class Entry {
44 public:
45 explicit Entry(const ID &id) : id_(&id) {}
46
47 explicit Entry(const bConstraint &constraint) : constraint_(&constraint) {}
48
49 explicit Entry(const bPoseChannel &pchan) : pchan_(&pchan) {}
50
51 explicit Entry(const ModifierData &modifier_data) : modifier_data_(&modifier_data) {}
52
53 private:
54 friend class BuilderStack;
55
56 const ID *id_ = nullptr;
57 const bConstraint *constraint_ = nullptr;
58 const ModifierData *modifier_data_ = nullptr;
59 const bPoseChannel *pchan_ = nullptr;
60 };
61
63
64 /* A helper class to provide a RAII style of tracing. It is constructed by the
65 * `BuilderStack::trace` (which pushes entry to the stack), and upon destruction of this object
66 * the corresponding entry is popped from the stack.
67 *
68 * The goal of this `ScopedEntry` is to free developers from worrying about removing entries from
69 * the stack whenever leaving a builder step scope. */
71 public:
72 /* Delete copy constructor and operator: scoped entries are only supposed to be constructed
73 * once and never copied. */
74 ScopedEntry(const ScopedEntry &other) = delete;
75 ScopedEntry &operator=(const ScopedEntry &other) = delete;
76
77 /* Move semantic. */
78 ScopedEntry(ScopedEntry &&other) noexcept : stack_(other.stack_)
79 {
80 other.stack_ = nullptr;
81 }
83 {
84 if (this == &other) {
85 return *this;
86 }
87
88 stack_ = other.stack_;
89 other.stack_ = nullptr;
90
91 return *this;
92 }
93
95 {
96 /* Stack will become nullptr when the entry was moved somewhere else. */
97 if (stack_ != nullptr) {
98 BLI_assert(!stack_->is_empty());
99 stack_->pop_last();
100 }
101 }
102
103 private:
104 friend BuilderStack;
105
106 explicit ScopedEntry(Stack &stack) : stack_(&stack) {}
107
108 Stack *stack_;
109 };
110
111 BuilderStack() = default;
112 ~BuilderStack() = default;
113
114 bool is_empty() const
115 {
116 return stack_.is_empty();
117 }
118
119 void print_backtrace(std::ostream &stream);
120
121 template<class... Args> ScopedEntry trace(const Args &...args)
122 {
123 stack_.append_as(args...);
124
125 return ScopedEntry(stack_);
126 }
127
128 private:
129 Stack stack_;
130};
131
132} // namespace deg
133} // namespace blender
Entry(const bPoseChannel &pchan)
Entry(const bConstraint &constraint)
Entry(const ModifierData &modifier_data)
ScopedEntry(const ScopedEntry &other)=delete
ScopedEntry(ScopedEntry &&other) noexcept
ScopedEntry & operator=(ScopedEntry &&other)
ScopedEntry & operator=(const ScopedEntry &other)=delete
void print_backtrace(std::ostream &stream)
ScopedEntry trace(const Args &...args)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)