KiCad PCB EDA Suite
common.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) 2014-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2007-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
7  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #ifndef INCLUDE__COMMON_H_
33 #define INCLUDE__COMMON_H_
34 
35 #include <vector>
36 #include <functional>
37 
38 #include <wx/wx.h>
39 #include <wx/confbase.h>
40 #include <wx/fileconf.h>
41 #include <wx/dir.h>
42 
43 #include <richio.h>
44 #include <gal/color4d.h>
45 
46 #include <atomic>
47 #include <limits>
48 #include <memory>
49 #include <type_traits>
50 #include <typeinfo>
51 #include <boost/uuid/uuid.hpp>
52 #include <macros.h>
53 
54 class PROJECT;
55 class SEARCH_STACK;
56 class REPORTER;
57 
66 typedef uint32_t timestamp_t;
67 
68 class KIID
69 {
70 public:
71  KIID();
72  KIID( int null );
73  KIID( const wxString& aString );
74  KIID( timestamp_t aTimestamp );
75 
76  void Clone( const KIID& aUUID );
77 
78  size_t Hash() const;
79 
80  bool IsLegacyTimestamp() const;
82 
83  wxString AsString() const;
84  wxString AsLegacyTimestampString() const;
85 
92 
93  bool operator==( KIID const& rhs) const
94  {
95  return m_uuid == rhs.m_uuid;
96  }
97 
98  bool operator!=( KIID const& rhs) const
99  {
100  return m_uuid != rhs.m_uuid;
101  }
102 
103  bool operator<( KIID const& rhs) const
104  {
105  return m_uuid < rhs.m_uuid;
106  }
107 
108 private:
109  boost::uuids::uuid m_uuid;
110 
112 };
113 
114 
115 extern KIID niluuid;
116 
117 KIID& NilUuid();
118 
119 // declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
120 DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
121 
122 class KIID_PATH : public KIID_VECT_LIST
123 {
124 public:
126  {}
127 
128  KIID_PATH( const wxString& aString )
129  {
130  for( const wxString& pathStep : wxSplit( aString, '/' ) )
131  {
132  if( !pathStep.empty() )
133  emplace_back( KIID( pathStep ) );
134  }
135  }
136 
137  wxString AsString() const
138  {
139  wxString path;
140 
141  for( const KIID& pathStep : *this )
142  path += '/' + pathStep.AsString();
143 
144  return path;
145  }
146 
147  bool operator==( KIID_PATH const& rhs) const
148  {
149  if( size() != rhs.size() )
150  return false;
151 
152  for( size_t i = 0; i < size(); ++i )
153  {
154  if( at( i ) != rhs.at( i ) )
155  return false;
156  }
157 
158  return true;
159  }
160 
161  bool operator<( KIID_PATH const& rhs) const
162  {
163  if( size() != rhs.size() )
164  return size() < rhs.size();
165 
166  for( size_t i = 0; i < size(); ++i )
167  {
168  if( at( i ) < rhs.at( i ) )
169  return true;
170 
171  if( at( i ) != rhs.at( i ) )
172  return false;
173  }
174 
175  return false;
176  }
177 };
178 
179 
181 #define NAMELESS_PROJECT wxT( "noname" )
182 
185 #define TEXT_ANGLE_HORIZ 0
186 #define TEXT_ANGLE_VERT 900
187 
191 enum class EDA_DATA_TYPE
192 {
193  DISTANCE = 0,
194  AREA = 1,
195  VOLUME = 2
196 };
197 
198 enum class EDA_UNITS
199 {
200  INCHES = 0,
201  MILLIMETRES = 1,
202  UNSCALED = 2,
203  DEGREES = 3,
204  PERCENT = 4,
205 };
206 
207 
217 {
218 public:
219  LOCALE_IO();
220  ~LOCALE_IO();
221 
222 private:
223  // allow for nesting of LOCALE_IO instantiations
224  static std::atomic<unsigned int> m_c_count;
225 
226  // The locale in use before switching to the "C" locale
227  // (the locale can be set by user, and is not always the system locale)
228  std::string m_user_locale;
229  wxLocale* m_wxLocale;
230 };
231 
236 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
237 
252 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
253 
257 void SelectReferenceNumber( wxTextEntry* aTextEntry );
258 
270 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
271  wxProcess *callback = NULL );
272 
280 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
281 
301 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
302 
312 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
313  const wxString& aBaseFilename,
314  REPORTER* aReporter = NULL );
315 
317 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
318 
325 const wxString ExpandEnvVarSubstitutions( const wxString& aString, PROJECT* aProject );
326 
331 wxString ExpandTextVars( const wxString& aSource,
332  const std::function<bool( wxString* )>* aLocalResolver,
333  const PROJECT* aProject,
334  const std::function<bool( wxString* )>* aFallbackResolver = nullptr );
335 
340 const wxString ResolveUriByEnvVars( const wxString& aUri, PROJECT* aProject );
341 
342 
343 #ifdef __WXMAC__
344 
348 wxString GetOSXKicadUserDataDir();
349 
354 wxString GetOSXKicadMachineDataDir();
355 
360 wxString GetOSXKicadDataDir();
361 #endif
362 
363 // Some wxWidgets versions (for instance before 3.1.0) do not include
364 // this function, so add it if missing
365 #if !wxCHECK_VERSION( 3, 1, 0 )
366 #define USE_KICAD_WXSTRING_HASH // for common.cpp
367 namespace std
369 {
370  template<> struct hash<wxString>
371  {
372  size_t operator()( const wxString& s ) const;
373  };
374 }
375 #endif
376 
378 #define USE_KICAD_WXPOINT_LESS_AND_HASH // for common.cpp
379 namespace std
380 {
381  template <> struct hash<wxPoint>
382  {
383  size_t operator() ( const wxPoint& k ) const;
384  };
385 }
386 
387 namespace std
388 {
389  template<> struct less<wxPoint>
390  {
391  bool operator()( const wxPoint& aA, const wxPoint& aB ) const;
392  };
393 }
394 
401 std::ostream& operator<<( std::ostream& out, const wxSize& size );
402 
409 std::ostream& operator<<( std::ostream& out, const wxPoint& pt );
410 
411 
416 {
417 public:
418  WX_FILENAME( const wxString& aPath, const wxString& aFilename );
419 
420  void SetFullName( const wxString& aFileNameAndExtension );
421 
422  wxString GetName() const;
423  wxString GetFullName() const;
424  wxString GetPath() const;
425  wxString GetFullPath() const;
426 
427  // Avoid multiple calls to stat() on POSIX kernels.
428  long long GetTimestamp();
429 
430 private:
431  // Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
432  void resolve();
433 
434  wxFileName m_fn;
435  wxString m_path;
436  wxString m_fullName;
437 };
438 
439 
440 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
441 
442 
443 
444 
445 #endif // INCLUDE__COMMON_H_
EDA_UNITS
Definition: common.h:198
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:767
~LOCALE_IO()
Definition: common.cpp:248
PROJECT holds project specific data.
Definition: project.h:61
KIID()
Definition: common.cpp:64
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:216
EDA_DATA_TYPE
The type of unit.
Definition: common.h:191
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject, const std::function< bool(wxString *)> *aFallbackResolver=nullptr)
Expand '${var-name}' templates in text.
Definition: common.cpp:384
bool operator!=(KIID const &rhs) const
Definition: common.h:98
wxLocale * m_wxLocale
Definition: common.h:229
const wxString PrePendPath(const wxString &aEnvVar, const wxString &aPriorityPath)
Put aPriorityPath in front of all paths in the value of aEnvVar.
Definition: prependpath.cpp:32
wxString GetName() const
Definition: common.cpp:780
bool operator==(KIID const &rhs) const
Definition: common.h:93
wxString AsString() const
Definition: common.cpp:165
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
wxString GetFullName() const
Definition: common.cpp:787
wxString m_path
Definition: common.h:435
boost::uuids::uuid m_uuid
Definition: common.h:109
KIID_PATH(const wxString &aString)
Definition: common.h:128
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:774
bool operator==(KIID_PATH const &rhs) const
Definition: common.h:147
KIID & NilUuid()
Definition: common.cpp:57
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:973
bool IsLegacyTimestamp() const
Definition: common.cpp:132
timestamp_t m_cached_timestamp
Definition: common.h:111
This file contains miscellaneous commonly used macros and functions.
wxFileName m_fn
Definition: common.h:434
bool operator<(KIID const &rhs) const
Definition: common.h:103
static std::atomic< unsigned int > m_c_count
Definition: common.h:224
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Run a command in a child process.
Definition: common.cpp:366
Definition: common.h:68
long long GetTimestamp()
Definition: common.cpp:814
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Return the help file's full path.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:581
void resolve()
Definition: common.cpp:806
size_t Hash() const
Definition: common.cpp:144
const wxString ResolveUriByEnvVars(const wxString &aUri, PROJECT *aProject)
Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:594
#define NULL
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:268
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:738
KIID_PATH()
Definition: common.h:125
timestamp_t AsLegacyTimestamp() const
Definition: common.cpp:138
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:343
#define DECL_VEC_FOR_SWIG(TypeName, MemberType)
Declare a std::vector but no swig template.
Definition: macros.h:190
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=NULL)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
Definition: common.cpp:283
KIID niluuid
LOCALE_IO()
Definition: common.cpp:227
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition: common.cpp:177
wxString GetPath() const
Definition: common.cpp:793
void Clone(const KIID &aUUID)
Definition: common.cpp:158
wxString GetFullPath() const
Definition: common.cpp:799
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:415
wxString AsLegacyTimestampString() const
Definition: common.cpp:171
wxString AsString() const
Definition: common.h:137
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:312
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter=NULL)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition: common.cpp:609
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:56
wxString m_fullName
Definition: common.h:436
bool operator<(KIID_PATH const &rhs) const
Definition: common.h:161
std::string m_user_locale
Definition: common.h:228