miniassimp

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