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.

77 lines
2.4 KiB

17 years ago
17 years ago
17 years ago
17 years ago
  1. /**
  2. * @file macros.h
  3. * @brief This file contains miscellaneous helper definitions and functions.
  4. */
  5. #ifndef MACROS_H
  6. #define MACROS_H
  7. #include <wx/wx.h>
  8. /**
  9. * Macro TO_UTF8
  10. * converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
  11. * wxstring is a wxString, not a wxT() or _(). The scope of the return value
  12. * is very limited and volatile, but can be used with printf() style functions well.
  13. * NOTE: Trying to convert it to a function is tricky because of the
  14. * type of the parameter!
  15. */
  16. #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
  17. /**
  18. * function FROM_UTF8
  19. * converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
  20. */
  21. static inline wxString FROM_UTF8( const char* cstring )
  22. {
  23. wxString line = wxString::FromUTF8( cstring );
  24. if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
  25. line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
  26. return line;
  27. }
  28. /**
  29. * Function GetChars
  30. * returns a wxChar* to the actual character data within a wxString, and is
  31. * helpful for passing strings to wxString::Printf(wxT("%s"), GetChars(wxString) )
  32. * <p>
  33. * wxChar is defined to be
  34. * <ul>
  35. * <li> standard C style char when wxUSE_UNICODE==0 </li>
  36. * <li> wchar_t when wxUSE_UNICODE==1 (the default). </li>
  37. * </ul>
  38. * i.e. it depends on how the wxWidgets library was compiled. There was a period
  39. * during the development of wxWidgets 2.9 when GetData() was missing, so this
  40. * function was used to provide insulation from that design change. It may
  41. * no longer be needed, and is harmless. GetData() seems to be an acceptable
  42. * alternative in all cases now.
  43. */
  44. static inline const wxChar* GetChars( const wxString& s )
  45. {
  46. #if wxCHECK_VERSION( 2, 9, 0 )
  47. return (const wxChar*) s.c_str();
  48. #else
  49. return s.GetData();
  50. #endif
  51. }
  52. // This really needs a function? well, it is used *a lot* of times
  53. template<class T> inline void NEGATE( T &x ) { x = -x; }
  54. /// # of elements in an array
  55. #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
  56. /// Exchange two values
  57. // std::swap works only with arguments of the same type (which is saner);
  58. // here the compiler will figure out what to do (I hope to get rid of
  59. // this soon or late)
  60. template<class T, class T2> inline void EXCHG( T& a, T2& b )
  61. {
  62. T temp = a;
  63. a = b;
  64. b = temp;
  65. }
  66. #endif /* ifdef MACRO_H */