Plugin.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #ifndef CRYINCLUDE_EDITOR_PLUGIN_H
  10. #define CRYINCLUDE_EDITOR_PLUGIN_H
  11. #include "Include/IEditorClassFactory.h"
  12. #include "Util/GuidUtil.h"
  13. #include <map>
  14. //! Class factory is a common repository of all registered plugin classes,
  15. //! Classes here can found by their class ID or all classes of given system class retrieved
  16. class CRYEDIT_API CClassFactory
  17. : public IEditorClassFactory
  18. {
  19. public:
  20. CClassFactory();
  21. ~CClassFactory();
  22. //! Access class factory singleton.
  23. static CClassFactory* Instance();
  24. //! Register a new class to the factory
  25. void RegisterClass(IClassDesc* pClassDesc);
  26. //! Find class in the factory by class name
  27. IClassDesc* FindClass(const char* className) const;
  28. //! Find class in the factory by class ID
  29. IClassDesc* FindClass(const GUID& rClassID) const;
  30. void UnregisterClass(const char* pClassName);
  31. void UnregisterClass(const GUID& rClassID);
  32. //! Get classes matching specific requirements ordered alphabetically by name.
  33. void GetClassesBySystemID(ESystemClassID aSystemClassID, std::vector<IClassDesc*>& rOutClasses);
  34. void GetClassesByCategory(const char* pCategory, std::vector<IClassDesc*>& rOutClasses);
  35. private:
  36. void RegisterAutoTypes();
  37. typedef std::map<QString, IClassDesc*> TNameMap;
  38. typedef std::map<GUID, IClassDesc*, guid_less_predicate> TGuidMap;
  39. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  40. TNameMap m_nameToClass;
  41. TGuidMap m_guidToClass;
  42. std::vector<IClassDesc*> m_classes;
  43. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  44. #if defined(DEBUG_CLASS_NAME_REGISTRATION)
  45. // This vector will mirror that of CClassFactory::m_classes.
  46. // When a class description is destroyed without being unregistered first
  47. // (i.e. from within plugins is a common scenario) you will get a crash
  48. // in ~CClassFactory as it's Releasing m_classes.
  49. // When you hit that crash, take the index and look into this vector at
  50. // the class name. That class did not unregister before it was released.
  51. std::vector<string> m_debugClassNames;
  52. #endif // DEBUG_CLASS_NAME_REGISTRATION
  53. static CClassFactory* s_pInstance;
  54. };
  55. //! Auto registration for classes
  56. class CAutoRegisterClassHelper
  57. {
  58. public:
  59. CAutoRegisterClassHelper(IClassDesc* pClassDesc)
  60. {
  61. m_pClassDesc = pClassDesc;
  62. m_pNext = 0;
  63. if (!s_pLast)
  64. {
  65. s_pFirst = this;
  66. }
  67. else
  68. {
  69. s_pLast->m_pNext = this;
  70. }
  71. s_pLast = this;
  72. }
  73. IClassDesc* m_pClassDesc;
  74. CAutoRegisterClassHelper* m_pNext;
  75. static CAutoRegisterClassHelper* s_pFirst;
  76. static CAutoRegisterClassHelper* s_pLast;
  77. };
  78. // Use this define to automatically register a new class description.
  79. #define REGISTER_CLASS_DESC(ClassDesc) \
  80. CAutoRegisterClassHelper g_AutoRegHelper##ClassDesc(new ClassDesc);
  81. #endif // CRYINCLUDE_EDITOR_PLUGIN_H