Skip to main content

ContractRegistry.h File

Contract registry for builder-level validation. More...

Included Headers

#include <memory> #include <string> #include <unordered_map> #include <utility> #include <vector> #include "contracts/Contract.h" #include "contracts/ValidationReport.h"

Namespaces Index

namespacesimaai
namespaceneat

Classes Index

classContractRegistry

Holds a set of Contracts and runs them to produce a ValidationReport. More...

Description

Contract registry for builder-level validation.

File Listing

The file content with the documentation metadata removed is:

1// include/contracts/ContractRegistry.h
7#pragma once
8
9#include <memory>
10#include <string>
11#include <unordered_map>
12#include <utility>
13#include <vector>
14
17
18namespace simaai::neat {
19
25class ContractRegistry final {
26public:
27 using ContractPtr = std::shared_ptr<Contract>;
28
29 ContractRegistry() = default;
30
33 if (!c)
34 return *this;
35 const std::string cid = c->id();
36 if (cid.empty())
37 return *this;
38
39 auto it = by_id_.find(cid);
40 if (it == by_id_.end()) {
41 order_.push_back(cid);
42 }
43 by_id_[cid] = std::move(c);
44 return *this;
45 }
46
48 template <class T, class... Args> ContractRegistry& emplace(Args&&... args) {
49 return add(std::make_shared<T>(std::forward<Args>(args)...));
50 }
51
53 bool remove(const std::string& id) {
54 auto it = by_id_.find(id);
55 if (it == by_id_.end())
56 return false;
57 by_id_.erase(it);
58
59 // Keep deterministic order_: erase id if present.
60 for (auto oit = order_.begin(); oit != order_.end(); ++oit) {
61 if (*oit == id) {
62 order_.erase(oit);
63 break;
64 }
65 }
66 return true;
67 }
68
69 void clear() {
70 by_id_.clear();
71 order_.clear();
72 }
73
74 std::size_t size() const noexcept {
75 return by_id_.size();
76 }
77 bool empty() const noexcept {
78 return by_id_.empty();
79 }
80
82 ContractPtr get(const std::string& id) const {
83 auto it = by_id_.find(id);
84 return (it == by_id_.end()) ? nullptr : it->second;
85 }
86
88 std::vector<std::string> ids() const {
89 return order_;
90 }
91
99 ValidationReport validate(const NodeGroup& nodes, const ValidationContext& ctx) const {
100 ValidationReport report;
101 report.set_mode(static_cast<int>(ctx.mode));
102
103 for (const auto& id : order_) {
104 auto it = by_id_.find(id);
105 if (it == by_id_.end() || !it->second)
106 continue;
107
108 report.note_contract_run(id);
109
110 try {
111 it->second->validate(nodes, ctx, report);
112 } catch (const std::exception& e) {
113 report.add_issue({
114 .severity = ValidationSeverity::Error,
115 .contract_id = id,
116 .code = "CONTRACT_THREW",
117 .message = std::string("Contract threw exception: ") + e.what(),
118 .node_index = -1,
119 });
120 } catch (...) {
121 report.add_issue({
122 .severity = ValidationSeverity::Error,
123 .contract_id = id,
124 .code = "CONTRACT_THREW",
125 .message = "Contract threw unknown exception",
126 .node_index = -1,
127 });
128 }
129 }
130
131 return report;
132 }
133
134private:
135 std::unordered_map<std::string, ContractPtr> by_id_;
136 std::vector<std::string> order_;
137};
138
139} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.1.