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.

139 lines
5.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef PCBNEW_COMPONENT_CLASS_MANAGER_H
  20. #define PCBNEW_COMPONENT_CLASS_MANAGER_H
  21. #include <memory>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include <wx/string.h>
  25. #include <board_item.h>
  26. /*
  27. * A lightweight representation of a component class. The membership within
  28. * m_consituentClasses allows determination of the type of class this is:
  29. *
  30. * m_constituentClasses.size() == 0: This is a null class (no assigment).
  31. * m_name is empty.
  32. * m_constituentClasses.size() == 1: This is an atomic class. The constituent class
  33. * pointer refers to itself. m_name contains the name of the atomic class
  34. * m_constituentClasses.size() > 1: This is a composite class. The constituent class
  35. * pointers refer to all atomic members. m_name contains a comma-delimited list of
  36. * all atomic member class names.
  37. */
  38. class COMPONENT_CLASS
  39. {
  40. public:
  41. COMPONENT_CLASS( const wxString& name ) : m_name( name ) {}
  42. /// Fetches the display name of this component class
  43. wxString GetName() const;
  44. /// Fetches the full name of this component class
  45. const wxString& GetFullName() const { return m_name; }
  46. /// Adds a constituent component class to an effective component class
  47. void AddConstituentClass( COMPONENT_CLASS* componentClass );
  48. /// Determines if this (effective) component class contains a specific sub-class
  49. bool ContainsClassName( const wxString& className ) const;
  50. /// Determines if this (effective) component class is empty (i.e. no classes defined)
  51. bool IsEmpty() const;
  52. /// Fetches a vector of the constituent classes for this (effective) class
  53. const std::vector<COMPONENT_CLASS*>& GetConstituentClasses() const
  54. {
  55. return m_constituentClasses;
  56. }
  57. private:
  58. /// The full name of the component class
  59. wxString m_name;
  60. /// The COMPONENT_CLASS objects contributing to this complete component class
  61. std::vector<COMPONENT_CLASS*> m_constituentClasses;
  62. };
  63. /*
  64. * A class to manage Component Classes in a board context
  65. *
  66. * This manager owns generated COMPONENT_CLASS objects, and guarantees that pointers to managed
  67. * objects are valid for the duration of the board lifetime. Note that, in order to maintain this
  68. * guarantee, there are two methods that must be called when updating the board from the netlist
  69. * (InitNetlistUpdate and FinishNetlistUpdate).
  70. */
  71. class COMPONENT_CLASS_MANAGER
  72. {
  73. public:
  74. COMPONENT_CLASS_MANAGER();
  75. /// @brief Gets the full effective class name for the given set of constituent classes
  76. static wxString GetFullClassNameForConstituents( std::unordered_set<wxString>& classNames );
  77. /// @brief Gets the full effective class name for the given set of constituent classes
  78. /// @param classNames a sorted vector of consituent class names
  79. static wxString GetFullClassNameForConstituents( std::vector<wxString>& classNames );
  80. /// @brief Gets an effective component class for the given constituent class names
  81. /// @param classes The names of the constituent component classes
  82. /// @return Effective COMPONENT_CLASS object
  83. COMPONENT_CLASS* GetEffectiveComponentClass( std::unordered_set<wxString>& classNames );
  84. /// Returns the unassigned component class
  85. const COMPONENT_CLASS* GetNoneComponentClass() const { return m_noneClass.get(); }
  86. /// Prepare the manager for a board update
  87. /// Must be called prior to updating the PCB from the netlist
  88. void InitNetlistUpdate();
  89. /// Cleans up the manager after a board update
  90. /// Must be called after updating the PCB from the netlist
  91. void FinishNetlistUpdate();
  92. /// Resets the contents of the manager
  93. // All pointers to COMPONENT_CLASS objects will being invalid
  94. void Reset();
  95. /// @brief Fetches a read-only map of the fundamental component classes
  96. const std::unordered_map<wxString, std::unique_ptr<COMPONENT_CLASS>>& GetClasses() const
  97. {
  98. return m_classes;
  99. }
  100. protected:
  101. /// All individual component classes
  102. std::unordered_map<wxString, std::unique_ptr<COMPONENT_CLASS>> m_classes;
  103. /// Generated effective component classes
  104. std::unordered_map<wxString, std::unique_ptr<COMPONENT_CLASS>> m_effectiveClasses;
  105. /// Cache of all individual component classes (for netlist updating)
  106. std::unordered_map<wxString, std::unique_ptr<COMPONENT_CLASS>> m_classesCache;
  107. /// Cache of all generated effective component classes (for netlist updating)
  108. std::unordered_map<wxString, std::unique_ptr<COMPONENT_CLASS>> m_effectiveClassesCache;
  109. /// The class to represent an unassigned component class
  110. std::unique_ptr<COMPONENT_CLASS> m_noneClass;
  111. };
  112. #endif