You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Alex Shvartzkop <dudesuchamazing@gmail.com>
  5. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef GENERATORS_MGR_H_
  25. #define GENERATORS_MGR_H_
  26. #include <cstdint>
  27. #include <config.h>
  28. #include <vector>
  29. #include <map>
  30. #include <memory>
  31. #include <wx/string.h>
  32. class PCB_GENERATOR;
  33. ///< Unique type identifier
  34. using TYPE_ID = size_t;
  35. /**
  36. * A factory which returns an instance of a #PCB_GENERATOR.
  37. */
  38. class GENERATORS_MGR
  39. {
  40. public:
  41. struct ENTRY
  42. {
  43. wxString m_type;
  44. wxString m_displayName;
  45. std::function<PCB_GENERATOR*( void )> m_createFunc;
  46. };
  47. static GENERATORS_MGR& Instance();
  48. /**
  49. * Associate a type string to display name and create function.
  50. *
  51. * @param aTypeStr is the type identifier string.
  52. * @param aName is the display name.
  53. * @param aCreateFunc is the create function.
  54. */
  55. void Register( const wxString& aTypeStr, const wxString& aName,
  56. std::function<PCB_GENERATOR*( void )> aCreateFunc );
  57. PCB_GENERATOR* CreateFromType( const wxString& aTypeStr );
  58. /**
  59. * Static helper to register a generator.
  60. * T must define static members GENERATOR_TYPE and DISPLAY_NAME
  61. */
  62. template <typename T>
  63. struct REGISTER
  64. {
  65. REGISTER()
  66. {
  67. GENERATORS_MGR::Instance().Register( T::GENERATOR_TYPE, T::DISPLAY_NAME,
  68. []
  69. {
  70. return new T;
  71. } );
  72. }
  73. };
  74. private:
  75. std::map<wxString, ENTRY> m_registry;
  76. };
  77. #endif // GENERATORS_MGR_H_