TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_string.hpp
1 
9 #ifndef TurtleBrains_String_hpp
10 #define TurtleBrains_String_hpp
11 
12 #include <turtle_brains/core/tb_configuration.hpp>
13 #include <turtle_brains/core/tb_types.hpp>
14 #include <turtle_brains/core/tb_error.hpp>
15 
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 
24 
30 #define tb_string(object) TurtleBrains::Core::ToString((object))
31 
35 
36 namespace TurtleBrains::Core
37 {
38 
50  template<typename Type> std::string ToStdString(const Type& object)
51  {
52  std::stringstream ss;
53  ss << object;
54  return ss.str();
55  }
56 
65  inline std::string ToStdString(const std::string& object)
66  {
67  return object;
68  }
69 
78  inline std::string ToStdString(const std::wstring& object)
79  {
80  std::stringstream stream;
81  const std::ctype<char>& ctfacet = std::use_facet< std::ctype<char> >(stream.getloc());
82  for (size_t i = 0; i < object.size(); ++i)
83  {
84  stream << static_cast<char>(ctfacet.narrow(static_cast<char>(object[i]), '?'));
85  }
86  return stream.str();
87  }
88 
96  inline std::string ToStdString(const wchar_t* object)
97  {
98  return ToStdString(std::wstring(object));
99  }
100 
110  inline std::string ToStdString(float value, int precision = -1)
111  {
112  std::stringstream ss;
113  if (precision >= 0)
114  {
115  ss.precision(precision);
116  ss << std::fixed;
117  }
118  ss << value;
119  return ss.str();
120  }
121 
122  inline std::string ToStdString(double value, int precision = -1)
123  {
124  std::stringstream ss;
125  if (precision >= 0)
126  {
127  ss.precision(precision);
128  ss << std::fixed;
129  }
130  ss << value;
131  return ss.str();
132  }
133 
146  template<typename Type> Type FromStdString(const std::string& input)
147  {
148  // 2026-01-02: This was added 2025-11-15 for an unknown reason. I don't have the context on that anymore but I
149  // would assume it was because something crashed deeper when the input was empty. However, the overloaded
150  // operator<<() function should be handling error conditions when building the Type, such as a UUID returning
151  // an invalid uuid key; expected by TrackBuilder, so the error check is now removed again.
152  //
153  //tb_error_if(true == input.empty(), "Invalid value for parameter: input, expected valid, non-empty, string.");
154  Type object;
155  std::stringstream ss(input);
156  ss >> object;
157  return object;
158  }
159 
160 //--------------------------------------------------------------------------------------------------------------------//
161 //--------------------------------------------------------------------------------------------------------------------//
162 //--------------------------------------------------------------------------------------------------------------------//
163 
175  template<typename Type> std::wstring ToWideString(const Type& object)
176  {
177  std::wstringstream wss;
178  wss << object;
179  return wss.str();
180  }
181 
190  inline std::wstring ToWideString(const std::wstring& object)
191  {
192  return object;
193  }
194 
202  inline std::wstring ToWideString(const std::string& object)
203  {
204  std::wostringstream stream;
205  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(stream.getloc());
206  for (size_t i = 0; i < object.size(); ++i)
207  {
208  stream << ctfacet.widen(object[i]);
209  }
210  return stream.str();
211  }
212 
220  inline std::wstring ToWideString(const char* object)
221  {
222  return ToWideString(std::string(object));
223  }
224 
234  inline std::wstring ToWideString(float value, int precision = -1)
235  {
236  std::wstringstream wss;
237  if (precision >= 0)
238  {
239  wss.precision(precision);
240  wss << std::fixed;
241  }
242  wss << value;
243  return wss.str();
244  }
245 
246  inline std::wstring ToWideString(double value, int precision = -1)
247  {
248  std::wstringstream wss;
249  if (precision >= 0)
250  {
251  wss.precision(precision);
252  wss << std::fixed;
253  }
254  wss << value;
255  return wss.str();
256  }
257 
270  template<typename Type> Type FromWideString(const std::wstring& input)
271  {
272  // 2026-01-02: This was added 2025-11-15 for an unknown reason. I don't have the context on that anymore but I
273  // would assume it was because something crashed deeper when the input was empty. However, the overloaded
274  // operator<<() function should be handling error conditions when building the Type, such as a UUID returning
275  // an invalid uuid key; expected by TrackBuilder, so the error check is now removed again.
276  //
277  //tb_error_if(true == input.empty(), "Invalid value for parameter: input, expected valid, non-empty, string.");
278  Type object;
279  std::wstringstream wss(input);
280  wss >> object;
281  return object;
282  }
283 
284 //--------------------------------------------------------------------------------------------------------------------//
285 //--------------------------------------------------------------------------------------------------------------------//
286 //--------------------------------------------------------------------------------------------------------------------//
287 
299 #ifdef tb_with_wide_string
300  typedef std::wstring String;
301 #else
302  typedef std::string String;
303 #endif /* tb_with_wide_string */
304 
317  template<typename Type> String ToString(const Type& object)
318  {
319 #ifdef tb_with_wide_string
320  return TurtleBrains::Core::ToWideString(object);
321 #else
322  return TurtleBrains::Core::ToStdString(object);
323 #endif /* tb_with_wide_string */
324  }
325 
331  inline String ToString(float value, int precision = -1)
332  {
333 #ifdef tb_with_wide_string
334  return TurtleBrains::Core::ToWideString(value, precision);
335 #else
336  return TurtleBrains::Core::ToStdString(value, precision);
337 #endif /* tb_with_wide_string */
338  }
339 
345  inline String ToString(double value, int precision = -1)
346  {
347 #ifdef tb_with_wide_string
348  return TurtleBrains::Core::ToWideString(value, precision);
349 #else
350  return TurtleBrains::Core::ToStdString(value, precision);
351 #endif /* tb_with_wide_string */
352  }
353 
366  template<typename Type> Type FromString(const std::string& input)
367  {
368  return TurtleBrains::Core::FromStdString<Type>(input);
369  }
370 
383  template<typename Type> Type FromString(const std::wstring& input)
384  {
385  return TurtleBrains::Core::FromWideString<Type>(input);
386  }
387 
388 }; /* namespace TurtleBrains::Core */
389 
390 namespace tbCore = TurtleBrains::Core;
391 
392 namespace TurtleBrains
393 {
394  using String = tbCore::String;
395 };
396 
397 // 2025-11-11: Yea yea yea... We are trying to rip this out, but I need Rushcremental to remain the focus on 100 day game
398 // days and not play with TurtleBrains for 5 hours removing the rest of the tbString from macOS and Linux etc. So remove
399 // this once we get to it in the api cleanup.
400 namespace TurtleBrains::Core
401 {
402  using tbString = String;
403 };
404 
405 #endif /* TurtleBrains_String_hpp */
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
String ToString(const Type &object)
Definition: tb_string.hpp:317
std::wstring ToWideString(const Type &object)
Definition: tb_string.hpp:175
Type FromWideString(const std::wstring &input)
Definition: tb_string.hpp:270
std::string String
Definition: tb_string.hpp:302
Type FromStdString(const std::string &input)
Definition: tb_string.hpp:146
std::string ToStdString(const Type &object)
Definition: tb_string.hpp:50
Type FromString(const std::string &input)
Definition: tb_string.hpp:366
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22