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 
359  virtual wxDateTime GetLibModificationTime( const wxString& aLibraryPath ) const
360  {
361  // Default implementation.
362  return wxDateTime::Now(); // If we don't know then we must assume the worst.
363  }
364 
384  virtual void PrefetchLib( const wxString& aLibraryPath,
385  const PROPERTIES* aProperties = NULL );
386 
408  virtual MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
409  const PROPERTIES* aProperties = NULL );
410 
430  virtual void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
431  const PROPERTIES* aProperties = NULL );
432 
450  virtual void FootprintDelete( const wxString& aLibraryPath,
451  const wxString& aFootprintName, const PROPERTIES* aProperties = NULL );
452 
470  virtual void FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
471 
491  virtual bool FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
492 
503  virtual bool IsFootprintLibWritable( const wxString& aLibraryPath );
504 
531  virtual void FootprintLibOptions( PROPERTIES* aListToAppendTo ) const;
532 
533  //-----</PUBLIC PLUGIN API>------------------------------------------------
534 
535 
536  /* The compiler writes the "zero argument" constructor for a PLUGIN
537  automatically if you do not provide one. If you decide you need to
538  provide a zero argument constructor of your own design, that is allowed.
539  It must be public, and it is what the IO_MGR uses. Parameters may be
540  passed into a PLUGIN via the PROPERTIES variable for any of the public
541  API functions which take one.
542  */
543 
544  virtual ~PLUGIN()
545  {
546  //printf( "~%s", __func__ );
547  };
548 
549 
550 #ifndef SWIG
551 
556  class RELEASER
557  {
559 
560  // private assignment operator so it's illegal
561  RELEASER& operator=( RELEASER& aOther ) { return *this; }
562 
563  // private copy constructor so it's illegal
564  RELEASER( const RELEASER& aOther ) {}
565 
566  public:
567  RELEASER( PLUGIN* aPlugin = NULL ) :
568  plugin( aPlugin )
569  {
570  }
571 
573  {
574  if( plugin )
575  release();
576  }
577 
578  void release()
579  {
580  IO_MGR::PluginRelease( plugin );
581  plugin = NULL;
582  }
583 
584  void set( PLUGIN* aPlugin )
585  {
586  if( plugin )
587  release();
588  plugin = aPlugin;
589  }
590 
591  operator PLUGIN* () const
592  {
593  return plugin;
594  }
595 
596  PLUGIN* operator -> () const
597  {
598  return plugin;
599  }
600  };
601 #endif
602 };
603 
604 #endif // IO_MGR_H_
void release()
Definition: io_mgr.h:578
RELEASER(PLUGIN *aPlugin=NULL)
Definition: io_mgr.h:567
RELEASER(const RELEASER &aOther)
Definition: io_mgr.h:564
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:132
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:556
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:97
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:179
RELEASER & operator=(RELEASER &aOther)
Definition: io_mgr.h:561
virtual wxDateTime GetLibModificationTime(const wxString &aLibraryPath) const
Return the footprint lib last-mod-time, if available.
Definition: io_mgr.h:359
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:194
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:558
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:169
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:544
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:117