21#include <nlohmann/json.hpp>
23namespace shamrock::patch {
28 ss <<
"empty table\n";
32 for (
const var_t &v : fields) {
33 v.visit([&](
auto &field) {
34 using f_t =
typename std::remove_reference<
decltype(field)>::type;
35 using base_t =
typename f_t::field_T;
37 ss << index <<
" : " << field.name <<
" : nvar=" << field.nvar <<
" type : ";
39 if (std::is_same<base_t, f32>::value) {
41 }
else if (std::is_same<base_t, f32_2>::value) {
43 }
else if (std::is_same<base_t, f32_3>::value) {
45 }
else if (std::is_same<base_t, f32_4>::value) {
47 }
else if (std::is_same<base_t, f32_8>::value) {
49 }
else if (std::is_same<base_t, f32_16>::value) {
51 }
else if (std::is_same<base_t, f64>::value) {
53 }
else if (std::is_same<base_t, f64_2>::value) {
55 }
else if (std::is_same<base_t, f64_3>::value) {
57 }
else if (std::is_same<base_t, f64_4>::value) {
59 }
else if (std::is_same<base_t, f64_8>::value) {
61 }
else if (std::is_same<base_t, f64_16>::value) {
63 }
else if (std::is_same<base_t, u32>::value) {
65 }
else if (std::is_same<base_t, u64>::value) {
67 }
else if (std::is_same<base_t, u32_3>::value) {
69 }
else if (std::is_same<base_t, u64_3>::value) {
71 }
else if (std::is_same<base_t, i64_3>::value) {
88 std::vector<std::string> ret;
90 for (var_t &v : fields) {
91 v.visit([&](
auto &field) {
92 ret.push_back(field.name);
100 for (
const var_t &fvar : fields) {
101 if (fvar.visit_return([&](
const auto &arg) {
102 return field_name == arg.name;
115 "add_field -> the name already exists");
119 "PatchDataLayerLayout",
126 fields.push_back(var_t{FieldDescriptor<T>(field_name, nvar)});
131 for (
u32 i = 0; i < fields.size(); i++) {
132 if (
const FieldDescriptor<T> *pval
133 = std::get_if<FieldDescriptor<T>>(&fields[i].value)) {
134 if (pval->name == field_name) {
141 "the requested field does not exists\n the function : {}\n the field name : {}\n "
142 " current table : \n{}",
150 for (
u32 i = 0; i < fields.size(); i++) {
151 if (
const FieldDescriptor<T> *pval
152 = std::get_if<FieldDescriptor<T>>(&fields[i].value)) {
153 if ((pval->name == field_name) && (pval->nvar == nvar)) {
165 using json = nlohmann::json;
167 std::vector<json> entries;
170 using f_t =
typename std::remove_reference<
decltype(field)>::type;
171 using base_t =
typename f_t::field_T;
173 auto get_tname = []() {
174 if (std::is_same<base_t, f32>::value) {
176 }
else if (std::is_same<base_t, f32_2>::value) {
178 }
else if (std::is_same<base_t, f32_3>::value) {
180 }
else if (std::is_same<base_t, f32_4>::value) {
182 }
else if (std::is_same<base_t, f32_8>::value) {
184 }
else if (std::is_same<base_t, f32_16>::value) {
186 }
else if (std::is_same<base_t, f64>::value) {
188 }
else if (std::is_same<base_t, f64_2>::value) {
190 }
else if (std::is_same<base_t, f64_3>::value) {
192 }
else if (std::is_same<base_t, f64_4>::value) {
194 }
else if (std::is_same<base_t, f64_8>::value) {
196 }
else if (std::is_same<base_t, f64_16>::value) {
198 }
else if (std::is_same<base_t, u32>::value) {
200 }
else if (std::is_same<base_t, u64>::value) {
202 }
else if (std::is_same<base_t, u32_3>::value) {
204 }
else if (std::is_same<base_t, u64_3>::value) {
206 }
else if (std::is_same<base_t, i64_3>::value) {
216 {
"type", get_tname()},
217 {
"nvar", field.nvar},
218 {
"field_name", field.name},
226 for (
auto &entry : j) {
227 p.
add_field_t(entry[
"field_name"], entry[
"nvar"].get<u32>(), entry[
"type"]);
234 ret = ret && (lhs.fields.size() == rhs.fields.size());
236 for (
u32 i = 0; i < lhs.fields.size(); i++) {
237 const PatchDataLayerLayout::var_t &var_lhs = lhs.fields[i];
238 const PatchDataLayerLayout::var_t &var_rhs = rhs.fields[i];
241 [&](
auto &flhs,
auto &frhs) {
242 using t1 =
typename std::remove_reference<
decltype(flhs)>::type;
243 using t2 =
typename std::remove_reference<
decltype(frhs)>::type;
245 ret = ret && std::is_same_v<t1, t2>;
246 ret = ret && (flhs.nvar == frhs.nvar);
247 ret = ret && (flhs.name == frhs.name);
265 template void shamrock::patch::PatchDataLayerLayout::add_field<a>( \
266 const std::string &field_name, u32 nvar, SourceLocation loc); \
267 template u32 shamrock::patch::PatchDataLayerLayout::get_field_idx<a>( \
268 const std::string &field_name) const; \
269 template u32 shamrock::patch::PatchDataLayerLayout::get_field_idx<a>( \
270 const std::string &field_name, u32 nvar) const;
271XMAC_LIST_ENABLED_FIELD
void from_json(const nlohmann::json &j, PatchDataLayerLayout &p)
Deserialize a PatchDataLayerLayout object from a JSON object.
void to_json(nlohmann::json &j, const PatchDataLayerLayout &p)
Serialize a PatchDataLayerLayout object to a JSON object.
std::uint32_t u32
32 bit unsigned integer
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
friend bool operator==(const PatchDataLayerLayout &lhs, const PatchDataLayerLayout &rhs)
Overloaded equality operator for PatchDataLayerLayout class.
void add_field(const std::string &field_name, u32 nvar, SourceLocation loc=SourceLocation{})
add a field of type T to the layout
std::vector< std::string > get_field_names()
Get the list of field names.
bool has_field_name(const std::string &field_name) const
Check whether a field with the given name already exists.
void for_each_field_any(Functor &&func) const
for each visit of each field
void add_field_t(std::string fname, u32 nvar, std::string type)
Add a field with type specified as a string.
std::string get_description_str() const
Get the description of the layout.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
provide information about the source location
std::string format_one_line() const
format the location in a one liner