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