1 | /* |
2 | * SPDX-License-Identifier: Apache-2.0 |
3 | */ |
4 | |
5 | #pragma once |
6 | |
7 | #include <sstream> |
8 | #include <string> |
9 | |
10 | namespace ONNX_NAMESPACE { |
11 | |
12 | #if defined(__ANDROID__) |
13 | template <typename T> |
14 | std::string to_string(T value) { |
15 | std::ostringstream os; |
16 | os << value; |
17 | return os.str(); |
18 | } |
19 | |
20 | inline int stoi(const std::string& str) { |
21 | std::stringstream ss; |
22 | int n = 0; |
23 | ss << str; |
24 | ss >> n; |
25 | return n; |
26 | } |
27 | |
28 | #else |
29 | using std::stoi; |
30 | using std::to_string; |
31 | #endif // defined(__ANDROID__) |
32 | |
33 | inline void MakeStringInternal(std::stringstream& /*ss*/) {} |
34 | |
35 | template <typename T> |
36 | inline void MakeStringInternal(std::stringstream& ss, const T& t) { |
37 | ss << t; |
38 | } |
39 | |
40 | template <typename T, typename... Args> |
41 | inline void MakeStringInternal(std::stringstream& ss, const T& t, const Args&... args) { |
42 | MakeStringInternal(ss, t); |
43 | MakeStringInternal(ss, args...); |
44 | } |
45 | |
46 | template <typename... Args> |
47 | std::string MakeString(const Args&... args) { |
48 | std::stringstream ss; |
49 | MakeStringInternal(ss, args...); |
50 | return std::string(ss.str()); |
51 | } |
52 | |
53 | // Specializations for already-a-string types. |
54 | template <> |
55 | inline std::string MakeString(const std::string& str) { |
56 | return str; |
57 | } |
58 | inline std::string MakeString(const char* c_str) { |
59 | return std::string(c_str); |
60 | } |
61 | } // namespace ONNX_NAMESPACE |
62 | |