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.

152 lines
5.5 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. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2015 KiCad Developers, see CHANGELOG.TXT for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file macros.h
  26. * @brief This file contains miscellaneous commonly used macros and functions.
  27. */
  28. #ifndef MACROS_H
  29. #define MACROS_H
  30. #include <wx/wx.h>
  31. #include <vector>
  32. #include <map>
  33. #include <set>
  34. #include <memory> // std::shared_ptr
  35. /**
  36. * Macro TO_UTF8
  37. * converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
  38. * wxstring is a wxString, not a wxT() or _(). The scope of the return value
  39. * is very limited and volatile, but can be used with printf() style functions well.
  40. * NOTE: Trying to convert it to a function is tricky because of the
  41. * type of the parameter!
  42. */
  43. #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
  44. /**
  45. * function FROM_UTF8
  46. * converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
  47. */
  48. static inline wxString FROM_UTF8( const char* cstring )
  49. {
  50. wxString line = wxString::FromUTF8( cstring );
  51. if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
  52. line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
  53. return line;
  54. }
  55. /// Utility to build comma separated lists in messages
  56. inline void AccumulateDescription( wxString &aDesc, const wxString &aItem )
  57. {
  58. if( !aDesc.IsEmpty() )
  59. aDesc << wxT(", ");
  60. aDesc << aItem;
  61. }
  62. /**
  63. * Function GetChars
  64. * returns a wxChar* to the actual wxChar* data within a wxString, and is
  65. * helpful for passing strings to wxString::Printf() and wxString::Format().
  66. * It can also be passed a UTF8 parameter which will be converted to wxString
  67. * by the compiler.
  68. * <p>
  69. * Example: wxString::Format( wxT( "%s" ), GetChars( UTF( "some text" ) ) );
  70. * <p>
  71. * When wxWidgets is properly built for KiCad, a const wxChar* points to either:
  72. * <ul>
  73. * <li> 32 bit unicode characters on linux/OSX or </li>
  74. * <li> 16 bit UTF16 characters on windows. </li>
  75. * </ul>
  76. * Note that you cannot pass 8 bit strings to wxString::Format() or Printf() so this
  77. * is a useful conversion function to wxChar*, which is needed by wxString::Format().
  78. *
  79. * @return const wxChar* - a pointer to the UNICODE or UTF16 (on windows) text.
  80. */
  81. static inline const wxChar* GetChars( const wxString& s )
  82. {
  83. return (const wxChar*) s.c_str();
  84. }
  85. /// # of elements in an array
  86. #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
  87. /**
  88. * Function MIRROR
  89. * Mirror @a aPoint in @a aMirrorRef.
  90. */
  91. template<typename T>
  92. T Mirror( T aPoint, T aMirrorRef )
  93. {
  94. return -( aPoint - aMirrorRef ) + aMirrorRef;
  95. }
  96. template<typename T>
  97. void MIRROR( T& aPoint, const T& aMirrorRef )
  98. {
  99. aPoint = Mirror( aPoint, aMirrorRef );
  100. }
  101. /**
  102. * Function Clamp
  103. * limits @a value within the range @a lower <= @a value <= @a upper. It will work
  104. * on temporary expressions, since they are evaluated only once, and it should work
  105. * on most if not all numeric types, string types, or any type for which "operator < ()"
  106. * is present. The arguments are accepted in this order so you can remember the
  107. * expression as a memory aid:
  108. * <p>
  109. * result is: lower <= value <= upper
  110. */
  111. template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
  112. {
  113. wxASSERT( lower <= upper );
  114. if( value < lower )
  115. return lower;
  116. else if( upper < value )
  117. return upper;
  118. return value;
  119. }
  120. #ifdef SWIG
  121. /// Declare a std::vector and also the swig %template in unison
  122. #define DECL_VEC_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) vector<MemberType>; } typedef std::vector<MemberType> TypeName;
  123. #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) namespace std { %template(TypeName) map<KeyType, ValueType>; } typedef std::map<KeyType, ValueType> TypeName;
  124. #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) %shared_ptr(MemberType) namespace std { %template(TypeName) shared_ptr<MemberType>; } typedef std::shared_ptr<MemberType> TypeName;
  125. #define DECL_SET_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) set<MemberType>; } typedef std::set<MemberType> TypeName;
  126. #else
  127. /// Declare a std::vector but no swig %template
  128. #define DECL_VEC_FOR_SWIG(TypeName, MemberType) typedef std::vector<MemberType> TypeName;
  129. #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) typedef std::map<KeyType, ValueType> TypeName;
  130. #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) typedef std::shared_ptr<MemberType> TypeName;
  131. #define DECL_SET_FOR_SWIG(TypeName, MemberType) typedef std::set<MemberType> TypeName;
  132. #endif
  133. #endif // MACROS_H