TinyFormatter.h 6.1 KB

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