Skip to main content

Validators.h File

Built-in contract implementations and default registry. More...

Included Headers

#include <memory> #include <string> #include <utility> #include "builder/Node.h" #include "contracts/Contract.h" #include "contracts/ContractRegistry.h" #include "contracts/ValidationReport.h"

Namespaces Index

namespacesimaai
namespaceneat
namespacevalidators

Description

Built-in contract implementations and default registry.

Provides the small set of header-only Contract factories that ship with the framework — non-empty pipeline, no-null nodes, sink-last-for-run, RTSP source presence — plus DefaultRegistry(), the recommended starting point for most callers. Library code can compose its own registry by cloning DefaultRegistry() and adding/removing contracts.

See Also

Contract

See Also

ContractRegistry

File Listing

The file content with the documentation metadata removed is:

1
15// include/contracts/Validators.h
16#pragma once
17
18#include <memory>
19#include <string>
20#include <utility>
21
22#include "builder/Node.h"
26
27namespace simaai::neat {
28namespace validators {
29
30// -----------------------------
31// Built-in Contract factories
32// -----------------------------
33
42inline std::shared_ptr<Contract> NonEmptyPipeline() {
43 class C final : public Contract {
44 public:
45 std::string id() const override {
46 return "NonEmptyPipeline";
47 }
48 std::string description() const override {
49 return "Pipeline must contain at least one node.";
50 }
51
52 void validate(std::span<const std::shared_ptr<Node>> nodes, const ValidationContext& ctx,
53 ValidationReport& r) const override {
54 (void)ctx;
55 if (nodes.empty()) {
56 r.add_error(id(), "EMPTY_PIPELINE", "No nodes were added to the pipeline.");
57 }
58 }
59 };
60 return std::make_shared<C>();
61}
62
71inline std::shared_ptr<Contract> NoNullNodes() {
72 class C final : public Contract {
73 public:
74 std::string id() const override {
75 return "NoNullNodes";
76 }
77 std::string description() const override {
78 return "All nodes must be non-null shared_ptr.";
79 }
80
81 void validate(std::span<const std::shared_ptr<Node>> nodes, const ValidationContext& ctx,
82 ValidationReport& r) const override {
83 (void)ctx;
84 for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
85 if (!nodes[static_cast<std::size_t>(i)]) {
86 r.add_error(id(), "NULL_NODE", "Null node pointer in pipeline node list.", i);
87 }
88 }
89 }
90 };
91 return std::make_shared<C>();
92}
93
106inline std::shared_ptr<Contract> SinkLastForRun(std::string sink_kind = "Output") {
107 class C final : public Contract {
108 public:
109 explicit C(std::string kind) : sink_kind_(std::move(kind)) {}
110 std::string id() const override {
111 return "SinkLastForRun";
112 }
113 std::string description() const override {
114 return "When running, the pipeline must end with the terminal appsink node.";
115 }
116
117 void validate(std::span<const std::shared_ptr<Node>> nodes, const ValidationContext& ctx,
118 ValidationReport& r) const override {
119 if (ctx.mode != ValidationContext::Mode::Run)
120 return;
121 if (nodes.empty())
122 return;
123
124 int last_idx = static_cast<int>(nodes.size()) - 1;
125 const auto& last = nodes.back();
126 const std::string last_kind = last ? last->kind() : "";
127
128 // Require sink kind last.
129 if (!last || last_kind != sink_kind_) {
130 r.add_error(id(), "SINK_NOT_LAST", "Last node must be " + sink_kind_ + " for run().",
131 last_idx, last_kind, last ? last->user_label() : "");
132 }
133
134 // Disallow additional sinks earlier (best-effort sanity).
135 for (int i = 0; i < last_idx; ++i) {
136 const auto& n = nodes[static_cast<std::size_t>(i)];
137 if (n && n->kind() == sink_kind_) {
138 r.add_error(id(), "MULTIPLE_SINKS",
139 "Found " + sink_kind_ + " before the end of the pipeline.", i, n->kind(),
140 n->user_label());
141 }
142 }
143 }
144
145 private:
146 std::string sink_kind_;
147 };
148 return std::make_shared<C>(std::move(sink_kind));
149}
150
162inline std::shared_ptr<Contract> RtspRequiresSource(std::string source_kind = "StillImageInput") {
163 class C final : public Contract {
164 public:
165 explicit C(std::string k) : src_kind_(std::move(k)) {}
166 std::string id() const override {
167 return "RtspRequiresSource";
168 }
169 std::string description() const override {
170 return "RTSP mode requires a server-side source node (e.g., StillImageInput).";
171 }
172
173 void validate(std::span<const std::shared_ptr<Node>> nodes, const ValidationContext& ctx,
174 ValidationReport& r) const override {
175 if (ctx.mode != ValidationContext::Mode::Rtsp)
176 return;
177
178 bool found = false;
179 for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
180 const auto& n = nodes[static_cast<std::size_t>(i)];
181 if (n && n->kind() == src_kind_) {
182 found = true;
183 break;
184 }
185 }
186
187 if (!found) {
188 r.add_error(id(), "RTSP_SOURCE_MISSING",
189 "RTSP mode requires a node of kind \"" + src_kind_ + "\".", -1, src_kind_, "");
190 }
191 }
192
193 private:
194 std::string src_kind_;
195 };
196 return std::make_shared<C>(std::move(source_kind));
197}
198
199// -----------------------------
200// Default registry
201// -----------------------------
202
215 reg.add(NonEmptyPipeline());
216 reg.add(NoNullNodes());
217 reg.add(SinkLastForRun());
219 return reg;
220}
221
222} // namespace validators
223} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.