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

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.