#pragma once #include "stream.h" #include #include class TCompressionCodecFactory { public: using TDecoderConstructor = std::function(IInputStream*)>; using TEncoderConstructor = std::function(IOutputStream*)>; TCompressionCodecFactory(); static inline TCompressionCodecFactory& Instance() noexcept { return *SingletonWithPriority(); } inline const TDecoderConstructor* FindDecoder(TStringBuf name) const { if (auto codec = Codecs_.FindPtr(name)) { return &codec->Decoder; } return nullptr; } inline const TEncoderConstructor* FindEncoder(TStringBuf name) const { if (auto codec = Codecs_.FindPtr(name)) { return &codec->Encoder; } return nullptr; } inline TArrayRef GetBestCodecs() const { return BestCodecs_; } private: void Add(TStringBuf name, TDecoderConstructor d, TEncoderConstructor e); struct TCodec { TDecoderConstructor Decoder; TEncoderConstructor Encoder; }; TDeque Strings_; THashMap Codecs_; TVector BestCodecs_; }; namespace NHttp { template TString ChooseBestCompressionScheme(F accepted, TArrayRef available) { if (available.empty()) { return "identity"; } if (accepted("*")) { return TString(available[0]); } for (const auto& coding : available) { TString s(coding); if (accepted(s)) { return s; } } return "identity"; } }