Repository navigation
Expand file tree
/
Copy pathregistry.cpp
More file actions
128 lines (117 loc) · 5.13 KB
/
Copy pathregistry.cpp
File metadata and controls
128 lines (117 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <teaql/runtime/registry.hpp>
#include <teaql/core/detail/debug.hpp>
namespace teaql {
const EntityDescriptor* MetadataStore::entity_by_symbols(const EntitySymbols& symbols) const {
auto name = symbols.name();
const auto* found = name ? entity(*name) : nullptr;
return found && (!found->symbols || found->symbols->same_layout(symbols)) ? found : nullptr;
}
InMemoryMetadataStore::InMemoryMetadataStore(std::allocator_arg_t,
Allocator a,
const InMemoryMetadataStore& other)
: entities_(other.entities_, a.resource()), models_(a.resource()) {
// Copy the actual index, including empty slots and replacement order.
// Rebuilding by sorted entity names would change aliased-slot semantics.
for (const auto& [model, entries] : other.models_) {
auto& copied = models_.try_emplace(model).first->second;
copied.reserve(entries.size());
for (const auto* entry : entries) {
copied.push_back(entry ? entity(entry->name) : nullptr);
}
}
}
namespace {
struct MetadataObserver {
using allocator_type = Allocator;
ResourceBorrow<const MetadataStore> store;
MetadataObserver(std::allocator_arg_t, Allocator a, const MetadataStore& target)
: store(std::allocator_arg, a, target, detail::resource_arena(target.memory_resource())) {}
MetadataObserver(std::allocator_arg_t, Allocator a, const MetadataObserver& other)
: store(std::allocator_arg, a, other.store) {}
void operator()(ExecutionMetadata metadata) const {
store.get().record_metadata_log(metadata);
}
};
}
std::optional<ExecutionObserver> MetadataStore::query_diagnostic_observer(Memory r) const {
if (!capture_execution_metadata()) {
return {};
}
return ExecutionObserver(std::allocator_arg,
Allocator(r),
MetadataObserver(std::allocator_arg, Allocator(r), *this));
}
std::optional<ExecutionObserver> MetadataStore::mutation_diagnostic_observer(Memory r) const {
return ExecutionObserver(std::allocator_arg,
Allocator(r),
MetadataObserver(std::allocator_arg, Allocator(r), *this));
}
void InMemoryMetadataStore::register_entity(EntityDescriptor descriptor) {
auto r = memory_resource();
auto found = entities_.find(descriptor.name);
const auto* previous = found == entities_.end() ? nullptr : found->second.symbols.read();
if (previous && descriptor.symbols && !previous->same_layout(*descriptor.symbols)) {
throw std::invalid_argument(
"cannot register different generated models under the same entity name");
}
if (descriptor.symbols) {
auto checked = descriptor.symbols->validate(r);
if (!checked) {
std::string message("invalid generated symbols: ");
message += detail::debug_text(r, checked.error());
throw std::invalid_argument(message);
}
}
descriptor.freeze();
Vector<const EntityDescriptor*>* indexed = nullptr;
if (descriptor.symbols) {
auto& entries = models_.try_emplace(&descriptor.symbols->model()).first->second;
entries.resize(descriptor.symbols->model().entities().size(), nullptr);
indexed = &entries;
}
if (found == entities_.end()) {
// Copy the key before moving the descriptor; argument evaluation order
// must not make the map key depend on a moved-from name.
String key(descriptor.name, r);
found = entities_.emplace(std::move(key), std::move(descriptor)).first;
} else {
detail::assign_scoped(found->second, std::move(descriptor));
}
if (previous) {
auto old = models_.find(&previous->model());
if (old != models_.end()) {
old->second[previous->entity().value] = nullptr;
}
}
if (indexed) {
(*indexed)[found->second.symbols->entity().value] = &found->second;
}
}
const EntityDescriptor*
InMemoryMetadataStore::entity_by_symbols(const EntitySymbols& symbols) const {
auto model = models_.find(&symbols.model());
if (model != models_.end() && symbols.entity().value < model->second.size()) {
const auto* found = model->second[symbols.entity().value];
if (found && found->symbols && found->symbols->same_layout(symbols)) {
return found;
}
}
auto name = symbols.name();
const auto* named = name ? entity(*name) : nullptr;
return named && !named->symbols ? named : nullptr;
}
Vector<const EntityDescriptor*> InMemoryMetadataStore::all_entities(Memory r) const {
// The array owns only borrowed pointers. Its allocation cannot extend the
// lifetime of the descriptors it enumerates.
auto& owner = detail::resource_arena(memory_resource());
if (!owner.can_lend_to(detail::resource_arena(r))) {
throw std::invalid_argument("Metadata enumeration cannot outlive its owning Context");
}
Vector<const EntityDescriptor*> out(r);
out.reserve(entities_.size());
for (const auto& [name, entity] : entities_) {
out.push_back(&entity);
}
return out;
}
}