CompressionRegistrarImpl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <AzCore/std/containers/vector.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <Compression/CompressionInterfaceAPI.h>
  12. namespace Compression
  13. {
  14. class CompressionRegistrarImpl final
  15. : public CompressionRegistrarInterface
  16. {
  17. public:
  18. AZ_TYPE_INFO_WITH_NAME_DECL(CompressionRegistrarImpl);
  19. AZ_RTTI_NO_TYPE_INFO_DECL();
  20. AZ_CLASS_ALLOCATOR_DECL;
  21. CompressionRegistrarImpl();
  22. ~CompressionRegistrarImpl();
  23. void VisitCompressionInterfaces(const VisitCompressionInterfaceCallback&) const override;
  24. //! Registers a compression interface with a standard deleter
  25. AZ::Outcome<void, AZStd::unique_ptr<ICompressionInterface>> RegisterCompressionInterface(
  26. CompressionAlgorithmId algorithmId,
  27. AZStd::unique_ptr<ICompressionInterface> compressionInterface) override;
  28. //! Registers a compression interface with a null deleter
  29. bool RegisterCompressionInterface(CompressionAlgorithmId algorithmId, ICompressionInterface& compressionInterface) override;
  30. bool UnregisterCompressionInterface(CompressionAlgorithmId algorithmId) override;
  31. [[nodiscard]] ICompressionInterface* FindCompressionInterface(CompressionAlgorithmId algorithmId) const override;
  32. [[nodiscard]] ICompressionInterface* FindCompressionInterface(AZStd::string_view algorithmName) const override;
  33. [[nodiscard]] bool IsRegistered(CompressionAlgorithmId algorithmId) const override;
  34. struct CompressionInterfaceDeleter
  35. {
  36. CompressionInterfaceDeleter();
  37. CompressionInterfaceDeleter(bool shouldDelete);
  38. void operator()(ICompressionInterface* ptr) const;
  39. bool m_delete{ true };
  40. };
  41. private:
  42. using CompressionInterfacePtr = AZStd::unique_ptr<ICompressionInterface, CompressionInterfaceDeleter>;
  43. //! Helper function that is used to register a compression interface into the compression interface array
  44. //! while taking into account whether the compression interface should be owned by this registrar
  45. //! @param compressionAlgorithmId Unique Id of the compression interface to register with this registrar
  46. //! @param compression unique_ptr to compression interface to register
  47. //! @return outcome which indicates whether the compression interface was registered with the compression interface array
  48. //! On success an empty Success outcome is returned
  49. //! On failure, the supplied compression interface parameter is returned back to the caller
  50. [[nodiscard]] AZ::Outcome<void, CompressionInterfacePtr> RegisterCompressionInterfaceImpl(
  51. CompressionAlgorithmId algorithmId, CompressionInterfacePtr compressionInterface);
  52. struct CompressionIdIndexEntry
  53. {
  54. CompressionAlgorithmId m_id;
  55. CompressionInterfacePtr m_compressionInterface;
  56. };
  57. using IdToCompressionInterfaceMap = AZStd::vector<CompressionIdIndexEntry>;
  58. //! Searches within the compression interface array for the compression interface registered with the specified id
  59. //! @param compressionAlgorithmId Unique Id of compression interface to locate
  60. //! @return iterator pointing the compression interface registered with the specified CompressionAlgorithmId
  61. //! NOTE: It is responsibility of the caller to lock the compression interface mutex to protect the search
  62. typename IdToCompressionInterfaceMap::const_iterator FindCompressionInterfaceImpl(CompressionAlgorithmId compressionAlgorithmId) const;
  63. //! Contains the registered compression interfaces
  64. //! Sorted to provide O(Log N) search
  65. IdToCompressionInterfaceMap m_compressionInterfaces;
  66. //! Protects modifications to the compression interfaces container
  67. mutable AZStd::mutex m_compressionInterfaceMutex;
  68. };
  69. }// namespace Compression