1b222b4490689300b1a77f6b8088b83048cc54f5
[platform/upstream/dldt.git] / inference-engine / src / vpu / graph_transformer / 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 template <typename T>
94 void printTo(std::ostream& os, const T& val) noexcept {
95     try {
96         os << val;
97     } catch (...) {
98         std::cerr << "[VPU] Unknown error while printing\n";
99         std::abort();
100     }
101 }
102
103 template <typename T1, typename T2>
104 void printTo(std::ostream& os, const std::pair<T1, T2>& p) noexcept {
105     try {
106         os << "[" << std::endl;
107
108         os << "first=";
109         printTo(os, p.first);
110         os << std::endl;
111
112         os << "second=";
113         printTo(os, p.second);
114         os << std::endl;
115
116         os << "]";
117     } catch (...) {
118         std::cerr << "[VPU] Unknown error while printing\n";
119         std::abort();
120     }
121 }
122
123 template <class Cont>
124 void printContainer(std::ostream& os, const Cont& cont) noexcept {
125     try {
126         os << "[";
127
128         size_t ind = 0;
129         for (const auto& val : cont) {
130             printTo(os, val);
131             if (ind + 1 < cont.size()) {
132                 os << ", ";
133             }
134             if (ind > 8) {
135                 os << "...";
136                 break;
137             }
138             ++ind;
139         }
140
141         os << "]";
142     } catch (...) {
143         std::cerr << "[VPU] Unknown error while printing\n";
144         std::abort();
145     }
146 }
147
148 template <typename T>
149 void printTo(std::ostream& os, const std::vector<T>& cont) noexcept {
150     printContainer(os, cont);
151 }
152
153 template <typename T, size_t COUNT>
154 void printTo(std::ostream& os, const std::array<T, COUNT>& cont) noexcept {
155     printContainer(os, cont);
156 }
157
158 template <typename T>
159 void printTo(std::ostream& os, const std::set<T>& cont) noexcept {
160     printContainer(os, cont);
161 }
162
163 template <typename T, class H>
164 void printTo(std::ostream& os, const std::unordered_set<T, H>& cont) noexcept {
165     printContainer(os, cont);
166 }
167
168 template <class Map>
169 void printMap(std::ostream& os, const Map& map) noexcept {
170     try {
171         os << "[" << std::endl;
172
173         size_t ind = 0;
174         for (const auto& p : map) {
175             printTo(os, p.first);
176             os << "=";
177             printTo(os, p.second);
178             os << std::endl;
179             if (ind > 16) {
180                 os << "...";
181                 break;
182             }
183             ++ind;
184         }
185
186         os << "]";
187     } catch (...) {
188         std::cerr << "[VPU] Unknown error while printing\n";
189         std::abort();
190     }
191 }
192
193 template <typename K, typename V>
194 void printTo(std::ostream& os, const std::map<K, V>& map) noexcept {
195     printMap(os, map);
196 }
197
198 template <typename K, typename V, class H>
199 void printTo(std::ostream& os, const std::unordered_map<K, V, H>& map) noexcept {
200     printMap(os, map);
201 }
202
203 template <typename T, int Capacity>
204 void printTo(std::ostream& os, const SmallVector<T, Capacity>& cont) noexcept {
205     printContainer(os, cont);
206 }
207
208 template <typename T, typename... Args>
209 void formatPrint(std::ostream& os, const char* str, const T& value, const Args&... args) noexcept {
210     try {
211         while (*str) {
212             if (*str == '%') {
213                 if (*(str + 1) == '%') {
214                     ++str;
215                 } else {
216                     printTo(os, value);
217                     formatPrint(os, str + 2, args...);
218                     return;
219                 }
220             }
221
222             os << *str++;
223         }
224     } catch (...) {
225         std::cerr << "[VPU] Unknown error while printing\n";
226         std::abort();
227     }
228
229     std::cerr << "[VPU] Extra arguments provided to formatPrint\n";
230     std::abort();
231 }
232
233 template <typename T>
234 std::string toString(const T& val) noexcept {
235     std::ostringstream os;
236     printTo(os, val);
237     return os.str();
238 }
239
240 template <typename... Args>
241 std::string formatString(const char* str, const Args&... args) noexcept {
242     std::ostringstream os;
243     formatPrint(os, str, args...);
244     return os.str();
245 }
246
247 }  // namespace vpu