FastLED 3.9.15
Loading...
Searching...
No Matches
type_schema.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/json.h"
4#include "fl/stl/stdint.h"
5#include "fl/stl/string.h"
7#include "fl/stl/vector.h"
8
9namespace fl {
10namespace detail {
11
12// =============================================================================
13// TypeSchema - Generate JSON Schema for C++ types
14// =============================================================================
15
16// Primary template - unknown types
17template <typename T, typename Enable = void>
18struct TypeSchema {
19 static const char* typeName() {
20 return "unknown";
21 }
22};
23
24// Integer types (excluding bool)
25template <typename T>
26struct TypeSchema<T, typename fl::enable_if<
27 fl::is_integral<T>::value && !fl::is_same<T, bool>::value
28>::type> {
29 static const char* typeName() {
30 return "integer";
31 }
32};
33
34// Boolean type
35template <>
36struct TypeSchema<bool, void> {
37 static const char* typeName() {
38 return "boolean";
39 }
40};
41
42// Floating point types
43template <typename T>
44struct TypeSchema<T, typename fl::enable_if<fl::is_floating_point<T>::value>::type> {
45 static const char* typeName() {
46 return "number";
47 }
48};
49
50// String type
51template <>
52struct TypeSchema<fl::string, void> {
53 static const char* typeName() {
54 return "string";
55 }
56};
57
58// Void type (for return types)
59template <>
60struct TypeSchema<void, void> {
61 static const char* typeName() {
62 return "void";
63 }
64};
65
66// =============================================================================
67// MethodSchema - Generate schema for a function signature
68// =============================================================================
69
70template <typename Sig>
72
73// Specialization for non-void return
74template <typename R, typename... Args>
75struct MethodSchema<R(Args...)> {
76 // Flat params: [["name", "type"], ...] optimized for low-memory devices
77 static json params(const fl::vector<fl::string>& names) {
78 json arr = json::array();
79 addParams<0, Args...>(arr, names);
80 return arr;
81 }
82
83 static const char* resultTypeName() {
85 }
86
87private:
88 template <fl::size Index>
89 static void addParams(json& arr, const fl::vector<fl::string>& names) {
90 // Base case: no more parameters
91 (void)arr;
92 (void)names;
93 }
94
95 template <fl::size Index, typename First, typename... Rest>
96 static void addParams(json& arr, const fl::vector<fl::string>& names) {
97 json param = json::array();
98 // Name
99 if (Index < names.size() && !names[Index].empty()) {
100 param.push_back(names[Index].c_str());
101 } else {
102 param.push_back("arg" + fl::to_string(static_cast<i64>(Index)));
103 }
104 // Type name
106 arr.push_back(param);
107 addParams<Index + 1, Rest...>(arr, names);
108 }
109};
110
111// Specialization for void return
112template <typename... Args>
113struct MethodSchema<void(Args...)> {
114 // Flat params: [["name", "type"], ...] optimized for low-memory devices
115 static json params(const fl::vector<fl::string>& names) {
116 json arr = json::array();
117 addParams<0, Args...>(arr, names);
118 return arr;
119 }
120
121 static const char* resultTypeName() {
123 }
124
125private:
126 template <fl::size Index>
127 static void addParams(json& arr, const fl::vector<fl::string>& names) {
128 // Base case: no more parameters
129 (void)arr;
130 (void)names;
131 }
132
133 template <fl::size Index, typename First, typename... Rest>
134 static void addParams(json& arr, const fl::vector<fl::string>& names) {
135 json param = json::array();
136 // Name
137 if (Index < names.size() && !names[Index].empty()) {
138 param.push_back(names[Index].c_str());
139 } else {
140 param.push_back("arg" + fl::to_string(static_cast<i64>(Index)));
141 }
142 // Type name
144 arr.push_back(param);
145 addParams<Index + 1, Rest...>(arr, names);
146 }
147};
148
149} // namespace detail
150} // namespace fl
void push_back(const json &value) FL_NOEXCEPT
Definition json.h:745
static json array() FL_NOEXCEPT
Definition json.h:688
fl::size size() const FL_NOEXCEPT
bool empty() const FL_NOEXCEPT
FastLED's Elegant JSON Library: fl::json
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
string to_string(T value) FL_NOEXCEPT
Definition string.h:450
fl::i64 i64
Definition s16x16x4.h:222
Base definition for an LED controller.
Definition crgb.hpp:179
static void addParams(json &arr, const fl::vector< fl::string > &names)
Definition type_schema.h:89
static void addParams(json &arr, const fl::vector< fl::string > &names)
Definition type_schema.h:96
static const char * resultTypeName()
Definition type_schema.h:83
static json params(const fl::vector< fl::string > &names)
Definition type_schema.h:77
static void addParams(json &arr, const fl::vector< fl::string > &names)
static json params(const fl::vector< fl::string > &names)
static void addParams(json &arr, const fl::vector< fl::string > &names)
static const char * typeName()
Definition type_schema.h:19