c583c45a7bbde6ee43ae6db497c52f13ddeb8190
[platform/upstream/dldt.git] / inference-engine / src / vpu / common / include / vpu / utils / io.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <iostream>
8 #include <sstream>
9 #include <vector>
10 #include <set>
11 #include <map>
12 #include <unordered_set>
13 #include <unordered_map>
14 #include <utility>
15 #include <string>
16 #include <array>
17
18 #include <ie_data.h>
19 #include <ie_blob.h>
20 #include <ie_layers.h>
21
22 #include <vpu/utils/extra.hpp>
23 #include <vpu/utils/containers.hpp>
24
25 namespace vpu {
26
27 namespace ie = InferenceEngine;
28
29 //
30 // printTo
31 //
32
33 template <typename T>
34 void printTo(std::ostream& os, const T& val) noexcept;
35
36 template <typename T1, typename T2>
37 void printTo(std::ostream& os, const std::pair<T1, T2>& p) noexcept;
38
39 template <typename T>
40 void printTo(std::ostream& os, const std::vector<T>& cont) noexcept;
41
42 template <typename T, size_t COUNT>
43 void printTo(std::ostream& os, const std::array<T, COUNT>& cont) noexcept;
44
45 template <typename T>
46 void printTo(std::ostream& os, const std::set<T>& cont) noexcept;
47
48 template <typename T, class H>
49 void printTo(std::ostream& os, const std::unordered_set<T, H>& cont) noexcept;
50
51 template <typename K, typename V>
52 void printTo(std::ostream& os, const std::map<K, V>& map) noexcept;
53
54 template <typename K, typename V, class H>
55 void printTo(std::ostream& os, const std::unordered_map<K, V, H>& map) noexcept;
56
57 template <typename T, int Capacity>
58 void printTo(std::ostream& os, const SmallVector<T, Capacity>& cont) noexcept;
59
60 class Any;
61 void printTo(std::ostream& os, const Any& any) noexcept;
62
63 class AttributesMap;
64 void printTo(std::ostream& os, const AttributesMap& attrs) noexcept;
65
66 //
67 // formatPrint
68 //
69
70 void formatPrint(std::ostream& os, const char* str) noexcept;
71
72 template <typename T, typename... Args>
73 void formatPrint(std::ostream& os, const char* str, const T& value, const Args&... args) noexcept;
74
75 //
76 // formatString
77 //
78
79 template <typename... Args>
80 std::string formatString(const char* str, const Args&... args) noexcept;
81
82 //
83 // toString
84 //
85
86 template <typename T>
87 std::string toString(const T& val) noexcept;
88
89 //
90 // Implementation
91 //
92
93 namespace details {
94
95 template <typename T>
96 auto printToDefault(std::ostream& os, const T& val, int) noexcept -> decltype(os << val) {
97     try {
98         return os << val;
99     } catch (...) {
100         std::cerr << "[VPU] Unknown error while printing\n";
101         std::abort();
102     }
103 }
104
105 template <typename T>
106 void printToDefault(std::ostream& os, const T& val, ...) noexcept {
107     try {
108         os << "<value at " << &val << ">";
109     } catch (...) {
110         std::cerr << "[VPU] Unknown error while printing\n";
111         std::abort();
112     }
113 }
114
115 }  // namespace details
116
117 template <typename T>
118 inline void printTo(std::ostream& os, const T& val) noexcept {
119     details::printToDefault(os, val, 0);
120 }
121
122 template <typename T1, typename T2>
123 void printTo(std::ostream& os, const std::pair<T1, T2>& p) noexcept {
124     try {
125         os << "[" << std::endl;
126
127         os << "first=";
128         printTo(os, p.first);
129         os << std::endl;
130
131         os << "second=";
132         printTo(os, p.second);
133         os << std::endl;
134
135         os << "]";
136     } catch (...) {
137         std::cerr << "[VPU] Unknown error while printing\n";
138         std::abort();
139     }
140 }
141
142 template <class Cont>
143 void printContainer(std::ostream& os, const Cont& cont) noexcept {
144     try {
145         os << "[";
146
147         size_t ind = 0;
148         for (const auto& val : cont) {
149             printTo(os, val);
150             if (ind + 1 < cont.size()) {
151                 os << ", ";
152             }
153             if (ind > 8) {
154                 os << "...";
155                 break;
156             }
157             ++ind;
158         }
159
160         os << "]";
161     } catch (...) {
162         std::cerr << "[VPU] Unknown error while printing\n";
163         std::abort();
164     }
165 }
166
167 template <typename T>
168 void printTo(std::ostream& os, const std::vector<T>& cont) noexcept {
169     printContainer(os, cont);
170 }
171
172 template <typename T, size_t COUNT>
173 void printTo(std::ostream& os, const std::array<T, COUNT>& cont) noexcept {
174     printContainer(os, cont);
175 }
176
177 template <typename T>
178 void printTo(std::ostream& os, const std::set<T>& cont) noexcept {
179     printContainer(os, cont);
180 }
181
182 template <typename T, class H>
183 void printTo(std::ostream& os, const std::unordered_set<T, H>& cont) noexcept {
184     printContainer(os, cont);
185 }
186
187 template <class Map>
188 void printMap(std::ostream& os, const Map& map) noexcept {
189     try {
190         os << "[" << std::endl;
191
192         size_t ind = 0;
193         for (const auto& p : map) {
194             printTo(os, p.first);
195             os << "=";
196             printTo(os, p.second);
197             os << std::endl;
198             if (ind > 16) {
199                 os << "...";
200                 break;
201             }
202             ++ind;
203         }
204
205         os << "]";
206     } catch (...) {
207         std::cerr << "[VPU] Unknown error while printing\n";
208         std::abort();
209     }
210 }
211
212 template <typename K, typename V>
213 void printTo(std::ostream& os, const std::map<K, V>& map) noexcept {
214     printMap(os, map);
215 }
216
217 template <typename K, typename V, class H>
218 void printTo(std::ostream& os, const std::unordered_map<K, V, H>& map) noexcept {
219     printMap(os, map);
220 }
221
222 template <typename T, int Capacity>
223 void printTo(std::ostream& os, const SmallVector<T, Capacity>& cont) noexcept {
224     printContainer(os, cont);
225 }
226
227 template <typename T, typename... Args>
228 void formatPrint(std::ostream& os, const char* str, const T& value, const Args&... args) noexcept {
229     try {
230         while (*str) {
231             if (*str == '%') {
232                 if (*(str + 1) == '%') {
233                     ++str;
234                 } else {
235                     printTo(os, value);
236                     formatPrint(os, str + 2, args...);
237                     return;
238                 }
239             }
240
241             os << *str++;
242         }
243     } catch (...) {
244         std::cerr << "[VPU] Unknown error while printing\n";
245         std::abort();
246     }
247
248     std::cerr << "[VPU] Extra arguments provided to formatPrint\n";
249     std::abort();
250 }
251
252 template <typename T>
253 std::string toString(const T& val) noexcept {
254     std::ostringstream os;
255     printTo(os, val);
256     return os.str();
257 }
258
259 template <typename... Args>
260 std::string formatString(const char* str, const Args&... args) noexcept {
261     std::ostringstream os;
262     formatPrint(os, str, args...);
263     return os.str();
264 }
265
266 }  // namespace vpu