StringStream.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <stdio.h>
  25. #include "List.h"
  26. #include "StringUtils.h"
  27. #include "OS.h"
  28. namespace crown
  29. {
  30. class StringStream
  31. {
  32. public:
  33. StringStream(Allocator& allocator);
  34. void clear();
  35. StringStream& operator<<(int16_t val);
  36. StringStream& operator<<(uint16_t val);
  37. StringStream& operator<<(int32_t val);
  38. StringStream& operator<<(uint32_t val);
  39. StringStream& operator<<(int64_t val);
  40. StringStream& operator<<(uint64_t val);
  41. StringStream& operator<<(float val);
  42. StringStream& operator<<(double val);
  43. StringStream& operator<<(const char* s);
  44. const char* c_str();
  45. private:
  46. template <typename T>
  47. StringStream& stream_printf(const char* format, T& val);
  48. private:
  49. List<char> m_string;
  50. };
  51. //-----------------------------------------------------------------------------
  52. inline StringStream::StringStream(Allocator& allocator)
  53. : m_string(allocator)
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. inline void StringStream::clear()
  58. {
  59. m_string.clear();
  60. }
  61. //-----------------------------------------------------------------------------
  62. inline StringStream& StringStream::operator<<(int16_t val)
  63. {
  64. return stream_printf("%hd", val);
  65. }
  66. //-----------------------------------------------------------------------------
  67. inline StringStream& StringStream::operator<<(uint16_t val)
  68. {
  69. return stream_printf("%hu", val);
  70. }
  71. //-----------------------------------------------------------------------------
  72. inline StringStream& StringStream::operator<<(int32_t val)
  73. {
  74. return stream_printf("%d", val);
  75. }
  76. //-----------------------------------------------------------------------------
  77. inline StringStream& StringStream::operator<<(uint32_t val)
  78. {
  79. return stream_printf("%u", val);
  80. }
  81. //-----------------------------------------------------------------------------
  82. inline StringStream& StringStream::operator<<(int64_t val)
  83. {
  84. return stream_printf("%lld", val);
  85. }
  86. //-----------------------------------------------------------------------------
  87. inline StringStream& StringStream::operator<<(uint64_t val)
  88. {
  89. return stream_printf("%llu", val);
  90. }
  91. //-----------------------------------------------------------------------------
  92. inline StringStream& StringStream::operator<<(float val)
  93. {
  94. return stream_printf("%g", val);
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline StringStream& StringStream::operator<<(double val)
  98. {
  99. return stream_printf("%g", val);
  100. }
  101. //-----------------------------------------------------------------------------
  102. inline StringStream& StringStream::operator<<(const char* s)
  103. {
  104. m_string.push(s, string::strlen(s));
  105. return *this;
  106. }
  107. //-----------------------------------------------------------------------------
  108. inline const char* StringStream::c_str()
  109. {
  110. m_string.push_back('\0');
  111. m_string.pop_back();
  112. return m_string.begin();
  113. }
  114. //-----------------------------------------------------------------------------
  115. template <typename T>
  116. inline StringStream& StringStream::stream_printf(const char* format, T& val)
  117. {
  118. char buf[32];
  119. snprintf(buf, 32, format, val);
  120. return *this << buf;
  121. }
  122. } // namespace crown