You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TinyFormatter.h 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #ifndef INCLUDED_TINY_FORMATTER_H
  39. #define INCLUDED_TINY_FORMATTER_H
  40. #include <sstream>
  41. namespace Assimp {
  42. namespace Formatter {
  43. // ------------------------------------------------------------------------------------------------
  44. /** stringstream utility. Usage:
  45. * @code
  46. * void writelog(const std::string&s);
  47. * void writelog(const std::wstring&s);
  48. * ...
  49. * writelog(format()<< "hi! this is a number: " << 4);
  50. * writelog(wformat()<< L"hi! this is a number: " << 4);
  51. *
  52. * @endcode */
  53. template < typename T,
  54. typename CharTraits = std::char_traits<T>,
  55. typename Allocator = std::allocator<T>
  56. >
  57. class basic_formatter
  58. {
  59. public:
  60. typedef class std::basic_string<
  61. T,CharTraits,Allocator
  62. > string;
  63. typedef class std::basic_ostringstream<
  64. T,CharTraits,Allocator
  65. > stringstream;
  66. public:
  67. basic_formatter() {}
  68. /* Allow basic_formatter<T>'s to be used almost interchangeably
  69. * with std::(w)string or const (w)char* arguments because the
  70. * conversion c'tor is called implicitly. */
  71. template <typename TT>
  72. basic_formatter(const TT& sin) {
  73. underlying << sin;
  74. }
  75. // The problem described here:
  76. // https://sourceforge.net/tracker/?func=detail&atid=1067632&aid=3358562&group_id=226462
  77. // can also cause trouble here. Apparently, older gcc versions sometimes copy temporaries
  78. // being bound to const ref& function parameters. Copying streams is not permitted, though.
  79. // This workaround avoids this by manually specifying a copy ctor.
  80. #if !defined(__GNUC__) || !defined(__APPLE__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  81. basic_formatter(const basic_formatter& other) {
  82. underlying << (string)other;
  83. }
  84. #endif
  85. public:
  86. operator string () const {
  87. return underlying.str();
  88. }
  89. /* note - this is declared const because binding temporaries does only
  90. * work for const references, so many function prototypes will
  91. * include const basic_formatter<T>& s but might still want to
  92. * modify the formatted string without the need for a full copy.*/
  93. template <typename TToken>
  94. const basic_formatter& operator << (const TToken& s) const {
  95. underlying << s;
  96. return *this;
  97. }
  98. template <typename TToken>
  99. basic_formatter& operator << (const TToken& s) {
  100. underlying << s;
  101. return *this;
  102. }
  103. // comma operator overloaded as well, choose your preferred way.
  104. template <typename TToken>
  105. const basic_formatter& operator, (const TToken& s) const {
  106. underlying << s;
  107. return *this;
  108. }
  109. template <typename TToken>
  110. basic_formatter& operator, (const TToken& s) {
  111. underlying << s;
  112. return *this;
  113. }
  114. // Fix for MSVC8
  115. // See https://sourceforge.net/projects/assimp/forums/forum/817654/topic/4372824
  116. template <typename TToken>
  117. basic_formatter& operator, (TToken& s) {
  118. underlying << s;
  119. return *this;
  120. }
  121. private:
  122. mutable stringstream underlying;
  123. };
  124. typedef basic_formatter< char > format;
  125. typedef basic_formatter< wchar_t > wformat;
  126. } // ! namespace Formatter
  127. } // ! namespace Assimp
  128. #endif