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 class BOARD;
34 class PLUGIN;
35 class MODULE;
36 class PROPERTIES;
37 
38 
43 class IO_MGR
44 {
45 public:
46 
53  {
60 
61  // add your type here.
62 
63  // ALTIUM,
64  // etc.
65 
67  };
68 
75  {
76  public:
77  struct ENTRY
78  {
80  std::function<PLUGIN*(void)> m_createFunc;
81  wxString m_name;
82  };
83 
85  {
86  static PLUGIN_REGISTRY *self = nullptr;
87 
88  if( !self )
89  {
90  self = new PLUGIN_REGISTRY;
91  }
92  return self;
93  }
94 
95  void Register( PCB_FILE_T aType, const wxString& aName, std::function<PLUGIN*(void)> aCreateFunc )
96  {
97  ENTRY ent;
98  ent.m_type = aType;
99  ent.m_createFunc = aCreateFunc;
100  ent.m_name = aName;
101  m_plugins.push_back( ent );
102  }
103 
104  PLUGIN* Create( PCB_FILE_T aFileType ) const
105  {
106  for( auto& ent : m_plugins )
107  {
108  if ( ent.m_type == aFileType )
109  {
110  return ent.m_createFunc();
111  }
112  }
113  return nullptr;
114  }
115 
116  const std::vector<ENTRY>& AllPlugins() const
117  {
118  return m_plugins;
119  }
120 
121  private:
122  std::vector<ENTRY> m_plugins;
123  };
124 
133  {
134  REGISTER_PLUGIN( PCB_FILE_T aType, const wxString& aName, std::function<PLUGIN*(void)> aCreateFunc )
135  {
136  PLUGIN_REGISTRY::Instance()->Register( aType, aName, aCreateFunc );
137  }
138  };
139 
140 
153  static PLUGIN* PluginFind( PCB_FILE_T aFileType );
154 
162  static void PluginRelease( PLUGIN* aPlugin );
163 
168  static const wxString ShowType( PCB_FILE_T aFileType );
169 
174  static PCB_FILE_T EnumFromStr( const wxString& aFileType );
175 
184  static const wxString GetFileExtension( PCB_FILE_T aFileType );
185 
190  static PCB_FILE_T GuessPluginTypeFromLibPath( const wxString& aLibPath );
191 
213  static BOARD* Load( PCB_FILE_T aFileType, const wxString& aFileName,
214  BOARD* aAppendToMe = NULL, const PROPERTIES* aProperties = NULL );
215 
239  static void Save( PCB_FILE_T aFileType, const wxString& aFileName,
240  BOARD* aBoard, const PROPERTIES* aProperties = NULL );
241 };
242 
243 
266 class PLUGIN
267 {
268 public:
269 
270  //-----<PUBLIC PLUGIN API>-------------------------------------------------
271 
276  virtual const wxString PluginName() const = 0;
277 
282  virtual const wxString GetFileExtension() const = 0;
283 
310  virtual BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
311  const PROPERTIES* aProperties = NULL );
312 
333  virtual void Save( const wxString& aFileName, BOARD* aBoard,
334  const PROPERTIES* aProperties = NULL );
335 
336  //-----<Footprint Stuff>-----------------------------
337 
353  virtual void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
354  const PROPERTIES* aProperties = NULL );
355 
361  virtual long long GetLibraryTimestamp( const wxString& aLibraryPath ) const = 0;
362 
382  virtual void PrefetchLib( const wxString& aLibraryPath,
383  const PROPERTIES* aProperties = NULL );
384 
406  virtual MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
407  const PROPERTIES* aProperties = NULL );
408 
414  virtual const MODULE* GetEnumeratedFootprint( const wxString& aLibraryPath,
415  const wxString& aFootprintName,
416  const PROPERTIES* aProperties = NULL );
417 
437  virtual void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
438  const PROPERTIES* aProperties = NULL );
439 
457  virtual void FootprintDelete( const wxString& aLibraryPath,
458  const wxString& aFootprintName, const PROPERTIES* aProperties = NULL );
459 
477  virtual void FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
478 
498  virtual bool FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
499 
510  virtual bool IsFootprintLibWritable( const wxString& aLibraryPath );
511 
538  virtual void FootprintLibOptions( PROPERTIES* aListToAppendTo ) const;
539 
540  //-----</PUBLIC PLUGIN API>------------------------------------------------
541 
542 
543  /* The compiler writes the "zero argument" constructor for a PLUGIN
544  automatically if you do not provide one. If you decide you need to
545  provide a zero argument constructor of your own design, that is allowed.
546  It must be public, and it is what the IO_MGR uses. Parameters may be
547  passed into a PLUGIN via the PROPERTIES variable for any of the public
548  API functions which take one.
549  */
550 
551  virtual ~PLUGIN()
552  {
553  //printf( "~%s", __func__ );
554  };
555 
556 
557 #ifndef SWIG
558 
563  class RELEASER
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 ) {}
572 
573  public:
574  RELEASER( PLUGIN* aPlugin = NULL ) :
575  plugin( aPlugin )
576  {
577  }
578 
580  {
581  if( plugin )
582  release();
583  }
584 
585  void release()
586  {
587  IO_MGR::PluginRelease( plugin );
588  plugin = NULL;
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 
603  PLUGIN* operator -> () const
604  {
605  return plugin;
606  }
607  };
608 #endif
609 };
610 
611 #endif // IO_MGR_H_
void release()
Definition: io_mgr.h:585
RELEASER(PLUGIN *aPlugin=NULL)
Definition: io_mgr.h:574
RELEASER(const RELEASER &aOther)
Definition: io_mgr.h:571
const std::vector< ENTRY > & AllPlugins() const
Definition: io_mgr.h:116
static const wxString ShowType(PCB_FILE_T aFileType)
Function ShowType returns a brief name for a plugin, given aFileType enum.
Definition: io_mgr.cpp:77
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
Geda PCB file formats.
Definition: io_mgr.h:58
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Function GuessPluginTypeFromLibPath returns a plugin type given a footprint library&#39;s libPath...
Definition: io_mgr.cpp:124
Class PLUGIN_REGISTRY Holds a list of available plugins, created using a singleton REGISTER_PLUGIN ob...
Definition: io_mgr.h:74
Class RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destruct...
Definition: io_mgr.h:563
Class IO_MGR is a factory which returns an instance of a PLUGIN.
Definition: io_mgr.h:43
std::vector< ENTRY > m_plugins
Definition: io_mgr.h:122
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:93
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:171
RELEASER & operator=(RELEASER &aOther)
Definition: io_mgr.h:568
std::function< PLUGIN *(void)> m_createFunc
Definition: io_mgr.h:80
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:186
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:67
static PLUGIN_REGISTRY * Instance()
Definition: io_mgr.h:84
PLUGIN * plugin
Definition: io_mgr.h:565
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
Class PLUGIN is a base class that BOARD loading and saving plugins should derive from.
Definition: io_mgr.h:266
PLUGIN * Create(PCB_FILE_T aFileType) const
Definition: io_mgr.h:104
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:52
virtual ~PLUGIN()
Definition: io_mgr.h:551
Read only http://github.com repo holding pretty footprints.
Definition: io_mgr.h:59
Class REGISTER_PLUGIN Registers a plugin.
Definition: io_mgr.h:132
void Register(PCB_FILE_T aType, const wxString &aName, std::function< PLUGIN *(void)> aCreateFunc)
Definition: io_mgr.h:95
S-expression Pcbnew file format.
Definition: io_mgr.h:55
static const wxString GetFileExtension(PCB_FILE_T aFileType)
Function GetFileExtension returns the file extension for aFileType.
Definition: io_mgr.cpp:109