Skip to main content

ContractRegistry Class

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

Declaration

class simaai::neat::ContractRegistry { ... }

Included Headers

#include <ContractRegistry.h>

Public Member Typedefs Index

usingContractPtr = std::shared_ptr< Contract >

Shared-pointer alias for a Contract. More...

Public Constructors Index

ContractRegistry ()=default

Construct an empty registry. More...

Public Member Functions Index

ContractRegistry &add (ContractPtr c)

Add or replace a contract (keyed by id()); returns *this for chaining. More...

template <class T, class... Args>
ContractRegistry &emplace (Args &&... args)

Convenience: construct a contract of type T from args and add it. More...

boolremove (const std::string &id)

Remove a contract by id. Returns true if removed. More...

voidclear ()

Drop all contracts; the registry becomes empty. More...

std::size_tsize () const noexcept

Number of contracts currently registered. More...

boolempty () const noexcept

True if no contracts are registered. More...

ContractPtrget (const std::string &id) const

Get a contract by id (nullptr if missing). More...

std::vector< std::string >ids () const

Deterministic list of ids in insertion order. More...

ValidationReportvalidate (std::span< const std::shared_ptr< Node > > nodes, const ValidationContext &ctx) const

Run all contracts and return a ValidationReport. More...

Private Member Attributes Index

std::unordered_map< std::string, ContractPtr >by_id_
std::vector< std::string >order_

Description

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

Insertion order is preserved (and used as evaluation order). Adding a contract whose id() matches an existing one replaces the old contract in place. The registry is intentionally small and STL-only.

See Also

Contract

See Also

ValidationReport

See Also

Validators::DefaultRegistry

Definition at line 41 of file ContractRegistry.h.

Public Member Typedefs

ContractPtr

using simaai::neat::ContractRegistry::ContractPtr = std::shared_ptr<Contract>

Shared-pointer alias for a Contract.

Definition at line 44 of file ContractRegistry.h.

44 using ContractPtr = std::shared_ptr<Contract>;

Public Constructors

ContractRegistry()

simaai::neat::ContractRegistry::ContractRegistry ()
default

Construct an empty registry.

Definition at line 47 of file ContractRegistry.h.

Public Member Functions

add()

ContractRegistry & simaai::neat::ContractRegistry::add (ContractPtr c)
inline

Add or replace a contract (keyed by id()); returns *this for chaining.

Definition at line 50 of file ContractRegistry.h.

51 if (!c)
52 return *this;
53 const std::string cid = c->id();
54 if (cid.empty())
55 return *this;
56
57 auto it = by_id_.find(cid);
58 if (it == by_id_.end()) {
59 order_.push_back(cid);
60 }
61 by_id_[cid] = std::move(c);
62 return *this;
63 }

clear()

void simaai::neat::ContractRegistry::clear ()
inline

Drop all contracts; the registry becomes empty.

Definition at line 88 of file ContractRegistry.h.

88 void clear() {
89 by_id_.clear();
90 order_.clear();
91 }

emplace()

template <class T, class... Args>
ContractRegistry & simaai::neat::ContractRegistry::emplace (Args &&... args)
inline

Convenience: construct a contract of type T from args and add it.

Definition at line 66 of file ContractRegistry.h.

66 template <class T, class... Args> ContractRegistry& emplace(Args&&... args) {
67 return add(std::make_shared<T>(std::forward<Args>(args)...));
68 }

empty()

bool simaai::neat::ContractRegistry::empty ()
inline noexcept

True if no contracts are registered.

Definition at line 98 of file ContractRegistry.h.

98 bool empty() const noexcept {
99 return by_id_.empty();
100 }

get()

ContractPtr simaai::neat::ContractRegistry::get (const std::string & id)
inline

Get a contract by id (nullptr if missing).

Definition at line 103 of file ContractRegistry.h.

103 ContractPtr get(const std::string& id) const {
104 auto it = by_id_.find(id);
105 return (it == by_id_.end()) ? nullptr : it->second;
106 }

ids()

std::vector< std::string > simaai::neat::ContractRegistry::ids ()
inline

Deterministic list of ids in insertion order.

Definition at line 109 of file ContractRegistry.h.

109 std::vector<std::string> ids() const {
110 return order_;
111 }

remove()

bool simaai::neat::ContractRegistry::remove (const std::string & id)
inline

Remove a contract by id. Returns true if removed.

Definition at line 71 of file ContractRegistry.h.

71 bool remove(const std::string& id) {
72 auto it = by_id_.find(id);
73 if (it == by_id_.end())
74 return false;
75 by_id_.erase(it);
76
77 // Keep deterministic order_: erase id if present.
78 for (auto oit = order_.begin(); oit != order_.end(); ++oit) {
79 if (*oit == id) {
80 order_.erase(oit);
81 break;
82 }
83 }
84 return true;
85 }

size()

std::size_t simaai::neat::ContractRegistry::size ()
inline noexcept

Number of contracts currently registered.

Definition at line 94 of file ContractRegistry.h.

94 std::size_t size() const noexcept {
95 return by_id_.size();
96 }

validate()

ValidationReport simaai::neat::ContractRegistry::validate (std::span< const std::shared_ptr< Node > > nodes, const ValidationContext & ctx)
inline

Run all contracts and return a ValidationReport.

Defensive behavior:

  • contract violations should be reported (not thrown)
  • if a Contract throws, registry converts that into an internal ERROR issue

Definition at line 120 of file ContractRegistry.h.

120 ValidationReport validate(std::span<const std::shared_ptr<Node>> nodes,
121 const ValidationContext& ctx) const {
122 ValidationReport report;
123 report.set_mode(static_cast<int>(ctx.mode));
124
125 for (const auto& id : order_) {
126 auto it = by_id_.find(id);
127 if (it == by_id_.end() || !it->second)
128 continue;
129
130 report.note_contract_run(id);
131
132 try {
133 it->second->validate(nodes, ctx, report);
134 } catch (const std::exception& e) {
135 report.add_issue({
136 .severity = ValidationSeverity::Error,
137 .contract_id = id,
138 .code = "CONTRACT_THREW",
139 .message = std::string("Contract threw exception: ") + e.what(),
140 .node_index = -1,
141 });
142 } catch (...) {
143 report.add_issue({
144 .severity = ValidationSeverity::Error,
145 .contract_id = id,
146 .code = "CONTRACT_THREW",
147 .message = "Contract threw unknown exception",
148 .node_index = -1,
149 });
150 }
151 }
152
153 return report;
154 }

Private Member Attributes

by_id_

std::unordered_map<std::string, ContractPtr> simaai::neat::ContractRegistry::by_id_

Definition at line 157 of file ContractRegistry.h.

157 std::unordered_map<std::string, ContractPtr> by_id_;

order_

std::vector<std::string> simaai::neat::ContractRegistry::order_

Definition at line 158 of file ContractRegistry.h.

158 std::vector<std::string> order_;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.