Utility.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #pragma once
  7. #ifndef GWEN_UTILITY_H
  8. #define GWEN_UTILITY_H
  9. #include <sstream>
  10. #include <vector>
  11. #include "Gwen/Structures.h"
  12. namespace Gwen
  13. {
  14. namespace Utility
  15. {
  16. template <typename T>
  17. const T& Max( const T& x, const T& y )
  18. {
  19. if ( y < x ) return x;
  20. return y;
  21. }
  22. template <typename T>
  23. const T& Min( const T& x, const T& y )
  24. {
  25. if ( y > x ) return x;
  26. return y;
  27. }
  28. #ifdef _MSC_VER
  29. #pragma warning( push )
  30. #pragma warning( disable : 4996 )
  31. #endif
  32. inline String UnicodeToString( const UnicodeString& strIn )
  33. {
  34. if ( !strIn.length() ) return "";
  35. String temp(strIn.length(), (char)0);
  36. std::use_facet< std::ctype<wchar_t> >(std::locale()). \
  37. narrow(&strIn[0], &strIn[0]+strIn.length(), ' ', &temp[0]);
  38. return temp;
  39. }
  40. inline UnicodeString StringToUnicode( const String& strIn )
  41. {
  42. if ( !strIn.length() ) return L"";
  43. UnicodeString temp(strIn.length(), (wchar_t)0);
  44. std::use_facet< std::ctype<wchar_t> >(std::locale()). \
  45. widen(&strIn[0], &strIn[0]+strIn.length(), &temp[0]);
  46. return temp;
  47. }
  48. #ifdef _MSC_VER
  49. #pragma warning( pop )
  50. #endif
  51. template <class T>
  52. String ToString( const T& object )
  53. {
  54. std::ostringstream os;
  55. os << object;
  56. return os.str();
  57. }
  58. inline Gwen::Rect ClampRectToRect( Gwen::Rect inside, Gwen::Rect outside, bool clampSize = false )
  59. {
  60. if ( inside.x < outside.x )
  61. inside.x = outside.x;
  62. if ( inside.y < outside.y )
  63. inside.y = outside.y;
  64. if ( inside.x + inside.w > outside.x + outside.w )
  65. {
  66. if ( clampSize )
  67. inside.w = outside.w;
  68. else
  69. inside.x = outside.x + outside.w - inside.w;
  70. }
  71. if ( inside.y + inside.h > outside.y + outside.h )
  72. {
  73. if ( clampSize )
  74. inside.h = outside.h;
  75. else
  76. inside.y = outside.w + outside.h - inside.h;
  77. }
  78. return inside;
  79. }
  80. GWEN_EXPORT UnicodeString Format( const wchar_t* fmt, ... );
  81. namespace Strings
  82. {
  83. typedef std::vector<Gwen::String> List;
  84. typedef std::vector<Gwen::UnicodeString> UnicodeList;
  85. GWEN_EXPORT void Split( const Gwen::String& str, const Gwen::String& seperator, Strings::List& outbits, bool bLeaveSeperators = false );
  86. GWEN_EXPORT void Split( const Gwen::UnicodeString& str, const Gwen::UnicodeString& seperator, Strings::UnicodeList& outbits, bool bLeaveSeperators = false );
  87. template <typename T>
  88. T TrimLeft( const T& str, const T& strChars )
  89. {
  90. T outstr = str;
  91. outstr.erase( 0, outstr.find_first_not_of( strChars ) );
  92. return outstr;
  93. }
  94. namespace To
  95. {
  96. GWEN_EXPORT bool Bool( const Gwen::String& str );
  97. GWEN_EXPORT int Int( const Gwen::String& str );
  98. GWEN_EXPORT float Float( const Gwen::String& str );
  99. GWEN_EXPORT bool Floats( const Gwen::String& str, float* f, size_t iCount );
  100. }
  101. }
  102. }
  103. }
  104. #endif