vrshoot

view libs/assimp/TinyFormatter.h @ 1:e7ca128b8713

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