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 >

Public Constructors Index

ContractRegistry ()=default

Public Member Functions Index

ContractRegistry &add (ContractPtr c)

Add/replace a contract by id(). More...

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

Convenience: construct + add. More...

boolremove (const std::string &id)

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

voidclear ()
std::size_tsize () const noexcept
boolempty () const noexcept
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 (const NodeGroup &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.

This is intentionally small and STL-only.

Definition at line 25 of file ContractRegistry.h.

Public Member Typedefs

ContractPtr

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

Definition at line 27 of file ContractRegistry.h.

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

Public Constructors

ContractRegistry()

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

Definition at line 29 of file ContractRegistry.h.

Public Member Functions

add()

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

Add/replace a contract by id().

Definition at line 32 of file ContractRegistry.h.

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 }

clear()

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

Definition at line 69 of file ContractRegistry.h.

69 void clear() {
70 by_id_.clear();
71 order_.clear();
72 }

emplace()

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

Convenience: construct + add.

Definition at line 48 of file ContractRegistry.h.

48 template <class T, class... Args> ContractRegistry& emplace(Args&&... args) {
49 return add(std::make_shared<T>(std::forward<Args>(args)...));
50 }

empty()

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

Definition at line 77 of file ContractRegistry.h.

77 bool empty() const noexcept {
78 return by_id_.empty();
79 }

get()

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

Get a contract by id (nullptr if missing).

Definition at line 82 of file ContractRegistry.h.

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 }

ids()

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

Deterministic list of ids in insertion order.

Definition at line 88 of file ContractRegistry.h.

88 std::vector<std::string> ids() const {
89 return order_;
90 }

remove()

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

Remove a contract by id. Returns true if removed.

Definition at line 53 of file ContractRegistry.h.

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 }

size()

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

Definition at line 74 of file ContractRegistry.h.

74 std::size_t size() const noexcept {
75 return by_id_.size();
76 }

validate()

ValidationReport simaai::neat::ContractRegistry::validate (const NodeGroup & 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 99 of file ContractRegistry.h.

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 }

Private Member Attributes

by_id_

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

Definition at line 135 of file ContractRegistry.h.

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

order_

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

Definition at line 136 of file ContractRegistry.h.

136 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.1.