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 <string>
23 #include <type_traits>
24 
25 namespace onnxruntime {
26 
27 namespace detail {
28 
29 inline void MakeStringImpl(std::ostringstream& /*ss*/) noexcept {
30 }
31 
32 template <typename T>
33 inline void MakeStringImpl(std::ostringstream& ss, const T& t) noexcept {
34  ss << t;
35 }
36 
37 template <typename T, typename... Args>
38 inline void MakeStringImpl(std::ostringstream& ss, const T& t, const Args&... args) noexcept {
39  MakeStringImpl(ss, t);
40  MakeStringImpl(ss, args...);
41 }
42 
43 // see MakeString comments for explanation of why this is necessary
44 template <typename... Args>
45 inline std::string MakeStringImpl(const Args&... args) noexcept {
46  std::ostringstream ss;
47  MakeStringImpl(ss, args...);
48  return ss.str();
49 }
50 
51 template <typename... Args>
52 inline std::string MakeStringWithClassicLocaleImpl(const Args&... args) noexcept {
53  std::ostringstream ss;
54  ss.imbue(std::locale::classic());
55  MakeStringImpl(ss, args...);
56  return ss.str();
57 }
58 
59 //
60 // Infrastructure to convert char[n] to char* to reduce binary size
61 //
62 
63 // default is to leave the type as is
64 template <class T>
66  using type = T;
67 };
68 
69 // specialization that matches an array reference, which is what the char array from a string literal
70 // used in a call to MakeString will be.
71 // if the type is a char[n] array we 'decay' it to a char* so that the usages can be folded.
72 template <class T, size_t N>
73 struct if_char_array_make_ptr<T (&)[N]> {
74  // remove a single extent (T[x] -> T, but T[x][y] -> T[y]) so we only match char[x],
75  // and get the type name without the 'const' so both 'const char (&)[n]' and 'char (&)[n]' are matched.
78 };
79 
80 // helper to make usage simpler in MakeString
81 template <class T>
83 } // namespace detail
84 
85 /**
86  * Makes a string by concatenating string representations of the arguments.
87  * This version uses the current locale.
88  */
89 template <typename... Args>
90 inline std::string MakeString(const Args&... args) {
91  // We need to update the types from the MakeString template instantiation to decay any char[n] to char*.
92  // e.g. MakeString("in", "out") goes from MakeString<char[2], char[3]> to MakeStringImpl<char*, char*>
93  // so that MakeString("out", "in") will also match MakeStringImpl<char*, char*> instead of requiring
94  // MakeStringImpl<char[3], char[2]>.
95  //
96  // We have to do the type processing before any actual work, so this function purely implements the type processing.
97  // If we do not do it this way we do not get the full binary size reduction.
98  //
99  // See https://stackoverflow.com/a/29418212/684911 for overall details of the approach, but note it does not cover
100  // the need to do the type processing as a separate step.
101 
103 }
104 
105 /**
106  * Makes a string by concatenating string representations of the arguments.
107  * This version uses std::locale::classic().
108  */
109 template <typename... Args>
110 inline std::string MakeStringWithClassicLocale(const Args&... args) {
112 }
113 
114 // MakeString versions for already-a-string types.
115 
116 inline std::string MakeString(const std::string& str) {
117  return str;
118 }
119 
120 inline std::string MakeString(const char* cstr) {
121  return cstr;
122 }
123 
124 inline std::string MakeStringWithClassicLocale(const std::string& str) {
125  return str;
126 }
127 
128 inline std::string MakeStringWithClassicLocale(const char* cstr) {
129  return cstr;
130 }
131 
132 } // namespace onnxruntime
type
Definition: core.h:556
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:77
std::string MakeString(const Args &...args)
Definition: make_string.h:90
void MakeStringImpl(std::ostringstream &) noexcept
Definition: make_string.h:29
std::string MakeStringWithClassicLocale(const Args &...args)
Definition: make_string.h:110
GLdouble t
Definition: glad.h:2397
std::string MakeStringWithClassicLocaleImpl(const Args &...args) noexcept
Definition: make_string.h:52
GA_API const UT_StringHolder N
**If you just want to fire and args
Definition: thread.h:618
typename std::remove_const< typename std::remove_extent< T >::type >::type element_type
Definition: make_string.h:76
typename if_char_array_make_ptr< T >::type if_char_array_make_ptr_t
Definition: make_string.h:82