-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_JSON.h
145 lines (130 loc) · 3.38 KB
/
LibXPUInfo_JSON.h
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
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef XPUINFO_USE_RAPIDJSON
#include "LibXPUInfo.h"
#include "LibXPUInfo_Util.h" // for XI::convert
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/istreamwrapper.h>
// Support serialization
#include <optional>
#define XPUINFO_JSON_VERSION "0.0.1"
namespace XI
{
namespace JSON
{
template <typename T>
const char* safeGetValString(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsString())
{
return val[valName].GetString();
}
}
return nullptr;
}
template <typename T>
std::string safeGetString(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsString())
{
return val[valName].GetString();
}
}
return std::string();
}
template <typename T>
std::wstring safeGetWString(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsString())
{
return convert(val[valName].GetString());
}
}
return std::wstring();
}
template <typename T>
std::optional<XI::UI64> safeGetUI64(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsUint64())
{
return val[valName].GetUint64();
}
}
return std::nullopt;
}
template <typename T>
std::optional<XI::UI64> safeGetI64(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsInt64())
{
return val[valName].GetInt64();
}
}
return std::nullopt;
}
template <typename T>
std::optional<XI::UI32> safeGetUI32(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsUint())
{
return val[valName].GetUint();
}
}
return std::nullopt;
}
template <typename T>
std::optional<XI::I32> safeGetI32(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsInt())
{
return val[valName].GetInt();
}
}
return std::nullopt;
}
template <typename T>
std::optional<double> safeGetDouble(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsDouble())
{
return val[valName].GetDouble();
}
}
return std::nullopt;
}
template <typename T>
std::optional<bool> safeGetBool(const T& val, const char* valName)
{
if (val.HasMember(valName))
{
if (val[valName].IsBool())
{
return val[valName].GetBool();
}
}
return std::nullopt;
}
// For validating original vs. deserialized objects
bool XPUINFO_EXPORT compareXI(const XPUInfoPtr& pXI, const XPUInfoPtr& pXID);
} // JSON
} // XI
#endif // XPUINFO_USE_RAPIDJSON