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 <dick@softplc.com>
8  * Copyright (C) 2016-2017 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 
35 class BOARD;
36 class PLUGIN;
37 class MODULE;
38 class PROPERTIES;
39 
40 
45 class IO_MGR
46 {
47 public:
48 
55  {
65  // add your type here.
66 
67  // etc.
68 
70  };
71 
78  {
79  public:
80  struct ENTRY
81  {
83  std::function<PLUGIN*(void)> m_createFunc;
84  wxString m_name;
85  };
86 
88  {
89  static PLUGIN_REGISTRY *self = nullptr;
90 
91  if( !self )
92  {
93  self = new PLUGIN_REGISTRY;
94  }
95  return self;
96  }
97 
98  void Register( PCB_FILE_T aType, const wxString& aName, 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  return nullptr;
117  }
118 
119  const std::vector<ENTRY>& AllPlugins() const
120  {
121  return m_plugins;
122  }
123 
124  private:
125  std::vector<ENTRY> m_plugins;
126  };
127 
136  {
137  REGISTER_PLUGIN( PCB_FILE_T aType, const wxString& aName, std::function<PLUGIN*(void)> aCreateFunc )
138  {
139  PLUGIN_REGISTRY::Instance()->Register( aType, aName, aCreateFunc );
140  }
141  };
142 
143 
156  static PLUGIN* PluginFind( PCB_FILE_T aFileType );
157 
165  static void PluginRelease( PLUGIN* aPlugin );
166 
171  static const wxString ShowType( PCB_FILE_T aFileType );
172 
177  static PCB_FILE_T EnumFromStr( const wxString& aFileType );
178 
187  static const wxString GetFileExtension( PCB_FILE_T aFileType );
188 
193  static PCB_FILE_T GuessPluginTypeFromLibPath( const wxString& aLibPath );
194 
216  static BOARD* Load( PCB_FILE_T aFileType, const wxString& aFileName,
217  BOARD* aAppendToMe = NULL, const PROPERTIES* aProperties = NULL );
218 
242  static void Save( PCB_FILE_T aFileType, const wxString& aFileName,
243  BOARD* aBoard, const PROPERTIES* aProperties = NULL );
244 };
245 
246 
269 class PLUGIN
270 {
271 public:
272 
273  //-----<PUBLIC PLUGIN API>-------------------------------------------------
274 
279  virtual const wxString PluginName() const = 0;
280 
285  virtual const wxString GetFileExtension() const = 0;
286 
313  virtual BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
314  const PROPERTIES* aProperties = NULL );
315 
336  virtual void Save( const wxString& aFileName, BOARD* aBoard,
337  const PROPERTIES* aProperties = NULL );
338 
339  //-----<Footprint Stuff>-----------------------------
340 
358  virtual void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
359  bool aBestEfforts, const PROPERTIES* aProperties = NULL );
360 
366  virtual long long GetLibraryTimestamp( const wxString& aLibraryPath ) const = 0;
367 
387  virtual void PrefetchLib( const wxString& aLibraryPath,
388  const PROPERTIES* aProperties = NULL );
389 
411  virtual MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
412  const PROPERTIES* aProperties = NULL );
413 
419  virtual const MODULE* GetEnumeratedFootprint( const wxString& aLibraryPath,
420  const wxString& aFootprintName,
421  const PROPERTIES* aProperties = NULL );
422 
427  virtual bool FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
428  const PROPERTIES* aProperties = NULL );
429 
449  virtual void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
450  const PROPERTIES* aProperties = NULL );
451 
469  virtual void FootprintDelete( const wxString& aLibraryPath,
470  const wxString& aFootprintName, const PROPERTIES* aProperties = NULL );
471 
489  virtual void FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
490 
510  virtual bool FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
511 
522  virtual bool IsFootprintLibWritable( const wxString& aLibraryPath );
523 
550  virtual void FootprintLibOptions( PROPERTIES* aListToAppendTo ) const;
551 
552  //-----</PUBLIC PLUGIN API>------------------------------------------------
553 
554 
555  /* The compiler writes the "zero argument" constructor for a PLUGIN
556  automatically if you do not provide one. If you decide you need to
557  provide a zero argument constructor of your own design, that is allowed.
558  It must be public, and it is what the IO_MGR uses. Parameters may be
559  passed into a PLUGIN via the PROPERTIES variable for any of the public
560  API functions which take one.
561  */
562 
563  virtual ~PLUGIN()
564  {
565  //printf( "~%s", __func__ );
566  };
567 
568 
569 #ifndef SWIG
570 
575  class RELEASER
576  {
578 
579  // private assignment operator so it's illegal
580  RELEASER& operator=( RELEASER& aOther ) { return *this; }
581 
582  // private copy constructor so it's illegal
583  RELEASER( const RELEASER& aOther ) : plugin( nullptr ) {}
584 
585  public:
586  RELEASER( PLUGIN* aPlugin = nullptr ) :
587  plugin( aPlugin )
588  {
589  }
590 
592  {
593  if( plugin )
594  release();
595  }
596 
597  void release()
598  {
600  plugin = nullptr;
601  }
602 
603  void set( PLUGIN* aPlugin )
604  {
605  if( plugin )
606  release();
607  plugin = aPlugin;
608  }
609 
610  operator PLUGIN* () const
611  {
612  return plugin;
613  }
614 
616  {
617  return plugin;
618  }
619  };
620 #endif
621 };
622 
623 #endif // IO_MGR_H_
void release()
Definition: io_mgr.h:597
PLUGIN * Create(PCB_FILE_T aFileType) const
Definition: io_mgr.h:107
virtual void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL)
Function Save will write aBoard to a storage file in a format that this PLUGIN implementation knows a...
Definition: plugin.cpp:54
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 PrefetchLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Function PrefetchLib If possible, prefetches the specified library (e.g.
Definition: plugin.cpp:69
RELEASER(const RELEASER &aOther)
Definition: io_mgr.h:583
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const PROPERTIES *aProperties=NULL)
Return a list of footprint names contained within the library at aLibraryPath.
Definition: plugin.cpp:61
virtual void FootprintSave(const wxString &aLibraryPath, const MODULE *aFootprint, const PROPERTIES *aProperties=NULL)
Function FootprintSave will write aModule to an existing library located at aLibraryPath.
Definition: plugin.cpp:102
virtual const MODULE * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL)
Function GetEnumeratedFootprint a version of FootprintLoad() for use after FootprintEnumerate() for m...
Definition: plugin.cpp:76
virtual void FootprintLibOptions(PROPERTIES *aListToAppendTo) const
Function FootprintLibOptions appends supported PLUGIN options to aListToAppenTo along with internatio...
Definition: plugin.cpp:141
static const wxString ShowType(PCB_FILE_T aFileType)
Function ShowType returns a brief name for a plugin, given aFileType enum.
Definition: io_mgr.cpp:76
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
PLUGIN * operator ->() const
Definition: io_mgr.h:615
Geda PCB file formats.
Definition: io_mgr.h:64
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Function GuessPluginTypeFromLibPath returns a plugin type given a footprint library's libPath.
Definition: io_mgr.cpp:123
PLUGIN_REGISTRY Holds a list of available plugins, created using a singleton REGISTER_PLUGIN object.
Definition: io_mgr.h:77
virtual bool IsFootprintLibWritable(const wxString &aLibraryPath)
Function IsFootprintLibWritable returns true iff the library at aLibraryPath is writable.
Definition: plugin.cpp:133
RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destructor.
Definition: io_mgr.h:575
IO_MGR is a factory which returns an instance of a PLUGIN.
Definition: io_mgr.h:45
std::vector< ENTRY > m_plugins
Definition: io_mgr.h:125
virtual MODULE * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL)
Function FootprintLoad loads a footprint having aFootprintName from the aLibraryPath containing a lib...
Definition: plugin.cpp:93
#define NULL
RELEASER(PLUGIN *aPlugin=nullptr)
Definition: io_mgr.h:586
virtual bool FootprintLibDelete(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Function FootprintLibDelete deletes an existing footprint library and returns true,...
Definition: plugin.cpp:125
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Function EnumFromStr returns the PCB_FILE_T from the corresponding plugin type name: "kicad",...
Definition: io_mgr.cpp:92
static BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=NULL, const PROPERTIES *aProperties=NULL)
Function Load finds the requested PLUGIN and if found, calls the PLUGIN->Load(..) funtion on it using...
Definition: io_mgr.cpp:157
RELEASER & operator=(RELEASER &aOther)
Definition: io_mgr.h:580
void set(PLUGIN *aPlugin)
Definition: io_mgr.h:603
virtual const wxString PluginName() const =0
Function PluginName returns a brief hard coded name for this PLUGIN.
std::function< PLUGIN *(void)> m_createFunc
Definition: io_mgr.h:83
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL)
Function FootprintExists check for the existence of a footprint.
Definition: plugin.cpp:85
virtual const wxString GetFileExtension() const =0
Function GetFileExtension returns the file extension for the PLUGIN.
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL)
Function Save will write either a full aBoard to a storage file in a format that this implementation ...
Definition: io_mgr.cpp:172
static void PluginRelease(PLUGIN *aPlugin)
Function PluginRelease releases a PLUGIN back to the system, and may cause it to be unloaded from mem...
Definition: io_mgr.cpp:66
static PLUGIN_REGISTRY * Instance()
Definition: io_mgr.h:87
PLUGIN * plugin
Definition: io_mgr.h:577
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save,...
Definition: io_mgr.cpp:57
PLUGIN is a base class that BOARD loading and saving plugins should derive from.
Definition: io_mgr.h:269
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL)
Function FootprintDelete deletes aFootprintName from the library at aLibraryPath.
Definition: plugin.cpp:110
virtual void FootprintLibCreate(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Function FootprintLibCreate creates a new empty footprint library at aLibraryPath empty.
Definition: plugin.cpp:118
const std::vector< ENTRY > & AllPlugins() const
Definition: io_mgr.h:119
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:54
virtual ~PLUGIN()
Definition: io_mgr.h:563
REGISTER_PLUGIN Registers a plugin.
Definition: io_mgr.h:135
void Register(PCB_FILE_T aType, const wxString &aName, std::function< PLUGIN *(void)> aCreateFunc)
Definition: io_mgr.h:98
virtual BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=NULL)
Function Load loads information from some input file format that this PLUGIN implementation knows abo...
Definition: plugin.cpp:47
S-expression Pcbnew file format.
Definition: io_mgr.h:57
static const wxString GetFileExtension(PCB_FILE_T aFileType)
Function GetFileExtension returns the file extension for aFileType.
Definition: io_mgr.cpp:108