aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Utility/PriorityPointerPair.h
blob: 49f0765d0f960516d0e263bfdbb6b220882c5b40 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===-- PriorityPointerPair.h ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_PriorityPointerPair_h_
#define liblldb_PriorityPointerPair_h_

#include "lldb/lldb-public.h"
#include "lldb/Utility/SharingPtr.h"

namespace lldb_utility {

//----------------------------------------------------------------------
// A prioritized pair of SharedPtr<T>. One of the two pointers is high
// priority, the other is low priority.
// The Get() method always returns high, if *high != NULL,
// otherwise, low is returned (even if *low == NULL)
//----------------------------------------------------------------------

template<typename T>
class PriorityPointerPair
{
public:
    
    typedef T& reference_type;
    typedef T* pointer_type;
    
    typedef typename std::shared_ptr<T> T_SP;
    
    PriorityPointerPair() : 
    m_high(),
    m_low()
    {}
    
    PriorityPointerPair(pointer_type high,
                        pointer_type low) : 
    m_high(high),
    m_low(low)
    {}

    PriorityPointerPair(pointer_type low) : 
    m_high(),
    m_low(low)
    {}

    PriorityPointerPair(T_SP& high,
                        T_SP& low) : 
    m_high(high),
    m_low(low)
    {}
    
    PriorityPointerPair(T_SP& low) : 
    m_high(),
    m_low(low)
    {}
    
    void
    SwapLow(pointer_type l)
    {
        m_low.swap(l);
    }
    
    void
    SwapHigh(pointer_type h)
    {
        m_high.swap(h);
    }
    
    void
    SwapLow(T_SP l)
    {
        m_low.swap(l);
    }

    void
    SwapHigh(T_SP h)
    {
        m_high.swap(h);
    }
    
    T_SP
    GetLow()
    {
        return m_low;
    }
    
    T_SP
    GetHigh()
    {
        return m_high;
    }
    
    T_SP
    Get()
    {
        if (m_high.get())
            return m_high;
        return m_low;
    }
    
    void
    ResetHigh()
    {
        m_high.reset();
    }
    
    void
    ResetLow()
    {
        m_low.reset();
    }
    
    void
    Reset()
    {
        ResetLow();
        ResetHigh();
    }
    
    reference_type
    operator*() const
    {
        return Get().operator*();
    }
    
    pointer_type
    operator->() const
    {
        return Get().operator->();
    }
    
    ~PriorityPointerPair();
    
private:

    T_SP m_high;
    T_SP m_low;
    
    DISALLOW_COPY_AND_ASSIGN (PriorityPointerPair);
        
};

} // namespace lldb_utility

#endif // #ifndef liblldb_PriorityPointerPair_h_