Path Tracer
TinyFormatter.h
Go to the documentation of this file.
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 
8 All rights reserved.
9 
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:
13 
14 * Redistributions of source code must retain the above
15  copyright notice, this list of conditions and the
16  following disclaimer.
17 
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.
22 
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.
27 
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.
39 
40 ----------------------------------------------------------------------
41 */
42 
48 #pragma once
49 #ifndef INCLUDED_TINY_FORMATTER_H
50 #define INCLUDED_TINY_FORMATTER_H
51 
52 #ifdef __GNUC__
53 # pragma GCC system_header
54 #endif
55 
56 #include <sstream>
57 
58 namespace Assimp {
59 namespace Formatter {
60 
61 // ------------------------------------------------------------------------------------------------
71 template < typename T,
72  typename CharTraits = std::char_traits<T>,
73  typename Allocator = std::allocator<T> >
75 public:
76  typedef class std::basic_string<T,CharTraits,Allocator> string;
77  typedef class std::basic_ostringstream<T,CharTraits,Allocator> stringstream;
78 
79  basic_formatter() {
80  // empty
81  }
82 
83  /* Allow basic_formatter<T>'s to be used almost interchangeably
84  * with std::(w)string or const (w)char* arguments because the
85  * conversion c'tor is called implicitly. */
86  template <typename TT>
87  basic_formatter(const TT& sin) {
88  underlying << sin;
89  }
90 
92  : underlying(std::move(other.underlying)) {
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  operator string () const {
107  return underlying.str();
108  }
109 
110  /* note - this is declared const because binding temporaries does only
111  * work for const references, so many function prototypes will
112  * include const basic_formatter<T>& s but might still want to
113  * modify the formatted string without the need for a full copy.*/
114  template <typename TToken>
115  const basic_formatter& operator << (const TToken& s) const {
116  underlying << s;
117  return *this;
118  }
119 
120  template <typename TToken>
121  basic_formatter& operator << (const TToken& s) {
122  underlying << s;
123  return *this;
124  }
125 
126 
127  // comma operator overloaded as well, choose your preferred way.
128  template <typename TToken>
129  const basic_formatter& operator, (const TToken& s) const {
130  underlying << s;
131  return *this;
132  }
133 
134  template <typename TToken>
135  basic_formatter& operator, (const TToken& s) {
136  underlying << s;
137  return *this;
138  }
139 
140  // Fix for MSVC8
141  // See https://sourceforge.net/projects/assimp/forums/forum/817654/topic/4372824
142  template <typename TToken>
143  basic_formatter& operator, (TToken& s) {
144  underlying << s;
145  return *this;
146  }
147 
148 
149 private:
150  mutable stringstream underlying;
151 };
152 
153 
156 
157 } // ! namespace Formatter
158 
159 } // ! namespace Assimp
160 
161 #endif
Assimp::Formatter::basic_formatter
Definition: TinyFormatter.h:74
Assimp
Definition: ai_assert.h:50