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