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 
53 static inline wxString FROM_UTF8( const char* cstring )
54 {
55  wxString line = wxString::FromUTF8( cstring );
56 
57  if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
58  line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
59 
60  return line;
61 }
62 
63 
65 inline void AccumulateDescription( wxString &aDesc, const wxString &aItem )
66 {
67  if( !aDesc.IsEmpty() )
68  aDesc << wxT(", ");
69 
70  aDesc << aItem;
71 }
72 
92 static inline const wxChar* GetChars( const wxString& s )
93 {
94  return (const wxChar*) s.c_str();
95 }
96 
98 #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
99 
100 
105 template<typename T>
106 T Mirror( T aPoint, T aMirrorRef )
107 {
108  return -( aPoint - aMirrorRef ) + aMirrorRef;
109 }
110 template<typename T>
111 void MIRROR( T& aPoint, const T& aMirrorRef )
112 {
113  aPoint = Mirror( aPoint, aMirrorRef );
114 }
115 
116 
127 template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
128 {
129  wxASSERT( lower <= upper );
130  if( value < lower )
131  return lower;
132  else if( upper < value )
133  return upper;
134  return value;
135 }
136 
137 
138 #ifdef SWIG
139 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) vector<MemberType>; } typedef std::vector<MemberType> TypeName;
141 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) namespace std { %template(TypeName) map<KeyType, ValueType>; } typedef std::map<KeyType, ValueType> TypeName;
142 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) %shared_ptr(MemberType) namespace std { %template(TypeName) shared_ptr<MemberType>; } typedef std::shared_ptr<MemberType> TypeName;
143 #define DECL_SET_FOR_SWIG(TypeName, MemberType) namespace std { %template(TypeName) set<MemberType>; } typedef std::set<MemberType> TypeName;
144 #else
145 #define DECL_VEC_FOR_SWIG(TypeName, MemberType) typedef std::vector<MemberType> TypeName;
147 #define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType) typedef std::map<KeyType, ValueType> TypeName;
148 #define DECL_SPTR_FOR_SWIG(TypeName, MemberType) typedef std::shared_ptr<MemberType> TypeName;
149 #define DECL_SET_FOR_SWIG(TypeName, MemberType) typedef std::set<MemberType> TypeName;
150 #endif
151 
152 #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:127
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:53
T
enum T contains all this lexer's tokens.
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:111
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
Definition: macros.h:65
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:92
T Mirror(T aPoint, T aMirrorRef)
Function MIRROR Mirror aPoint in aMirrorRef.
Definition: macros.h:106