codecs_registry.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include "codecs.h"
  3. #include <util/string/cast.h>
  4. namespace NCodecs {
  5. struct TNoCodecException : TCodecException {
  6. TNoCodecException(TStringBuf name) {
  7. (*this) << "unknown codec: " << name;
  8. }
  9. };
  10. struct ICodecFactory : TAtomicRefCount<ICodecFactory> {
  11. virtual ~ICodecFactory() = default;
  12. virtual TCodecPtr MakeCodec(TStringBuf name) const = 0;
  13. virtual TVector<TString> ListNames() const = 0;
  14. };
  15. typedef TIntrusivePtr<ICodecFactory> TCodecFactoryPtr;
  16. namespace NPrivate {
  17. template <typename TCodec>
  18. struct TInstanceFactory : ICodecFactory {
  19. TCodecPtr MakeCodec(TStringBuf) const override {
  20. return new TCodec;
  21. }
  22. TVector<TString> ListNames() const override {
  23. TVector<TString> vs;
  24. vs.push_back(ToString(TCodec::MyName()));
  25. return vs;
  26. }
  27. };
  28. class TCodecRegistry {
  29. using TRegistry = THashMap<TString, TIntrusivePtr<ICodecFactory>>;
  30. TRegistry Registry;
  31. public:
  32. using TFactoryPtr = TIntrusivePtr<ICodecFactory>;
  33. TCodecRegistry();
  34. void RegisterFactory(TFactoryPtr fac);
  35. TCodecPtr GetCodec(TStringBuf name) const;
  36. TVector<TString> GetCodecsList() const;
  37. };
  38. }
  39. void RegisterCodecFactory(TCodecFactoryPtr fact);
  40. template <typename TCodec>
  41. void RegisterCodec() {
  42. RegisterCodecFactory(new NPrivate::TInstanceFactory<TCodec>());
  43. }
  44. }