HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_string.h
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2016-present, Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Portions Copyright (c) Microsoft Corporation
17 
18 #pragma once
19 
20 #include <locale>
21 #include <sstream>
22 #include <type_traits>
23 
24 namespace onnxruntime {
25 
26 namespace detail {
27 
28 inline void MakeStringImpl(std::ostringstream& /*ss*/) noexcept {
29 }
30 
31 template <typename T>
32 inline void MakeStringImpl(std::ostringstream& ss, const T& t) noexcept {
33  ss << t;
34 }
35 
36 template <typename T, typename... Args>
37 inline void MakeStringImpl(std::ostringstream& ss, const T& t, const Args&... args) noexcept {
38  MakeStringImpl(ss, t);
39  MakeStringImpl(ss, args...);
40 }
41 
42 // see MakeString comments for explanation of why this is necessary
43 template <typename... Args>
44 inline std::string MakeStringImpl(const Args&... args) noexcept {
45  std::ostringstream ss;
46  MakeStringImpl(ss, args...);
47  return ss.str();
48 }
49 
50 //
51 // Infrastructure to convert char[n] to char* to reduce binary size
52 //
53 
54 // default is to leave the type as is
55 template <class T>
57  using type = T;
58 };
59 
60 // specialization that matches an array reference, which is what the char array from a string literal
61 // used in a call to MakeString will be.
62 // if the type is a char[n] array we 'decay' it to a char* so that the usages can be folded.
63 template <class T, size_t N>
64 struct if_char_array_make_ptr<T (&)[N]> {
65  // remove a single extent (T[x] -> T, but T[x][y] -> T[y]) so we only match char[x],
66  // and get the type name without the 'const' so both 'const char (&)[n]' and 'char (&)[n]' are matched.
69 };
70 
71 // helper to make usage simpler in MakeString
72 template <class T>
74 } // namespace detail
75 
76 /**
77  * Makes a string by concatenating string representations of the arguments.
78  * This version uses the current locale.
79  */
80 template <typename... Args>
81 std::string MakeString(const Args&... args) {
82  // We need to update the types from the MakeString template instantiation to decay any char[n] to char*.
83  // e.g. MakeString("in", "out") goes from MakeString<char[2], char[3]> to MakeStringImpl<char*, char*>
84  // so that MakeString("out", "in") will also match MakeStringImpl<char*, char*> instead of requiring
85  // MakeStringImpl<char[3], char[2]>.
86  //
87  // We have to do the type processing before any actual work, so this function purely implements the type processing.
88  // If we do not do it this way we do not get the full binary size reduction.
89  //
90  // See https://stackoverflow.com/a/29418212/684911 for overall details of the approach, but note it does not cover
91  // the need to do the type processing as a separate step.
92 
94 }
95 
96 /**
97  * Makes a string by concatenating string representations of the arguments.
98  * This version uses std::locale::classic().
99  */
100 template <typename... Args>
102  std::ostringstream ss;
103  ss.imbue(std::locale::classic());
104  detail::MakeStringImpl(ss, args...);
105  return ss.str();
106 }
107 
108 // MakeString versions for already-a-string types.
109 
110 inline std::string MakeString(const std::string& str) {
111  return str;
112 }
113 
114 inline std::string MakeString(const char* cstr) {
115  return cstr;
116 }
117 
119  return str;
120 }
121 
122 inline std::string MakeStringWithClassicLocale(const char* cstr) {
123  return cstr;
124 }
125 
126 } // namespace onnxruntime
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
typename std::conditional< std::is_same< char, element_type >::value, T *, T(&)[N]>::type type
Definition: make_string.h:68
std::string MakeString(const Args &...args)
Definition: make_string.h:81
void MakeStringImpl(std::ostringstream &) noexcept
Definition: make_string.h:28
std::string MakeStringWithClassicLocale(const Args &...args)
Definition: make_string.h:101
GLdouble t
Definition: glad.h:2397
GA_API const UT_StringHolder N
**If you just want to fire and args
Definition: thread.h:609
typename std::remove_const< typename std::remove_extent< T >::type >::type element_type
Definition: make_string.h:67
type
Definition: core.h:1059
typename if_char_array_make_ptr< T >::type if_char_array_make_ptr_t
Definition: make_string.h:73