Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:30:10

0001 /*
0002 Open Asset Import Library (assimp)
0003 ----------------------------------------------------------------------
0004 
0005 Copyright (c) 2006-2024, assimp team
0006 
0007 
0008 All rights reserved.
0009 
0010 Redistribution and use of this software in source and binary forms,
0011 with or without modification, are permitted provided that the
0012 following conditions are met:
0013 
0014 * Redistributions of source code must retain the above
0015   copyright notice, this list of conditions and the
0016   following disclaimer.
0017 
0018 * Redistributions in binary form must reproduce the above
0019   copyright notice, this list of conditions and the
0020   following disclaimer in the documentation and/or other
0021   materials provided with the distribution.
0022 
0023 * Neither the name of the assimp team, nor the names of its
0024   contributors may be used to endorse or promote products
0025   derived from this software without specific prior
0026   written permission of the assimp team.
0027 
0028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0029 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0030 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0031 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0032 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0033 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0034 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0035 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0036 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0038 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0039 
0040 ----------------------------------------------------------------------
0041 */
0042 
0043 /** @file  TinyFormatter.h
0044  *  @brief Utility to format log messages more easily. Introduced
0045  *    to get rid of the boost::format dependency. Much slinker,
0046  *    basically just extends stringstream.
0047  */
0048 #pragma once
0049 #ifndef INCLUDED_TINY_FORMATTER_H
0050 #define INCLUDED_TINY_FORMATTER_H
0051 
0052 #ifdef __GNUC__
0053 #   pragma GCC system_header
0054 #endif
0055 
0056 #include <sstream>
0057 
0058 namespace Assimp {
0059 namespace Formatter {
0060 
0061 // ------------------------------------------------------------------------------------------------
0062 /** stringstream utility. Usage:
0063  *  @code
0064  *  void writelog(const std::string&s);
0065  *  void writelog(const std::wstring&s);
0066  *  ...
0067  *  writelog(format()<< "hi! this is a number: " << 4);
0068  *  writelog(wformat()<< L"hi! this is a number: " << 4);
0069  *
0070  *  @endcode */
0071 template < typename T,
0072     typename CharTraits = std::char_traits<T>,
0073     typename Allocator  = std::allocator<T> >
0074 class basic_formatter {
0075 public:
0076     typedef class std::basic_string<T,CharTraits,Allocator> string;
0077     typedef class std::basic_ostringstream<T,CharTraits,Allocator> stringstream;
0078 
0079     basic_formatter() {
0080         // empty
0081     }
0082 
0083     /* Allow basic_formatter<T>'s to be used almost interchangeably
0084      * with std::(w)string or const (w)char* arguments because the
0085      * conversion c'tor is called implicitly. */
0086     template <typename TT>
0087     basic_formatter(const TT& sin)  {
0088         underlying << sin;
0089     }
0090 
0091     // Same problem as the copy constructor below, but with root cause is that stream move
0092     // is not permitted on older GCC versions. Small performance impact on those platforms.
0093 #if defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ <= 9)
0094     basic_formatter(basic_formatter&& other) {
0095         underlying << (string)other;
0096     }
0097 #else
0098     basic_formatter(basic_formatter&& other)
0099         : underlying(std::move(other.underlying)) {
0100     }
0101 #endif
0102 
0103     // The problem described here:
0104     // https://sourceforge.net/tracker/?func=detail&atid=1067632&aid=3358562&group_id=226462
0105     // can also cause trouble here. Apparently, older gcc versions sometimes copy temporaries
0106     // being bound to const ref& function parameters. Copying streams is not permitted, though.
0107     // This workaround avoids this by manually specifying a copy ctor.
0108 #if !defined(__GNUC__) || !defined(__APPLE__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
0109     explicit basic_formatter(const basic_formatter& other) {
0110         underlying << (string)other;
0111     }
0112 #endif
0113 
0114     operator string () const {
0115         return underlying.str();
0116     }
0117 
0118     /* note - this is declared const because binding temporaries does only
0119      * work for const references, so many function prototypes will
0120      * include const basic_formatter<T>& s but might still want to
0121      * modify the formatted string without the need for a full copy.*/
0122     template <typename TToken, typename std::enable_if<!std::is_base_of<std::exception, TToken>::value>::type * = nullptr>
0123     const basic_formatter &operator<<(const TToken &s) const {
0124         underlying << s;
0125         return *this;
0126     }
0127 
0128     template <typename TToken, typename std::enable_if<std::is_base_of<std::exception, TToken>::value>::type * = nullptr>
0129     const basic_formatter &operator<<(const TToken &s) const {
0130         underlying << s.what();
0131         return *this;
0132     }
0133 
0134     template <typename TToken, typename std::enable_if<!std::is_base_of<std::exception, TToken>::value>::type * = nullptr>
0135     basic_formatter &operator<<(const TToken &s) {
0136         underlying << s;
0137         return *this;
0138     }
0139 
0140     template <typename TToken, typename std::enable_if<std::is_base_of<std::exception, TToken>::value>::type * = nullptr>
0141     basic_formatter &operator<<(const TToken &s) {
0142         underlying << s.what();
0143         return *this;
0144     }
0145 
0146 
0147     // comma operator overloaded as well, choose your preferred way.
0148     template <typename TToken>
0149     const basic_formatter& operator, (const TToken& s) const {
0150         *this << s;
0151         return *this;
0152     }
0153 
0154     template <typename TToken>
0155     basic_formatter& operator, (const TToken& s) {
0156         *this << s;
0157         return *this;
0158     }
0159 
0160     // Fix for MSVC8
0161     // See https://sourceforge.net/projects/assimp/forums/forum/817654/topic/4372824
0162     template <typename TToken>
0163     basic_formatter& operator, (TToken& s) {
0164         *this << s;
0165         return *this;
0166     }
0167 
0168 
0169 private:
0170     mutable stringstream underlying;
0171 };
0172 
0173 
0174 typedef basic_formatter< char > format;
0175 typedef basic_formatter< wchar_t > wformat;
0176 
0177 } // ! namespace Formatter
0178 
0179 } // ! namespace Assimp
0180 
0181 #endif