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.

101 lines
3.2 KiB

17 years ago
12 years ago
12 years ago
12 years ago
12 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 wxChar* data within a wxString, and is
  31. * helpful for passing strings to wxString::Printf() and wxString::Format().
  32. * It can also be passed a UTF8 parameter which will be converted to wxString
  33. * by the compiler.
  34. * <p>
  35. * Example: wxString::Format( wxT( "%s" ), GetChars( UTF( "some text" ) ) );
  36. * <p>
  37. * When wxWidgets is properly built for KiCad, a const wxChar* points to either:
  38. * <ul>
  39. * <li> 32 bit unicode characters on linux/OSX or </li>
  40. * <li> 16 bit UTF16 characters on windows. </li>
  41. * </ul>
  42. * Note that you cannot pass 8 bit strings to wxString::Format() or Printf() so this
  43. * is a useful conversion function to wxChar*, which is needed by wxString::Format().
  44. *
  45. * @return const wxChar* - a pointer to the UNICODE or UTF16 (on windows) text.
  46. */
  47. static inline const wxChar* GetChars( const wxString& s )
  48. {
  49. #if wxCHECK_VERSION( 2, 9, 0 )
  50. return (const wxChar*) s.c_str();
  51. #else
  52. return s.GetData();
  53. #endif
  54. }
  55. // This really needs a function? well, it is used *a lot* of times
  56. template <class T> inline void NEGATE( T &x ) { x = -x; }
  57. /// # of elements in an array
  58. #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
  59. /// Exchange two values
  60. // std::swap works only with arguments of the same type (which is saner);
  61. // here the compiler will figure out what to do (I hope to get rid of
  62. // this soon or late)
  63. template <class T, class T2> inline void EXCHG( T& a, T2& b )
  64. {
  65. T temp = a;
  66. a = b;
  67. b = temp;
  68. }
  69. /**
  70. * Function Clamp
  71. * limits @a value within the range @a lower <= @a value <= @a upper. It will work
  72. * on temporary expressions, since they are evaluated only once, and it should work
  73. * on most if not all numeric types, string types, or any type for which "operator < ()"
  74. * is present. The arguments are accepted in this order so you can remember the
  75. * expression as a memory aid:
  76. * <p>
  77. * result is: lower <= value <= upper
  78. */
  79. template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
  80. {
  81. wxASSERT( lower <= upper );
  82. if( value < lower )
  83. return lower;
  84. else if( upper < value )
  85. return upper;
  86. return value;
  87. }
  88. #endif /* ifdef MACRO_H */