KiCad PCB EDA Suite
io_mgr.h
Go to the documentation of this file.
1#ifndef IO_MGR_H_
2#define IO_MGR_H_
3
4/*
5 * This program source code file is part of KICAD, a free EDA CAD application.
6 *
7 * Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
8 * Copyright (C) 2016-2020 Kicad Developers, see AUTHORS.txt for contributors.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#include <richio.h>
29#include <map>
30#include <functional>
31#include <wx/time.h>
32
33#include <config.h>
34
35class BOARD;
36class PLUGIN;
37class FOOTPRINT;
38class PROPERTIES;
39class PROJECT;
41
45class IO_MGR
46{
47public:
48
54 {
65 // add your type here.
66
67 // etc.
68
70 };
71
77 {
78 public:
79 struct ENTRY
80 {
82 std::function<PLUGIN*(void)> m_createFunc;
83 wxString m_name;
84 };
85
87 {
88 static PLUGIN_REGISTRY *self = nullptr;
89
90 if( !self )
91 {
92 self = new PLUGIN_REGISTRY;
93 }
94 return self;
95 }
96
97 void Register( PCB_FILE_T aType, const wxString& aName,
98 std::function<PLUGIN*(void)> aCreateFunc )
99 {
100 ENTRY ent;
101 ent.m_type = aType;
102 ent.m_createFunc = aCreateFunc;
103 ent.m_name = aName;
104 m_plugins.push_back( ent );
105 }
106
107 PLUGIN* Create( PCB_FILE_T aFileType ) const
108 {
109 for( auto& ent : m_plugins )
110 {
111 if ( ent.m_type == aFileType )
112 {
113 return ent.m_createFunc();
114 }
115 }
116
117 return nullptr;
118 }
119
120 const std::vector<ENTRY>& AllPlugins() const
121 {
122 return m_plugins;
123 }
124
125 private:
126 std::vector<ENTRY> m_plugins;
127 };
128
139 {
140 REGISTER_PLUGIN( PCB_FILE_T aType, const wxString& aName,
141 std::function<PLUGIN*(void)> aCreateFunc )
142 {
143 PLUGIN_REGISTRY::Instance()->Register( aType, aName, aCreateFunc );
144 }
145 };
146
147
160 static PLUGIN* PluginFind( PCB_FILE_T aFileType );
161
168 static void PluginRelease( PLUGIN* aPlugin );
169
173 static const wxString ShowType( PCB_FILE_T aFileType );
174
178 static PCB_FILE_T EnumFromStr( const wxString& aFileType );
179
186 static const wxString GetFileExtension( PCB_FILE_T aFileType );
187
191 static PCB_FILE_T GuessPluginTypeFromLibPath( const wxString& aLibPath );
192
212 static BOARD* Load( PCB_FILE_T aFileType, const wxString& aFileName,
213 BOARD* aAppendToMe = nullptr, const PROPERTIES* aProperties = nullptr,
214 PROJECT* aProject = nullptr,
215 PROGRESS_REPORTER* aProgressReporter = nullptr );
216
236 static void Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
237 const PROPERTIES* aProperties = nullptr );
238};
239
240
270{
271public:
275 virtual const wxString PluginName() const = 0;
276
280 virtual const wxString GetFileExtension() const = 0;
281
285 virtual void SetQueryUserCallback( std::function<bool( wxString aTitle, int aIcon,
286 wxString aMessage,
287 wxString aAction )> aCallback )
288 { }
289
318 virtual BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
319 const PROPERTIES* aProperties = nullptr, PROJECT* aProject = nullptr,
320 PROGRESS_REPORTER* aProgressReporter = nullptr );
321
330 virtual std::vector<FOOTPRINT*> GetImportedCachedLibraryFootprints();
331
350 virtual void Save( const wxString& aFileName, BOARD* aBoard,
351 const PROPERTIES* aProperties = nullptr );
352
367 virtual void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
368 bool aBestEfforts, const PROPERTIES* aProperties = nullptr );
369
376 virtual long long GetLibraryTimestamp( const wxString& aLibraryPath ) const = 0;
377
395 virtual void PrefetchLib( const wxString& aLibraryPath,
396 const PROPERTIES* aProperties = nullptr );
397
419 virtual FOOTPRINT* FootprintLoad( const wxString& aLibraryPath,
420 const wxString& aFootprintName,
421 bool aKeepUUID = false,
422 const PROPERTIES* aProperties = nullptr );
423
428 virtual const FOOTPRINT* GetEnumeratedFootprint( const wxString& aLibraryPath,
429 const wxString& aFootprintName,
430 const PROPERTIES* aProperties = nullptr );
431
435 virtual bool FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
436 const PROPERTIES* aProperties = nullptr );
437
454 virtual void FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
455 const PROPERTIES* aProperties = nullptr );
456
471 virtual void FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
472 const PROPERTIES* aProperties = nullptr );
473
490 virtual void FootprintLibCreate( const wxString& aLibraryPath,
491 const PROPERTIES* aProperties = nullptr );
492
509 virtual bool FootprintLibDelete( const wxString& aLibraryPath,
510 const PROPERTIES* aProperties = nullptr );
511
522 virtual bool IsFootprintLibWritable( const wxString& aLibraryPath );
523
551 virtual void FootprintLibOptions( PROPERTIES* aListToAppendTo ) const;
552
553 virtual ~PLUGIN()
554 {
555 //printf( "~%s", __func__ );
556 };
557
558
559#ifndef SWIG
564 {
566
567 // private assignment operator so it's illegal
568 RELEASER& operator=( RELEASER& aOther ) { return *this; }
569
570 // private copy constructor so it's illegal
571 RELEASER( const RELEASER& aOther ) : plugin( nullptr ) {}
572
573 public:
574 RELEASER( PLUGIN* aPlugin = nullptr ) :
575 plugin( aPlugin )
576 {
577 }
578
580 {
581 if( plugin )
582 release();
583 }
584
585 void release()
586 {
588 plugin = nullptr;
589 }
590
591 void set( PLUGIN* aPlugin )
592 {
593 if( plugin )
594 release();
595 plugin = aPlugin;
596 }
597
598 operator PLUGIN* () const
599 {
600 return plugin;
601 }
602
604 {
605 return plugin;
606 }
607 };
608#endif
609};
610
611#endif // IO_MGR_H_
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
Hold a list of available plugins, created using a singleton REGISTER_PLUGIN object.
Definition: io_mgr.h:77
std::vector< ENTRY > m_plugins
Definition: io_mgr.h:126
PLUGIN * Create(PCB_FILE_T aFileType) const
Definition: io_mgr.h:107
const std::vector< ENTRY > & AllPlugins() const
Definition: io_mgr.h:120
static PLUGIN_REGISTRY * Instance()
Definition: io_mgr.h:86
void Register(PCB_FILE_T aType, const wxString &aName, std::function< PLUGIN *(void)> aCreateFunc)
Definition: io_mgr.h:97
A factory which returns an instance of a PLUGIN.
Definition: io_mgr.h:46
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
Definition: io_mgr.cpp:77
static BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Find the requested PLUGIN and if found, calls the PLUGIN::Load() function on it using the arguments p...
Definition: io_mgr.cpp:162
static void PluginRelease(PLUGIN *aPlugin)
Release a PLUGIN back to the system and may cause it to be unloaded from memory.
Definition: io_mgr.cpp:67
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=nullptr)
Write either a full aBoard to a storage file in a format that this implementation knows about,...
Definition: io_mgr.cpp:178
static const wxString GetFileExtension(PCB_FILE_T aFileType)
Return the file extension for aFileType.
Definition: io_mgr.cpp:109
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a footprint library's libPath.
Definition: io_mgr.cpp:124
PCB_FILE_T
The set of file types that the IO_MGR knows about, and for which there has been a plugin written.
Definition: io_mgr.h:54
@ LEGACY
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:55
@ ALTIUM_DESIGNER
Definition: io_mgr.h:60
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition: io_mgr.h:56
@ ALTIUM_CIRCUIT_MAKER
Definition: io_mgr.h:62
@ PCAD
Definition: io_mgr.h:58
@ EAGLE
Definition: io_mgr.h:57
@ FILE_TYPE_NONE
Definition: io_mgr.h:69
@ CADSTAR_PCB_ARCHIVE
Definition: io_mgr.h:63
@ GEDA_PCB
Geda PCB file formats.
Definition: io_mgr.h:64
@ FABMASTER
Definition: io_mgr.h:59
@ ALTIUM_CIRCUIT_STUDIO
Definition: io_mgr.h:61
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Return the PCB_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
Definition: io_mgr.cpp:93
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Return a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:564
RELEASER(const RELEASER &aOther)
Definition: io_mgr.h:571
PLUGIN * plugin
Definition: io_mgr.h:565
RELEASER & operator=(RELEASER &aOther)
Definition: io_mgr.h:568
RELEASER(PLUGIN *aPlugin=nullptr)
Definition: io_mgr.h:574
void release()
Definition: io_mgr.h:585
PLUGIN * operator->() const
Definition: io_mgr.h:603
void set(PLUGIN *aPlugin)
Definition: io_mgr.h:591
A base class that BOARD loading and saving plugins should derive from.
Definition: io_mgr.h:270
virtual void PrefetchLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr)
If possible, prefetches the specified library (e.g.
Definition: plugin.cpp:75
virtual void SetQueryUserCallback(std::function< bool(wxString aTitle, int aIcon, wxString aMessage, wxString aAction)> aCallback)
Registers a KIDIALOG callback for collecting info from the user.
Definition: io_mgr.h:285
virtual void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=nullptr)
Write aBoard to a storage file in a format that this PLUGIN implementation knows about or it can be u...
Definition: plugin.cpp:60
virtual void FootprintLibCreate(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr)
Create a new empty footprint library at aLibraryPath empty.
Definition: plugin.cpp:124
virtual std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints()
Return a container with the cached library footprints generated in the last call to Load.
Definition: plugin.cpp:54
virtual void FootprintLibOptions(PROPERTIES *aListToAppendTo) const
Append supported PLUGIN options to aListToAppenTo along with internationalized descriptions.
Definition: plugin.cpp:147
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr)
Check for the existence of a footprint.
Definition: plugin.cpp:89
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const PROPERTIES *aProperties=nullptr)
Return a list of footprint names contained within the library at aLibraryPath.
Definition: plugin.cpp:67
virtual bool IsFootprintLibWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: plugin.cpp:139
virtual const wxString PluginName() const =0
Return a brief hard coded name for this PLUGIN.
virtual bool FootprintLibDelete(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr)
Delete an existing footprint library and returns true, or if library does not exist returns false,...
Definition: plugin.cpp:131
virtual const wxString GetFileExtension() const =0
Returns the file extension for the PLUGIN.
virtual void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const PROPERTIES *aProperties=nullptr)
Write aFootprint to an existing library located at aLibraryPath.
Definition: plugin.cpp:108
virtual long long GetLibraryTimestamp(const wxString &aLibraryPath) const =0
Generate a timestamp representing all the files in the library (including the library directory).
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr)
Delete aFootprintName from the library at aLibraryPath.
Definition: plugin.cpp:116
virtual BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Load information from some input file format that this PLUGIN implementation knows about into either ...
Definition: plugin.cpp:46
virtual ~PLUGIN()
Definition: io_mgr.h:553
virtual const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
Definition: plugin.cpp:80
virtual FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const PROPERTIES *aProperties=nullptr)
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
Definition: plugin.cpp:97
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:63
A name/value tuple with unique names and optional values.
Definition: properties.h:34
std::function< PLUGIN *(void)> m_createFunc
Definition: io_mgr.h:82
Register a plugin.
Definition: io_mgr.h:139
REGISTER_PLUGIN(PCB_FILE_T aType, const wxString &aName, std::function< PLUGIN *(void)> aCreateFunc)
Definition: io_mgr.h:140