forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathany.cpp
More file actions
260 lines (220 loc) · 6.98 KB
/
any.cpp
File metadata and controls
260 lines (220 loc) · 6.98 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/core/any.hpp"
#include <limits>
#include <string>
namespace ov {
bool Any::equal(std::type_index lhs, std::type_index rhs) {
auto result = lhs == rhs;
#if (defined(__ANDROID__) || defined(__APPLE__)) && defined(__clang__)
if (!result) {
result = std::strcmp(lhs.name(), rhs.name()) == 0;
}
#endif
return result;
}
bool Any::Base::is(const std::type_info& other) const {
return Any::equal(type_info(), other);
}
void Any::Base::type_check(const std::type_info& type_info_) const {
OPENVINO_ASSERT(is(type_info_), "Bad cast from: ", type_info().name(), " To type: ", type_info_.name());
}
std::shared_ptr<RuntimeAttribute> Any::Base::as_runtime_attribute() const {
return {};
}
bool Any::Base::is_copyable() const {
return true;
}
Any Any::Base::init(const std::shared_ptr<Node>&) {
return {};
}
Any Any::Base::merge(const std::vector<std::shared_ptr<Node>>&) {
return {};
}
std::string Any::Base::to_string() {
std::stringstream strm;
print(strm);
return strm.str();
}
std::string Any::Base::to_string() const {
return const_cast<Any::Base*>(this)->to_string();
}
bool Any::Base::visit_attributes(AttributeVisitor&) {
return false;
}
bool Any::Base::visit_attributes(AttributeVisitor& visitor) const {
return const_cast<Any::Base*>(this)->visit_attributes(visitor);
}
void Any::Base::read_to(Base& other) const {
std::stringstream strm;
print(strm);
if (other.is<std::string>()) {
*static_cast<std::string*>(other.addressof()) = strm.str();
} else {
if (!strm.str().empty())
other.read(strm);
}
}
Any::~Any() {
_temp = {};
_impl = {};
}
Any::Any(const Any& other, const std::vector<std::shared_ptr<void>>& so) : _so{so}, _impl{other._impl} {}
Any::Any(const char* str) : Any(std::string{str}) {}
Any::Any(const std::nullptr_t) : Any() {}
void Any::impl_check() const {
if (_impl == nullptr) {
OPENVINO_UNREACHABLE("Any was not initialized.");
}
}
const std::type_info& Any::type_info() const {
impl_check();
return _impl->type_info();
}
bool Any::empty() const {
return _impl == nullptr;
}
void Any::print(std::ostream& ostream) const {
if (_impl != nullptr) {
_impl->print(ostream);
}
}
void Any::read(std::istream& istream) {
if (_impl != nullptr) {
_impl->read(istream);
}
}
bool Any::operator==(const Any& other) const {
if (_impl == nullptr || other._impl == nullptr) {
return false;
}
if (_impl == other._impl) {
return true;
}
return _impl->equal(*other._impl);
}
bool Any::operator==(const std::nullptr_t&) const {
return _impl == nullptr;
}
bool Any::operator!=(const Any& other) const {
return !operator==(other);
}
Any::Base* Any::operator->() {
return _impl.get();
}
const Any::Base* Any::operator->() const {
return _impl.get();
}
void* Any::addressof() {
return _impl != nullptr ? _impl->addressof() : nullptr;
}
const void* Any::addressof() const {
return _impl != nullptr ? _impl->addressof() : nullptr;
}
namespace util {
void Read<bool>::operator()(std::istream& is, bool& value) const {
std::string str;
is >> str;
if (str == "YES") {
value = true;
} else if (str == "NO") {
value = false;
} else {
OPENVINO_UNREACHABLE("Could not convert to bool from string " + str);
}
}
template <typename F>
static auto stream_to(std::istream& is, F&& f) -> decltype(f(std::declval<const std::string&>())) {
std::string str;
is >> str;
try {
return f(str);
} catch (std::exception& e) {
OPENVINO_UNREACHABLE(std::string{"Could not convert to: "} +
typeid(decltype(f(std::declval<const std::string&>()))).name() + " from string \"" + str +
"\": " + e.what());
}
}
void Read<int>::operator()(std::istream& is, int& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stoi(str);
});
}
void Read<long>::operator()(std::istream& is, long& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stol(str);
});
}
void Read<long long>::operator()(std::istream& is, long long& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stoll(str);
});
}
void Read<unsigned>::operator()(std::istream& is, unsigned& value) const {
value = stream_to(is, [](const std::string& str) {
auto ul = std::stoul(str);
if (ul > std::numeric_limits<unsigned>::max()) {
throw std::out_of_range{"Out of range"};
}
return static_cast<unsigned>(ul);
});
}
void Read<unsigned long>::operator()(std::istream& is, unsigned long& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stoul(str);
});
}
void Read<unsigned long long>::operator()(std::istream& is, unsigned long long& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stoull(str);
});
}
void Read<float>::operator()(std::istream& is, float& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stof(str);
});
}
void Read<double>::operator()(std::istream& is, double& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stod(str);
});
}
void Read<long double>::operator()(std::istream& is, long double& value) const {
value = stream_to(is, [](const std::string& str) {
return std::stold(str);
});
}
void Read<std::tuple<unsigned int, unsigned int, unsigned int>>::operator()(
std::istream& is,
std::tuple<unsigned int, unsigned int, unsigned int>& tuple) const {
Read<unsigned int>{}(is, std::get<0>(tuple));
Read<unsigned int>{}(is, std::get<1>(tuple));
Read<unsigned int>{}(is, std::get<2>(tuple));
}
void Read<std::tuple<unsigned int, unsigned int>>::operator()(std::istream& is,
std::tuple<unsigned int, unsigned int>& tuple) const {
Read<unsigned int>{}(is, std::get<0>(tuple));
Read<unsigned int>{}(is, std::get<1>(tuple));
}
void Read<Any>::operator()(std::istream& is, Any& any) const {
any.read(is);
}
void Write<bool>::operator()(std::ostream& os, const bool& b) const {
os << (b ? "YES" : "NO");
}
void Write<std::tuple<unsigned int, unsigned int, unsigned int>>::operator()(
std::ostream& os,
const std::tuple<unsigned int, unsigned int, unsigned int>& tuple) const {
os << std::get<0>(tuple) << " " << std::get<1>(tuple) << " " << std::get<2>(tuple);
}
void Write<std::tuple<unsigned int, unsigned int>>::operator()(
std::ostream& os,
const std::tuple<unsigned int, unsigned int>& tuple) const {
os << std::get<0>(tuple) << " " << std::get<1>(tuple);
}
void Write<Any>::operator()(std::ostream& os, const Any& any) const {
any.print(os);
}
} // namespace util
} // namespace ov