Debian Codegraph @

common/mptStringFormat.h

raw
1 /*
2 * mptStringFormat.h
3 * -----------------
4 * Purpose: Convert other types to strings.
5 * Notes : Currently none.
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15 #include "mpt/base/detect.hpp"
16 #include "mpt/endian/integer.hpp"
17 #include "mpt/format/default_formatter.hpp"
18 #include "mpt/format/message.hpp"
19 #include "mpt/format/message_macros.hpp"
20 #include "mpt/format/simple.hpp"
21 #include "mpt/format/simple_spec.hpp"
22 #include "mpt/string/types.hpp"
23 #include "mpt/string_transcode/transcode.hpp"
24
25 #include "openmpt/base/FlagSet.hpp"
26
27 #include "mptString.h"
28
29 #include <cstddef>
30
31
32
33 // The following section demands a rationale.
34 // 1. mpt::afmt::val(), mpt::wfmt::val() and mpt::ufmt::val() mimic the semantics of c++11 std::to_string() and std::to_wstring().
35 // There is an important difference though. The c++11 versions are specified in terms of sprintf formatting which in turn
36 // depends on the current C locale. This renders these functions unusable in a library context because the current
37 // C locale is set by the library-using application and could be anything. There is no way a library can get reliable semantics
38 // out of these functions. It is thus better to just avoid them.
39 // ToAString() and ToWString() are based on iostream internally, but the the locale of the stream is forced to std::locale::classic(),
40 // which results in "C" ASCII locale behavior.
41 // 2. The full suite of printf-like or iostream like number formatting is generally not required. Instead, a sane subset functionality
42 // is provided here.
43 // When formatting integers, it is recommended to use mpt::afmt::dec or mpt::afmt::hex. Appending a template argument '<n>' sets the width,
44 // the same way as '%nd' would do. Appending a '0' to the function name causes zero-filling as print-like '%0nd' would do. Spelling 'HEX'
45 // in upper-case generates upper-case hex digits. If these are not known at compile-time, a more verbose FormatValA(int, format) can be
46 // used.
47 // 3. mpt::format(format)(...) provides simplified and type-safe message and localization string formatting.
48 // The only specifier allowed is '{}' enclosing a number n. It references to n-th parameter after the format string (1-based).
49 // This mimics the behaviour of QString::arg() in QT4/5 or MFC AfxFormatString2(). C printf-like functions offer similar functionality
50 // with a '%n$TYPE' syntax. In .NET, the syntax is '{n}'. This is useful to support localization strings that can change the parameter
51 // ordering.
52 // 4. Every function is available for std::string, std::wstring and mpt::ustring. std::string makes no assumption about the encoding, which
53 // basically means, it should work for any 7-bit or 8-bit encoding, including for example ASCII, UTF8 or the current locale encoding.
54 // std::string std::wstring mpt::ustring mpt::tsrtring CString
55 // mpt::afmt mpt::wfmt mpt::ufmt mpt::tfmt mpt::cfmt
56 // MPT_AFORMAT("{}") MPT_WFORMAT("{}") MPT_UFORMAT("{}") MPT_TFORMAT("{}") MPT_CFORMAT("{}")
57 // 5. All functionality here delegates real work outside of the header file so that <sstream> and <locale> do not need to be included when
58 // using this functionality.
59 // Advantages:
60 // - Avoids binary code bloat when too much of iostream operator << gets inlined at every usage site.
61 // - Faster compile times because <sstream> and <locale> (2 very complex headers) are not included everywhere.
62 // Disadvantages:
63 // - Slightly more c++ code is required for delegating work.
64 // - As the header does not use iostreams, custom types need to overload mpt::UString instead of iostream operator << to allow for custom type
65 // formatting.
66 // - std::string, std::wstring and mpt::ustring are returned from somewhat deep cascades of helper functions. Where possible, code is
67 // written in such a way that return-value-optimization (RVO) or named-return-value-optimization (NRVO) should be able to eliminate
68 // almost all these copies. This should not be a problem for any decent modern compiler (and even less so for a c++11 compiler where
69 // move-semantics will kick in if RVO/NRVO fails).
70
71
72
73 namespace mpt {
74 inline namespace MPT_INLINE_NS {
75 template <typename Tstring, typename Tint, mpt::endian endian>
76 inline auto format_value_default(const mpt::packed<Tint, endian> & x) -> decltype(mpt::default_formatter::format<Tstring, Tint>(x)) {
77 return mpt::default_formatter::format<Tstring, Tint>(x);
78 }
79 } // namespace MPT_INLINE_NS
80 } // namespace mpt
81
82
83 OPENMPT_NAMESPACE_BEGIN
84
85
86 template <typename Tstring, typename T>
87 inline auto format_value_default(const T & x) -> decltype(mpt::transcode<Tstring>(x.ToUString())) {
88 return mpt::transcode<Tstring>(x.ToUString());
89 }
90
91
92 template <typename Tstring, typename T>
93 inline auto format_value_default(const T & x) -> decltype(mpt::transcode<Tstring>(ToUString(x))) {
94 return mpt::transcode<Tstring>(ToUString(x));
95 }
96
97
98 namespace mpt
99 {
100
101
102 template <typename Tstring, typename T>
103 inline auto format_value_default(const T & x) -> decltype(mpt::transcode<Tstring>(x.ToUString())) {
104 return mpt::transcode<Tstring>(x.ToUString());
105 }
106
107
108 template <typename Tstring, typename T>
109 inline auto format_value_default(const T & x) -> decltype(mpt::transcode<Tstring>(ToUString(x))) {
110 return mpt::transcode<Tstring>(ToUString(x));
111 }
112
113
114 template <typename Tstring>
115 using fmtT = mpt::format<Tstring>;
116
117 using afmt = fmtT<std::string>;
118 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
119 using wfmt = fmtT<std::wstring>;
120 #endif
121 using ufmt = fmtT<mpt::ustring>;
122 #if defined(MPT_ENABLE_CHARSET_LOCALE)
123 using lfmt = fmtT<mpt::lstring>;
124 #endif // MPT_ENABLE_CHARSET_LOCALE
125 #if MPT_OS_WINDOWS
126 using tfmt = fmtT<mpt::tstring>;
127 #endif
128 #if defined(MPT_WITH_MFC)
129 using cfmt = fmtT<CString>;
130 #endif // MPT_WITH_MFC
131
132
133 #define MPT_AFORMAT(f) MPT_AFORMAT_MESSAGE(f)
134
135 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
136 #define MPT_WFORMAT(f) MPT_WFORMAT_MESSAGE(f)
137 #endif
138
139 #define MPT_UFORMAT(f) MPT_UFORMAT_MESSAGE(f)
140
141 #if defined(MPT_ENABLE_CHARSET_LOCALE)
142 #define MPT_LFORMAT(f) MPT_LFORMAT_MESSAGE(f)
143 #endif // MPT_ENABLE_CHARSET_LOCALE
144
145 #if MPT_OS_WINDOWS
146 #define MPT_TFORMAT(f) MPT_TFORMAT_MESSAGE(f)
147 #endif // MPT_OS_WINDOWS
148
149 #if defined(MPT_WITH_MFC)
150 #define MPT_CFORMAT(f) MPT_CFORMAT_MESSAGE(f)
151 #endif // MPT_WITH_MFC
152
153
154 } // namespace mpt
155
156
157
158 template <typename enum_t, typename store_t>
159 mpt::ustring ToUString(FlagSet<enum_t, store_t> flagset)
160 {
161 mpt::ustring str(flagset.size_bits(), UC_('0'));
162
163 for(std::size_t x = 0; x < flagset.size_bits(); ++x)
164 {
165 str[flagset.size_bits() - x - 1] = (flagset.value().as_bits() & (static_cast<typename FlagSet<enum_t>::store_type>(1) << x) ? UC_('1') : UC_('0'));
166 }
167
168 return str;
169 }
170
171
172
173 OPENMPT_NAMESPACE_END