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) 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 
30 #ifndef MACROS_H
31 #define MACROS_H
32 
33 #include <wx/wx.h>
34 #include <deque>
35 #include <vector>
36 #include <map>
37 #include <set>
38 #include <memory> // std::shared_ptr
39 
48 #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
49 
55 #define TO_STR2(x) #x
56 #define TO_STR(x) TO_STR2(x)
57 
62 static inline wxString FROM_UTF8( const char* cstring )
63 {
64  wxString line = wxString::FromUTF8( cstring );
65 
66  if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
67  line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
68 
69  return line;
70 }
71 
72 
74 inline void AccumulateDescription( wxString &aDesc, const wxString &aItem )
75 {
76  if( !aDesc.IsEmpty() )
77  aDesc << wxT(", ");
78 
79  aDesc << aItem;
80 }
81 
101 static inline const wxChar* GetChars( const wxString& s )
102 {
103  return (const wxChar*) s.c_str();
104 }
105 
107 template <typename T, std::size_t N>
108 constexpr std::size_t arrayDim(T const (&)[N]) noexcept
109 {
110  return N;
111 }
112 
117 template<typename T>
118 T Mirror( T aPoint, T aMirrorRef )
119 {
120  return -( aPoint - aMirrorRef ) + aMirrorRef;
121 }
122 template<typename T>
123 void MIRROR( T& aPoint, const T& aMirrorRef )
124 {
125  aPoint = Mirror( aPoint, aMirrorRef );
126 }
127 
128 
139 template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
140 {
141  wxASSERT( lower <= upper );
142  if( value < lower )
143  return lower;
144  else if( upper < value )
145  return upper;
146  return value;
147 }
148 
149 
150 #ifdef SWIG
151 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) vector<MemberType>; } typedef std::vector<MemberType> TypeName;
153 #define DECL_DEQ_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) deque<MemberType>; } typedef std::deque<MemberType> TypeName;
154 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) namespace std { %template(TypeName) map<KeyType, ValueType>; } typedef std::map<KeyType, ValueType> TypeName;
155 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) %shared_ptr(MemberType) namespace std { %template(TypeName) shared_ptr<MemberType>; } typedef std::shared_ptr<MemberType> TypeName;
156 #define DECL_SET_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) set<MemberType>; } typedef std::set<MemberType> TypeName;
157 #else
158 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) typedef std::vector<MemberType> TypeName;
160 #define DECL_DEQ_FOR_SWIG(TypeName, MemberType) typedef std::deque<MemberType> TypeName;
161 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) typedef std::map<KeyType, ValueType> TypeName;
162 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) typedef std::shared_ptr<MemberType> TypeName;
163 #define DECL_SET_FOR_SWIG(TypeName, MemberType) typedef std::set<MemberType> TypeName;
164 #endif
165 
166 #endif // MACROS_H
const T & Clamp(const T &lower, const T &value, const T &upper)
Function Clamp limits value within the range lower <= value <= upper.
Definition: macros.h:139
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:62
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:123
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: macros.h:74
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:101
T Mirror(T aPoint, T aMirrorRef)
Function MIRROR Mirror aPoint in aMirrorRef.
Definition: macros.h:118