vrshoot

diff libs/assimp/TinyFormatter.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/TinyFormatter.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,163 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/** @file  TinyFormatter.h
    1.45 + *  @brief Utility to format log messages more easily. Introduced
    1.46 + *    to get rid of the boost::format dependency. Much slinker,
    1.47 + *    basically just extends stringstream.
    1.48 + */
    1.49 +#ifndef INCLUDED_TINY_FORMATTER_H
    1.50 +#define INCLUDED_TINY_FORMATTER_H
    1.51 +
    1.52 +#include <sstream>
    1.53 +
    1.54 +namespace Assimp {
    1.55 +	namespace Formatter {
    1.56 +
    1.57 +// ------------------------------------------------------------------------------------------------
    1.58 +/** stringstream utility. Usage:
    1.59 + *  @code
    1.60 + *  void writelog(const std::string&s);
    1.61 + *  void writelog(const std::wstring&s);
    1.62 + *  ...
    1.63 + *  writelog(format()<< "hi! this is a number: " << 4);
    1.64 + *  writelog(wformat()<< L"hi! this is a number: " << 4);
    1.65 + *
    1.66 + *  @endcode */
    1.67 +template < typename T,
    1.68 +	typename CharTraits = std::char_traits<T>,
    1.69 +	typename Allocator  = std::allocator<T>
    1.70 +> 
    1.71 +class basic_formatter 
    1.72 +{
    1.73 +
    1.74 +public:
    1.75 +
    1.76 +	typedef class std::basic_string< 
    1.77 +		T,CharTraits,Allocator
    1.78 +	> string;
    1.79 +
    1.80 +	typedef class std::basic_ostringstream< 
    1.81 +		T,CharTraits,Allocator
    1.82 +	> stringstream;
    1.83 +
    1.84 +public:
    1.85 +
    1.86 +	basic_formatter() {}
    1.87 +
    1.88 +	/* Allow basic_formatter<T>'s to be used almost interchangeably
    1.89 +	 * with std::(w)string or const (w)char* arguments because the
    1.90 +	 * conversion c'tor is called implicitly. */
    1.91 +	template <typename TT>
    1.92 +	basic_formatter(const TT& sin)	{
    1.93 +		underlying << sin;
    1.94 +	}
    1.95 +
    1.96 +
    1.97 +	// The problem described here:
    1.98 +	// https://sourceforge.net/tracker/?func=detail&atid=1067632&aid=3358562&group_id=226462
    1.99 +	// can also cause trouble here. Apparently, older gcc versions sometimes copy temporaries
   1.100 +	// being bound to const ref& function parameters. Copying streams is not permitted, though.
   1.101 +	// This workaround avoids this by manually specifying a copy ctor.
   1.102 +#if !defined(__GNUC__) || !defined(__APPLE__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
   1.103 +	basic_formatter(const basic_formatter& other) {
   1.104 +		underlying << (string)other;
   1.105 +	}
   1.106 +#endif
   1.107 +	 
   1.108 +
   1.109 +public:
   1.110 +
   1.111 +	operator string () const {
   1.112 +		return underlying.str();
   1.113 +	}
   1.114 +
   1.115 +
   1.116 +	/* note - this is declared const because binding temporaries does only
   1.117 +	 * work for const references, so many function prototypes will 
   1.118 +	 * include const basic_formatter<T>& s but might still want to
   1.119 +	 * modify the formatted string without the need for a full copy.*/
   1.120 +	template <typename TToken>
   1.121 +	const basic_formatter& operator << (const TToken& s) const {
   1.122 +		underlying << s;
   1.123 +		return *this;
   1.124 +	}
   1.125 +
   1.126 +	template <typename TToken>
   1.127 +	basic_formatter& operator << (const TToken& s) {
   1.128 +		underlying << s;
   1.129 +		return *this;
   1.130 +	}
   1.131 +
   1.132 +
   1.133 +	// comma operator overloaded as well, choose your preferred way.
   1.134 +	template <typename TToken>
   1.135 +	const basic_formatter& operator, (const TToken& s) const {
   1.136 +		underlying << s;
   1.137 +		return *this;
   1.138 +	}
   1.139 +
   1.140 +	template <typename TToken>
   1.141 +	basic_formatter& operator, (const TToken& s) {
   1.142 +		underlying << s;
   1.143 +		return *this;
   1.144 +	}
   1.145 +
   1.146 +	// Fix for MSVC8
   1.147 +	// See https://sourceforge.net/projects/assimp/forums/forum/817654/topic/4372824
   1.148 +	template <typename TToken>
   1.149 +	basic_formatter& operator, (TToken& s) {
   1.150 +		underlying << s;
   1.151 +		return *this;
   1.152 +	}
   1.153 +
   1.154 +
   1.155 +private:
   1.156 +	mutable stringstream underlying;
   1.157 +};
   1.158 +
   1.159 +
   1.160 +typedef basic_formatter< char > format;
   1.161 +typedef basic_formatter< wchar_t > wformat;
   1.162 +
   1.163 +} // ! namespace Formatter 
   1.164 +
   1.165 +} // ! namespace Assimp
   1.166 +#endif