added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / to_string.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_UTIL_TO_STRING_HPP
20 #define REALM_UTIL_TO_STRING_HPP
21
22 #include <iosfwd>
23 #include <string>
24
25 namespace realm {
26 namespace util {
27
28 class Printable {
29 public:
30     Printable(bool value)
31         : m_type(Type::Bool)
32         , m_uint(value)
33     {
34     }
35     Printable(unsigned char value)
36         : m_type(Type::Uint)
37         , m_uint(value)
38     {
39     }
40     Printable(unsigned int value)
41         : m_type(Type::Uint)
42         , m_uint(value)
43     {
44     }
45     Printable(unsigned long value)
46         : m_type(Type::Uint)
47         , m_uint(value)
48     {
49     }
50     Printable(unsigned long long value)
51         : m_type(Type::Uint)
52         , m_uint(value)
53     {
54     }
55     Printable(char value)
56         : m_type(Type::Int)
57         , m_int(value)
58     {
59     }
60     Printable(int value)
61         : m_type(Type::Int)
62         , m_int(value)
63     {
64     }
65     Printable(long value)
66         : m_type(Type::Int)
67         , m_int(value)
68     {
69     }
70     Printable(long long value)
71         : m_type(Type::Int)
72         , m_int(value)
73     {
74     }
75     Printable(const char* value)
76         : m_type(Type::String)
77         , m_string(value)
78     {
79     }
80     Printable(std::string const& value)
81         : m_type(Type::String)
82         , m_string(value.c_str())
83     {
84     }
85
86
87     void print(std::ostream& out, bool quote) const;
88     std::string str() const;
89
90     static void print_all(std::ostream& out, const std::initializer_list<Printable>& values, bool quote);
91
92 private:
93     enum class Type {
94         Bool,
95         Int,
96         Uint,
97         String,
98     } m_type;
99
100     union {
101         uintmax_t m_uint;
102         intmax_t m_int;
103         const char* m_string;
104     };
105 };
106
107
108 template <class T>
109 std::string to_string(const T& v)
110 {
111     return Printable(v).str();
112 }
113
114 std::string format(const char* fmt, std::initializer_list<Printable>);
115
116 template<typename... Args>
117 std::string format(const char* fmt, Args&&... args)
118 {
119     return format(fmt, {Printable(args)...});
120 }
121
122
123 } // namespace util
124 } // namespace realm
125
126 #endif // REALM_UTIL_TO_STRING_HPP