Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef GKFS_IO_SERIALIZE_HPP
#define GKFS_IO_SERIALIZE_HPP
#include <nlohmann/json.hpp>
template <typename T>
nlohmann::json
serialize(const T& object) {
using json = nlohmann::json;
json j;
constexpr auto n = std::tuple_size<decltype(T::properties)>::value;
refl::for_sequence(std::make_index_sequence<n>{},
[&](auto i) {
constexpr auto p = std::get<i>(T::properties);
//j[p.name] = json {
// { "type" , p.type },
// { "value" , object.*(p.member) }
//};
j[p.name] = object.*(p.member);
}
);
return j;
}
namespace nlohmann {
// ADL specialization for pointers to complete types that
// we want serialized
template <typename T>
struct adl_serializer<T*> {
static void to_json(json& j, const T* opt) {
if (opt) {
j = *opt;
} else {
j = nullptr;
}
}
};
// ADL specialization for C strings
template <>
struct adl_serializer<const char*> {
static void to_json(json& j, const char* opt) {
if (opt) {
j = std::string{opt};
} else {
j = std::string{};
}
}
};
// base serializer for opaque pointers
template <typename T>
struct opaque_ptr_serializer {
static void to_json(json& j, const T opt) {
if (opt) {
j = reinterpret_cast<uint64_t>(opt);
} else {
j = nullptr;
}
}
};
// ADL specialization for opaque ::DIR* type
template <>
struct adl_serializer<::DIR*> : public opaque_ptr_serializer<::DIR*> {
using opaque_ptr_serializer<::DIR*>::to_json;
};
// ADL specialization for void* type
template <>
struct adl_serializer<void*> : public opaque_ptr_serializer<void*> {
using opaque_ptr_serializer<void*>::to_json;
};
// ADL specialization for struct ::timespec type
template <>
struct adl_serializer<struct ::timespec> {
static void to_json(json& j, const struct ::timespec opt) {
j = json {
{ "tv_sec", opt.tv_sec },
{ "tv_nsec", opt.tv_nsec }
};
}
};
// ADL specialization for struct ::dirent type
template <>
struct adl_serializer<struct ::dirent> {
static void to_json(json& j, const struct ::dirent opt) {
j = json {
{ "d_ino", opt.d_ino },
{ "d_off", opt.d_off },
{ "d_reclen", opt.d_reclen },
{ "d_type", opt.d_type },
{ "d_name", opt.d_name },
};
}
};
//std::ostream&
//operator<<(std::ostream& os, const struct ::dirent& d) {
// return os << "hello there!\n";
//}
// ADL specialization for struct ::stat type
template <>
struct adl_serializer<struct ::stat> {
static void to_json(json& j, const struct ::stat opt) {
j = json {
{ "st_dev", opt.st_dev },
{ "st_ino", opt.st_ino },
{ "st_mode", opt.st_mode },
{ "st_nlink", opt.st_nlink },
{ "st_uid", opt.st_uid },
{ "st_gid", opt.st_gid },
{ "st_rdev", opt.st_rdev },
{ "st_size", opt.st_size },
{ "st_blksize", opt.st_blksize },
{ "st_blocks", opt.st_blocks },
{ "st_atim", opt.st_atim },
{ "st_mtim", opt.st_mtim },
{ "st_ctim", opt.st_ctim }
};
}
};
} // namespace nlohmann
template <> struct fmt::formatter<struct ::dirent> {
constexpr auto parse(format_parse_context &ctx) {
// [ctx.begin(), ctx.end()) is a character range that contains a part of
// the format string starting from the format specifications to be parsed,
// e.g. in
//
// fmt::format("{:f} - point of interest", point{1, 2});
//
// the range will contain "f} - point of interest". The formatter should
// parse specifiers until '}' or the end of the range. In this example
// the formatter should parse the 'f' specifier and return an iterator
// pointing to '}'.
// Parse the presentation format and store it in the formatter:
auto it = ctx.begin(), end = ctx.end();
// Check if reached the end of the range:
if (it != end && *it != '}')
throw format_error("invalid format");
// Return an iterator past the end of the parsed range:
return it;
}
template <typename FormatContext>
auto format(const struct ::dirent &dirent, FormatContext &ctx) {
return format_to(ctx.out(),
"struct dirent {{\n"
" d_ino = {};\n"
" d_off = {};\n"
" d_reclen = {};\n"
" d_type = {};\n"
" d_name = {};\n"
"}}",
dirent.d_ino,
dirent.d_off,
dirent.d_reclen,
dirent.d_type,
dirent.d_name);
}
};
#endif // GKFS_IO_SERIALIZE_HPP