MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Decoder.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "Schema.hpp"
5
6namespace MayaFlux::Nexus {
7
8/**
9 * @class StateDecoder
10 * @brief Patches Fabric state from a previously encoded EXR + JSON schema pair.
11 *
12 * v1 scope: reads {base_path}.exr (schema v2) and patches Emitters, Sensors,
13 * and Agents by id. Dispatches per Kind; entities present in the schema but
14 * absent from the Fabric are logged and counted in missing_count(). Optional
15 * fields (color, size) are only patched when the schema records a non-null
16 * value. Callable name mismatches between schema and live entity are warned
17 * but do not abort the patch.
18 */
19class MAYAFLUX_API StateDecoder {
20public:
21 StateDecoder() = default;
22 ~StateDecoder() = default;
23
24 StateDecoder(const StateDecoder&) = delete;
28
29 /**
30 * @brief Result of a reconstruct() call.
31 */
33 int constructed { 0 };
34 int patched { 0 };
35 int skipped { 0 };
36 std::vector<std::string> warnings;
37 nlohmann::json user_state;
38 };
39
40 /**
41 * @brief Decode and apply to @p fabric.
42 * @param fabric Target fabric. Must already contain entities with
43 * ids matching the schema.
44 * @param base_path Path stem without extension, same value passed to
45 * StateEncoder::encode.
46 * @return True on success. Partial patches (some ids missing) still
47 * return true; failures are logged.
48 */
49 [[nodiscard]] bool decode(Fabric& fabric, const std::string& base_path);
50
51 /**
52 * @brief Patch existing entities and construct missing ones from schema.
53 *
54 * Entities whose id exists in the fabric are patched in place. Missing
55 * entities are constructed, their callables resolved via the fabric's
56 * function registry (no-op fallback with warning if absent), and wired.
57 * Supported wiring kinds for v2: every, move_to, commit_driven.
58 * Unsupported kinds (on, use, bind) fall back to commit_driven + warning.
59 * Hard failure (bad schema, unreadable EXR, dimension mismatch) sets
60 * last_error() and returns a zeroed result.
61 *
62 * @param fabric Target fabric. May be empty, partial, or full.
63 * @param base_path Path stem without extension.
64 * @return Counts and per-entity warnings.
65 */
66 [[nodiscard]] ReconstructionResult reconstruct(Fabric& fabric, const std::string& base_path);
67
68 /**
69 * @brief Reconstruct all Fabrics in @p tapestry from @p base_dir.
70 *
71 * Reads tapestry.json, creates or finds Fabrics by name, calls
72 * reconstruct(fabric, base_path) for each, then restores Tapestry-level
73 * named Expanses and registers them on the listed Fabrics.
74 *
75 * @param tapestry Target Tapestry. May be empty or partially populated.
76 * @param base_dir Directory path matching the one passed to encode().
77 * @return ReconstructionResult aggregated across all Fabrics.
78 */
79 [[nodiscard]] ReconstructionResult reconstruct(Tapestry& tapestry, const std::string& base_dir);
80
81 /**
82 * @brief Last error message, empty if no error.
83 */
84 [[nodiscard]] const std::string& last_error() const { return m_last_error; }
85
86 /**
87 * @brief Number of Emitters actually patched on the last decode call.
88 */
89 [[nodiscard]] size_t patched_count() const { return m_patched_count; }
90
91 /**
92 * @brief Number of ids present in the schema but missing from the fabric.
93 */
94 [[nodiscard]] size_t missing_count() const { return m_missing_count; }
95
96private:
97 std::string m_last_error;
98 size_t m_patched_count { 0 };
99 size_t m_missing_count { 0 };
100};
101
102} // namespace MayaFlux::Nexus
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:38
const std::string & last_error() const
Last error message, empty if no error.
Definition Decoder.hpp:84
StateDecoder(const StateDecoder &)=delete
StateDecoder & operator=(StateDecoder &&)=default
size_t patched_count() const
Number of Emitters actually patched on the last decode call.
Definition Decoder.hpp:89
StateDecoder(StateDecoder &&)=default
size_t missing_count() const
Number of ids present in the schema but missing from the fabric.
Definition Decoder.hpp:94
StateDecoder & operator=(const StateDecoder &)=delete
Patches Fabric state from a previously encoded EXR + JSON schema pair.
Definition Decoder.hpp:19
Owner of one or more Fabrics and the shared state they rely on.
Definition Tapestry.hpp:25