aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/misc/configurable_singleton_def.h
blob: 8fe97e25d5623d6b1d15cb7bc9614c24be3b20c1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once

#include <yt/yt/core/ytree/yson_struct.h>

#include <any>
#include <typeindex>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

struct TSingletonConfigHelpers;
class TSingletonManagerImpl;

template <bool Static>
class TSingletonsConfigBase
{
public:
    template <class TConfig>
    TIntrusivePtr<TConfig> TryGetSingletonConfig();

    template <class TConfig>
    TIntrusivePtr<TConfig> GetSingletonConfig();

    template <class TConfig>
    void SetSingletonConfig(TIntrusivePtr<TConfig> config);

protected:
    static void RegisterSingletons(
        auto&& registrar,
        auto&& registerFieldSelector);

private:
    friend struct NYT::NDetail::TSingletonConfigHelpers;
    friend class NYT::NDetail::TSingletonManagerImpl;

    THashMap<std::string, std::any> NameToConfig_;
    THashMap<std::type_index, std::any*> TypeToConfig_;
};

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

struct TSingletonsConfig
    : public NDetail::TSingletonsConfigBase<true>
    , public virtual NYTree::TYsonStruct
{
    REGISTER_YSON_STRUCT(TSingletonsConfig);

    static void Register(TRegistrar registrar);
};

DEFINE_REFCOUNTED_TYPE(TSingletonsConfig);

////////////////////////////////////////////////////////////////////////////////

struct TSingletonsDynamicConfig
    : public NDetail::TSingletonsConfigBase<false>
    , public virtual NYTree::TYsonStruct
{
    REGISTER_YSON_STRUCT(TSingletonsDynamicConfig);

    static void Register(TRegistrar registrar);
};

DEFINE_REFCOUNTED_TYPE(TSingletonsDynamicConfig);

////////////////////////////////////////////////////////////////////////////////

#define YT_DEFINE_CONFIGURABLE_SINGLETON(singletonName, configType)
#define YT_DEFINE_RECONFIGURABLE_SINGLETON(singletonName, configType, dynamicConfigType)

////////////////////////////////////////////////////////////////////////////////

class TSingletonManager
{
public:
    static void Configure(const TSingletonsConfigPtr& config);
    static void Reconfigure(const TSingletonsDynamicConfigPtr& dynamicConfig);

    static TSingletonsConfigPtr GetConfig();
    static TSingletonsDynamicConfigPtr GetDynamicConfig();
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define CONFIGURABLE_SINGLETON_DEF_INL_H_
#include "configurable_singleton_def-inl.h"
#undef CONFIGURABLE_SINGLETON_DEF_INL_H_