miniassimp

view include/miniassimp/TinyFormatter.h @ 0:879c81d94345

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Jan 2019 18:19:26 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2018, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 ----------------------------------------------------------------------
41 */
43 /** @file TinyFormatter.h
44 * @brief Utility to format log messages more easily. Introduced
45 * to get rid of the boost::format dependency. Much slinker,
46 * basically just extends stringstream.
47 */
48 #ifndef INCLUDED_TINY_FORMATTER_H
49 #define INCLUDED_TINY_FORMATTER_H
51 #include <sstream>
53 namespace Assimp {
54 namespace Formatter {
56 // ------------------------------------------------------------------------------------------------
57 /** stringstream utility. Usage:
58 * @code
59 * void writelog(const std::string&s);
60 * void writelog(const std::wstring&s);
61 * ...
62 * writelog(format()<< "hi! this is a number: " << 4);
63 * writelog(wformat()<< L"hi! this is a number: " << 4);
64 *
65 * @endcode */
66 template < typename T,
67 typename CharTraits = std::char_traits<T>,
68 typename Allocator = std::allocator<T>
69 >
70 class basic_formatter
71 {
73 public:
75 typedef class std::basic_string<
76 T,CharTraits,Allocator
77 > string;
79 typedef class std::basic_ostringstream<
80 T,CharTraits,Allocator
81 > stringstream;
83 public:
85 basic_formatter() {}
87 /* Allow basic_formatter<T>'s to be used almost interchangeably
88 * with std::(w)string or const (w)char* arguments because the
89 * conversion c'tor is called implicitly. */
90 template <typename TT>
91 basic_formatter(const TT& sin) {
92 underlying << sin;
93 }
96 // The problem described here:
97 // https://sourceforge.net/tracker/?func=detail&atid=1067632&aid=3358562&group_id=226462
98 // can also cause trouble here. Apparently, older gcc versions sometimes copy temporaries
99 // being bound to const ref& function parameters. Copying streams is not permitted, though.
100 // This workaround avoids this by manually specifying a copy ctor.
101 #if !defined(__GNUC__) || !defined(__APPLE__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
102 explicit basic_formatter(const basic_formatter& other) {
103 underlying << (string)other;
104 }
105 #endif
108 public:
110 operator string () const {
111 return underlying.str();
112 }
115 /* note - this is declared const because binding temporaries does only
116 * work for const references, so many function prototypes will
117 * include const basic_formatter<T>& s but might still want to
118 * modify the formatted string without the need for a full copy.*/
119 template <typename TToken>
120 const basic_formatter& operator << (const TToken& s) const {
121 underlying << s;
122 return *this;
123 }
125 template <typename TToken>
126 basic_formatter& operator << (const TToken& s) {
127 underlying << s;
128 return *this;
129 }
132 // comma operator overloaded as well, choose your preferred way.
133 template <typename TToken>
134 const basic_formatter& operator, (const TToken& s) const {
135 underlying << s;
136 return *this;
137 }
139 template <typename TToken>
140 basic_formatter& operator, (const TToken& s) {
141 underlying << s;
142 return *this;
143 }
145 // Fix for MSVC8
146 // See https://sourceforge.net/projects/assimp/forums/forum/817654/topic/4372824
147 template <typename TToken>
148 basic_formatter& operator, (TToken& s) {
149 underlying << s;
150 return *this;
151 }
154 private:
155 mutable stringstream underlying;
156 };
159 typedef basic_formatter< char > format;
160 typedef basic_formatter< wchar_t > wformat;
162 } // ! namespace Formatter
164 } // ! namespace Assimp
166 #endif