KiCad PCB EDA Suite
macros.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 Ian McInerney <ian.s.mcinerney@ieee.org>
5  * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
6  * Copyright (C) 1992-2020 KiCad Developers, see CHANGELOG.TXT for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef MACROS_H
32 #define MACROS_H
33 
34 #include <wx/wx.h>
35 #include <deque>
36 #include <vector>
37 #include <map>
38 #include <set>
39 #include <memory> // std::shared_ptr
40 
41 #if defined( __has_attribute )
42  #define KI_HAS_ATTRIBUTE( x ) __has_attribute( x )
43 #else
44  #define KI_HAS_ATTRIBUTE( x ) 0
45 #endif
46 
47 // Based on the declaration inside the LLVM source code
48 #if defined( __cplusplus ) && defined( __has_cpp_attribute )
49  #define KI_HAS_CPP_ATTRIBUTE( x ) __has_cpp_attribute( x )
50 #else
51  #define KI_HAS_CPP_ATTRIBUTE( x ) 0
52 #endif
53 
70 #if __cplusplus >= 201703L
71  // C++ 17 includes this macro on all compilers
72  #define KI_FALLTHROUGH [[fallthrough]]
73 
74 #elif KI_HAS_CPP_ATTRIBUTE( clang::fallthrough )
75  // Clang provides this attribute to silence the "-Wimplicit-fallthrough" warning
76  #define KI_FALLTHROUGH [[clang::fallthrough]]
77 
78 #elif KI_HAS_CPP_ATTRIBUTE( gnu::fallthrough )
79  // GNU-specific C++ attribute to sliencing the warning
80  #define KI_FALLTHROUGH [[gnu::fallthrough]]
81 
82 #elif defined( __GNUC__ ) && __GNUC__ >= 7
83  // GCC 7+ includes the "-Wimplicit-fallthrough" warning, and this attribute to silence it
84  #define KI_FALLTHROUGH __attribute__ ((fallthrough))
85 
86 #else
87  // In every other case, don't do anything
88  #define KI_FALLTHROUGH ( ( void ) 0 )
89 
90 #endif
91 
100 #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
101 
107 #define TO_STR2(x) #x
108 #define TO_STR(x) TO_STR2(x)
109 
114 static inline wxString FROM_UTF8( const char* cstring )
115 {
116  wxString line = wxString::FromUTF8( cstring );
117 
118  if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
119  line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
120 
121  return line;
122 }
123 
124 
126 inline void AccumulateDescription( wxString &aDesc, const wxString &aItem )
127 {
128  if( !aDesc.IsEmpty() )
129  aDesc << wxT(", ");
130 
131  aDesc << aItem;
132 }
133 
153 static inline const wxChar* GetChars( const wxString& s )
154 {
155  return (const wxChar*) s.c_str();
156 }
157 
159 template <typename T, std::size_t N>
160 constexpr std::size_t arrayDim(T const (&)[N]) noexcept
161 {
162  return N;
163 }
164 
169 template<typename T>
170 T Mirror( T aPoint, T aMirrorRef )
171 {
172  return -( aPoint - aMirrorRef ) + aMirrorRef;
173 }
174 template<typename T>
175 void MIRROR( T& aPoint, const T& aMirrorRef )
176 {
177  aPoint = Mirror( aPoint, aMirrorRef );
178 }
179 
180 
181 #ifdef SWIG
182 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) vector<MemberType>; } typedef std::vector<MemberType> TypeName;
184 #define DECL_DEQ_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) deque<MemberType>; } typedef std::deque<MemberType> TypeName;
185 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) namespace std { %template(TypeName) map<KeyType, ValueType>; } typedef std::map<KeyType, ValueType> TypeName;
186 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) %shared_ptr(MemberType) namespace std { %template(TypeName) shared_ptr<MemberType>; } typedef std::shared_ptr<MemberType> TypeName;
187 #define DECL_SET_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) set<MemberType>; } typedef std::set<MemberType> TypeName;
188 #else
189 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) typedef std::vector<MemberType> TypeName;
191 #define DECL_DEQ_FOR_SWIG(TypeName, MemberType) typedef std::deque<MemberType> TypeName;
192 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) typedef std::map<KeyType, ValueType> TypeName;
193 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) typedef std::shared_ptr<MemberType> TypeName;
194 #define DECL_SET_FOR_SWIG(TypeName, MemberType) typedef std::set<MemberType> TypeName;
195 #endif
196 
197 #endif // MACROS_H
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:114
T
enum T contains all this lexer's tokens.
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:175
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:160
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: macros.h:126
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:153
T Mirror(T aPoint, T aMirrorRef)
Function MIRROR Mirror aPoint in aMirrorRef.
Definition: macros.h:170