TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tb_string.h
1 
9 #ifndef _TurtleBrains_String_h_
10 #define _TurtleBrains_String_h_
11 
12 #include "tb_configuration.h"
13 #include "tb_error.h"
14 
15 #include <sstream>
16 #include <string>
17 
22 
28 #define tb_string(object) TurtleBrains::Core::ToString((object))
29 
33 
34 namespace TurtleBrains
35 {
36  namespace Core
37  {
38 
50  template <typename T> std::string ToStdString(const T& 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 
134  template <typename T> T FromStdString(const std::string& input)
135  {
136  tb_error_if(true == input.empty(), "Invalid value for parameter: input, expected valid, non-empty, string.");
137  T object;
138  std::stringstream ss(input);
139  ss >> object;
140  return object;
141  }
142 
154  template <typename T> T FromString(const std::string& input)
155  {
156  return FromStdString<T>(input);
157  }
158 
159 //--------------------------------------------------------------------------------------------------------------------//
160 //--------------------------------------------------------------------------------------------------------------------//
161 //--------------------------------------------------------------------------------------------------------------------//
162 
174  template <typename T> std::wstring ToWideString(const T& object)
175  {
176  std::wstringstream wss;
177  wss << object;
178  return wss.str();
179  }
180 
189  inline std::wstring ToWideString(const std::wstring& object)
190  {
191  return object;
192  }
193 
201  inline std::wstring ToWideString(const std::string& object)
202  {
203  std::wostringstream stream;
204  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(stream.getloc());
205  for (size_t i = 0; i < object.size(); ++i)
206  {
207  stream << ctfacet.widen(object[i]);
208  }
209  return stream.str();
210  }
211 
219  inline std::wstring ToWideString(const char* object)
220  {
221  return ToWideString(std::string(object));
222  }
223 
233  inline std::wstring ToWideString(float value, int precision = -1)
234  {
235  std::wstringstream wss;
236  if (precision >= 0)
237  {
238  wss.precision(precision);
239  wss << std::fixed;
240  }
241  wss << value;
242  return wss.str();
243  }
244 
257  template <typename T> T FromWideString(const std::wstring& input)
258  {
259  tb_error_if(true == input.empty(), "Invalid value for parameter: input, expected valid, non-empty, string.");
260  T object;
261  std::wstringstream wss(input);
262  wss >> object;
263  return object;
264  }
265 
277  template <typename T> T FromString(const std::wstring& input)
278  {
279  return FromWideString<T>(input);
280  }
281 
282 //--------------------------------------------------------------------------------------------------------------------//
283 //--------------------------------------------------------------------------------------------------------------------//
284 //--------------------------------------------------------------------------------------------------------------------//
285 
297 #ifdef tb_with_wide_string
298  typedef std::wstring tbString;
299 // class tbString : public std::wstring
300 // {
301 // public:
302 // tbString(void) : std::wstring(L"") { }
303 // tbString(const tbString& input) : std::wstring(input.c_str()) { }
304 // tbString(const std::wstring& input) : std::wstring(input) { }
305 // tbString(const std::string& input) : std::wstring(ToWideString(input)) { }
306 // tbString(const wchar_t* input) : std::wstring(input) { }
307 // tbString(const char* input) : std::wstring(ToWideString(input)) { }
308 //
309 // tbString& operator=(const tbString& input) { std::wstring::operator=(input.c_str()); return *this; }
310 // tbString& operator=(const std::wstring& input) { std::wstring::operator=(input); return *this; }
311 // tbString& operator=(const std::string& input) { std::wstring::operator=(ToWideString(input)); return *this; }
312 // tbString& operator=(const wchar_t* input) { std::wstring::operator=(input); return *this; }
313 // tbString& operator=(const char* input) { std::wstring::operator=(ToWideString(input)); return *this; }
314 //
315 // friend tbString operator+(const std::string& leftSide, const tbString& rightSide) { return ToWideString(leftSide) + rightSide.c_str(); }
316 // friend tbString operator+(const char* leftSide, const tbString& rightSide) { return ToWideString(leftSide) + rightSide.c_str(); }
317 //
318 // // tbString operator+(const tbString& input) { return std::wstring::operator+(std::wstring(input.c_str())); }
319 // // tbString operator+(const std::wstring& input) { return std::wstring::operator+(input); }
320 // // tbString operator+(const std::string& input) { return std::wstring::operator+(ToWideString(input)); }
321 // // tbString operator+(const wchar_t* input) { return std::wstring::operator+(input); }
322 // // tbString operator+(const char* input) { return std::wstring::operator+(ToWideString(input.c_str())); }
323 //
324 // operator const void*(void) const { return c_str(); }
325 //
326 // //operator const wchar_t*(void) const { return c_str(); }
327 //
328 // //std::istream& operator>>(std::istream& inputStream) { std::wstring s; inputStream >> s; *this = s; return inputStream; }
333 // };
334 #else
335  typedef std::string tbString;
336 // class tbString : public std::string
337 // {
338 // public:
339 // tbString(void) : std::string("") { }
340 // tbString(const tbString& input) : std::string(input.c_str()) { }
341 // tbString(const std::wstring& input) : std::string(ToStdString(input)) { }
342 // tbString(const std::string& input) : std::string(input) { }
343 // tbString(const wchar_t* input) : std::string(ToStdString(input)) { }
344 // tbString(const char* input) : std::string(input) { }
345 //
346 // tbString& operator=(const tbString& input) { std::string::operator=(input.c_str()); return *this; }
347 // tbString& operator=(const std::wstring& input) { std::string::operator=(ToStdString(input)); return *this; }
348 // tbString& operator=(const std::string& input) { std::string::operator=(input); return *this; }
349 // tbString& operator=(const wchar_t* input) { std::string::operator=(ToStdString(input)); return *this; }
350 // tbString& operator=(const char* input) { std::string::operator=(input); return *this; }
351 //
352 // friend tbString operator+(const std::wstring& leftSide, const tbString& rightSide) { return ToStdString(leftSide) + rightSide.c_str(); }
353 // friend tbString operator+(const wchar_t* leftSide, const tbString& rightSide) { return ToStdString(leftSide) + rightSide.c_str(); }
354 //
355 // operator const void*(void) const { return c_str(); }
356 // };
357 
358 #endif /* tb_with_wide_string */
359 
371  template <typename T> tbString ToString(const T& object)
372  {
373 #ifdef tb_with_wide_string
374  return ToWideString(object);
375 #else
376  return ToStdString(object);
377 #endif /* tb_with_wide_string */
378  }
379 
380  inline tbString ToString(float value, int precision = 0)
381  {
382 #ifdef tb_with_wide_string
383  return ToWideString(value, precision);
384 #else
385  return ToStdString(value, precision);
386 #endif /* tb_with_wide_string */
387  }
388 
389  }; /* namespace Core */
390 }; /* namespace TurtleBrains */
391 
392 // inline operator float*(void) { return mFloatArray; }
393 
394 //std::string& operator=(std::string& leftSide, const std::wstring& rightSide) { lhs = TurtleBrains::Core::ToStdString(rightSide); return leftSide; }
395 //std::wstring& operator=(std::wstring& leftSide, const std::string& rightSide) { lhs = TurtleBrains::Core::ToWideString(rightSide); return rightSide; }
396 
397 //operator() std::string(const std::wstring& input) { return TurtleBrains::Core::ToStdString(input); }
398 //std::wstring operator(const std::string& input) { return TurtleBrains::Core::ToWideString(input); }
399 
400 namespace tbCore = TurtleBrains::Core;
401 
402 #endif /* _TurtleBrains_String_h_ */
T FromString(const std::string &input)
Definition: tb_string.h:154
tbString ToString(const T &object)
Definition: tb_string.h:371
std::string ToStdString(const T &object)
Definition: tb_string.h:50
T FromWideString(const std::wstring &input)
Definition: tb_string.h:257
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
T FromStdString(const std::string &input)
Definition: tb_string.h:134
std::wstring ToWideString(const T &object)
Definition: tb_string.h:174
Contains core functionality for each component of the API.
Definition: tb_debug_logger.h:91
#define tb_error_if(errorTest, message,...)
Definition: tb_error.h:37
std::string tbString
Definition: tb_string.h:335