CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
introspection_helper.hpp
Go to the documentation of this file.
1
16#pragma once
17
19#include <sertial/core/layout/struct_layout_reflector.hpp>
20#include <rfl.hpp>
21#include <rfl/json.hpp>
22#include <string>
23#include <vector>
24#include <fstream>
25#include <sstream>
26#include <tuple>
27
28namespace commrat {
29
60template<typename UserRegistry>
117 template<typename T, typename Writer = rfl::json::Writer>
118 static std::string export_as() {
119 using Schema = MessageSchema<T, UserRegistry>;
120 return rfl::json::write(Schema{});
121 }
122
157 template<typename Writer = rfl::json::Writer>
158 static std::string export_all() {
159 using PayloadTuple = typename UserRegistry::PayloadTypes;
160 return export_all_impl<Writer>(PayloadTuple{});
161 }
162
189 template<typename Writer = rfl::json::Writer>
190 static void write_to_file(const std::string& filename) {
191 auto data = export_all<Writer>();
192 std::ofstream file(filename);
193 if (!file) {
194 throw std::runtime_error("Failed to open file: " + filename);
195 }
196 file << data;
197 }
198
199private:
209 template<typename Writer, typename... PayloadTypes>
210 static std::string export_all_impl(std::tuple<PayloadTypes...>) {
211 // Create tuple of all schemas - rfl will serialize this directly!
212 auto all_schemas = std::make_tuple(
214 );
215
216 // Let rfl do its magic - it serializes tuples as arrays
217 return rfl::json::write(all_schemas);
218 }
219};
220
221} // namespace commrat
Complete schema combining CommRaT metadata with SeRTial layout.
CommRaT - Modern C++ Real-Time Communication Framework.
Registry-wide introspection and schema export.
static void write_to_file(const std::string &filename)
Convenience: write all schemas to file.
static std::string export_all()
Export complete schemas for all registered messages.
static std::string export_as()
Export complete schema for a message type.
Complete schema for a CommRaT message type.