aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/codecs/codecs_registry.h
blob: 1c82365d8fa950d6d14f85d26af5389696d99c1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include "codecs.h"
#include <util/string/cast.h>

namespace NCodecs {
    struct TNoCodecException : TCodecException {
        TNoCodecException(TStringBuf name) {
            (*this) << "unknown codec: " << name;
        }
    };

    struct ICodecFactory : TAtomicRefCount<ICodecFactory> {
        virtual ~ICodecFactory() = default;
        virtual TCodecPtr MakeCodec(TStringBuf name) const = 0;
        virtual TVector<TString> ListNames() const = 0; 
    };

    typedef TIntrusivePtr<ICodecFactory> TCodecFactoryPtr;

    namespace NPrivate {
        template <typename TCodec>
        struct TInstanceFactory : ICodecFactory {
            TCodecPtr MakeCodec(TStringBuf) const override {
                return new TCodec;
            }

            TVector<TString> ListNames() const override { 
                TVector<TString> vs; 
                vs.push_back(ToString(TCodec::MyName()));
                return vs;
            }
        };

        class TCodecRegistry {
            using TRegistry = THashMap<TString, TIntrusivePtr<ICodecFactory>>; 
            TRegistry Registry;

        public:
            using TFactoryPtr = TIntrusivePtr<ICodecFactory>;

            TCodecRegistry();

            void RegisterFactory(TFactoryPtr fac);

            TCodecPtr GetCodec(TStringBuf name) const;

            TVector<TString> GetCodecsList() const; 
        };

    }

    void RegisterCodecFactory(TCodecFactoryPtr fact);

    template <typename TCodec>
    void RegisterCodec() {
        RegisterCodecFactory(new NPrivate::TInstanceFactory<TCodec>());
    }

}