aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/utils.h
blob: 1aa5e61bf1a80b163fcac163c2935eea6217ae35 (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
95
#pragma once

#include "api/settings.h"
#include "tool/settings.h"

#include <util/string/cast.h>
#include <util/system/spinlock.h>

#include <optional>

namespace NTvmAuth {
    class TTvmClient;
}

namespace NTvmAuth::NInternal {
    class TClientCaningKnife {
    public:
        static void StartTvmClientStopping(TTvmClient* c);
        static bool IsTvmClientStopped(TTvmClient* c);
    };
}

namespace NTvmAuth::NUtils {
    TString ToHex(const TStringBuf s);

    inline NTvmAuth::NTvmApi::TClientSettings::TDstMap ParseDstMap(TStringBuf dsts) {
        NTvmAuth::NTvmApi::TClientSettings::TDstMap res;

        while (dsts) {
            TStringBuf pair = dsts.NextTok(';');
            TStringBuf alias = pair.NextTok(':');
            res.insert(decltype(res)::value_type(
                alias,
                IntFromString<TTvmId, 10>(pair)));
        }

        return res;
    }

    inline NTvmAuth::NTvmApi::TClientSettings::TDstVector ParseDstVector(TStringBuf dsts) {
        NTvmAuth::NTvmApi::TClientSettings::TDstVector res;

        while (dsts) {
            res.push_back(IntFromString<TTvmId, 10>(dsts.NextTok(';')));
        }

        return res;
    }

    bool CheckBbEnvOverriding(EBlackboxEnv original, EBlackboxEnv override) noexcept;

    template <class T>
    class TProtectedValue {
        class TAssignOp {
        public:
            static void Assign(T& l, const T& r) {
                l = r;
            }

            template <typename U>
            static void Assign(std::shared_ptr<U>& l, std::shared_ptr<U>& r) {
                l.swap(r);
            }

            template <typename U>
            static void Assign(TIntrusiveConstPtr<U>& l, TIntrusiveConstPtr<U>& r) {
                l.Swap(r);
            }
        };

    public:
        TProtectedValue() = default;

        TProtectedValue(T value)
            : Value_(value)
        {
        }

        T Get() const {
            with_lock (Lock_) {
                return Value_;
            }
        }

        void Set(T o) {
            with_lock (Lock_) {
                TAssignOp::Assign(Value_, o);
            }
        }

    private:
        T Value_;
        mutable TAdaptiveLock Lock_;
    };
}