aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/mind/bscontroller/indir.h
blob: ed2df12c437962bdcd2097977f61019aab4d9e44 (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
#pragma once 
 
#include "defs.h" 
 
template<typename TValue> 
class TIndirectReferable { 
    friend class TPtr; 
    TValue *Current; 
 
public: 
    class TPtr { 
        const TValue *Object = nullptr; 
 
    public: 
        TPtr() = default; 
 
        TPtr(const TValue *object) 
            : Object(object) 
        {} 
 
        operator const TValue*() const { 
            return Get(); 
        } 
 
        const TValue *operator ->() const { 
            return *this; 
        } 
 
        const TValue& operator *() const { 
            const TValue *ptr = Get(); 
            Y_VERIFY_DEBUG(ptr); 
            return *ptr; 
        } 
 
        // MUST be called from OnCommit() only 
        TValue& Mutable() const { 
            TValue *ptr = Get(); 
            Y_VERIFY_DEBUG(ptr); 
            return *ptr; 
        } 
 
    private: 
        TValue *Get() const { 
            return Object ? Object->Current : nullptr; 
        } 
    }; 
 
public: 
    TIndirectReferable() 
        : Current(static_cast<TValue*>(this)) 
    {} 
 
    TIndirectReferable(const TIndirectReferable&) 
        : TIndirectReferable() // prevent from copying link -- set it up for outselves 
    {} 
 
    TIndirectReferable& operator =(const TIndirectReferable&) = delete; 
    TIndirectReferable& operator =(TIndirectReferable&&) = delete; 
 
    // TOverlayMap machinery 
    void OnClone(const THolder<TValue>& clone) { 
        Y_VERIFY_DEBUG(Current == this); 
        Current = clone.Get(); 
    } 
 
    std::pair<void**, void*> Preserve() { 
        Y_VERIFY_DEBUG(Current != this); 
        return std::make_pair((void**)&Current, (void*)std::exchange(Current, static_cast<TValue*>(this))); 
    } 
 
    void OnRollback() { 
        Current = static_cast<TValue*>(this); 
    } 
};