KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 STRING_UTF8_MAP;
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 STRING_UTF8_MAP* 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 STRING_UTF8_MAP* 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 STRING_UTF8_MAP* 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 STRING_UTF8_MAP* aProperties = nullptr );
352
367 virtual void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
368 bool aBestEfforts, const STRING_UTF8_MAP* aProperties = nullptr );
369
376 virtual long long GetLibraryTimestamp( const wxString& aLibraryPath ) const = 0;
377
395 virtual void PrefetchLib( const wxString& aLibraryPath,
396 const STRING_UTF8_MAP* aProperties = nullptr );
397
419 virtual FOOTPRINT* FootprintLoad( const wxString& aLibraryPath,
420 const wxString& aFootprintName,
421 bool aKeepUUID = false,
422 const STRING_UTF8_MAP* aProperties = nullptr );
423
428 virtual const FOOTPRINT* GetEnumeratedFootprint( const wxString& aLibraryPath,
429 const wxString& aFootprintName,
430 const STRING_UTF8_MAP* aProperties = nullptr );
431
435 virtual bool FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
436 const STRING_UTF8_MAP* aProperties = nullptr );
437
454 virtual void FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
455 const STRING_UTF8_MAP* aProperties = nullptr );
456
471 virtual void FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
472 const STRING_UTF8_MAP* aProperties = nullptr );
473
490 virtual void FootprintLibCreate( const wxString& aLibraryPath,
491 const STRING_UTF8_MAP* aProperties = nullptr );
492
509 virtual bool FootprintLibDelete( const wxString& aLibraryPath,
510 const STRING_UTF8_MAP* aProperties = nullptr );
511
522 virtual bool IsFootprintLibWritable( const wxString& aLibraryPath );
523
551 virtual void FootprintLibOptions( STRING_UTF8_MAP* aListToAppendTo ) const;
552
553 virtual ~PLUGIN()
554 {};
555
556
557#ifndef SWIG
562 {
564
565 // private assignment operator so it's illegal
566 RELEASER& operator=( RELEASER& aOther ) { return *this; }
567
568 // private copy constructor so it's illegal
569 RELEASER( const RELEASER& aOther ) : plugin( nullptr ) {}
570
571 public:
572 RELEASER( PLUGIN* aPlugin = nullptr ) :
573 plugin( aPlugin )
574 {
575 }
576
578 {
579 if( plugin )
580 release();
581 }
582
583 void release()
584 {
586 plugin = nullptr;
587 }
588
589 void set( PLUGIN* aPlugin )
590 {
591 if( plugin )
592 release();
593 plugin = aPlugin;
594 }
595
596 operator PLUGIN* () const
597 {
598 return plugin;
599 }
600
602 {
603 return plugin;
604 }
605 };
606#endif
607};
608
609#endif // IO_MGR_H_
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:270
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 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 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 BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=nullptr, const STRING_UTF8_MAP *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 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
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *aProperties=nullptr)
Write either a full aBoard to a storage file in a format that this implementation knows about,...
Definition: io_mgr.cpp:178
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:562
RELEASER(const RELEASER &aOther)
Definition: io_mgr.h:569
PLUGIN * plugin
Definition: io_mgr.h:563
RELEASER & operator=(RELEASER &aOther)
Definition: io_mgr.h:566
RELEASER(PLUGIN *aPlugin=nullptr)
Definition: io_mgr.h:572
void release()
Definition: io_mgr.h:583
PLUGIN * operator->() const
Definition: io_mgr.h:601
void set(PLUGIN *aPlugin)
Definition: io_mgr.h:589
A base class that BOARD loading and saving plugins should derive from.
Definition: io_mgr.h:270
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 const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
Definition: plugin.cpp:70
virtual void FootprintLibCreate(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty footprint library at aLibraryPath empty.
Definition: plugin.cpp:111
virtual bool FootprintLibDelete(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Delete an existing footprint library and returns true, or if library does not exist returns false,...
Definition: plugin.cpp:118
virtual void Save(const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *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:50
virtual BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *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:38
virtual void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const STRING_UTF8_MAP *aProperties=nullptr)
Write aFootprint to an existing library located at aLibraryPath.
Definition: plugin.cpp:95
virtual std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints()
Return a container with the cached library footprints generated in the last call to Load.
Definition: plugin.cpp:45
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
Delete aFootprintName from the library at aLibraryPath.
Definition: plugin.cpp:103
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
Check for the existence of a footprint.
Definition: plugin.cpp:79
virtual FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr)
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
Definition: plugin.cpp:87
virtual bool IsFootprintLibWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: plugin.cpp:125
virtual const wxString PluginName() const =0
Return a brief hard coded name for this PLUGIN.
virtual const wxString GetFileExtension() const =0
Returns the file extension for the PLUGIN.
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 FootprintLibOptions(STRING_UTF8_MAP *aListToAppendTo) const
Append supported PLUGIN options to aListToAppenTo along with internationalized descriptions.
Definition: plugin.cpp:132
virtual void PrefetchLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
If possible, prefetches the specified library (e.g.
Definition: plugin.cpp:65
virtual ~PLUGIN()
Definition: io_mgr.h:553
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr)
Return a list of footprint names contained within the library at aLibraryPath.
Definition: plugin.cpp:57
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:64
A name/value tuple with unique names and optional values.
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