PriorityPointerPair.h revision 13d24fb1817faa7ccc4cfd799113ba1a2b8968eb
1//===-- PriorityPointerPair.h ----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_PriorityPointerPair_h_
11#define liblldb_PriorityPointerPair_h_
12
13#include "lldb/lldb-public.h"
14#include "lldb/Utility/SharingPtr.h"
15
16namespace lldb_utility {
17
18//----------------------------------------------------------------------
19// A prioritized pair of SharedPtr<T>. One of the two pointers is high
20// priority, the other is low priority.
21// The Get() method always returns high, if *high != NULL,
22// otherwise, low is returned (even if *low == NULL)
23//----------------------------------------------------------------------
24
25template<typename T>
26class PriorityPointerPair
27{
28public:
29
30    typedef T& reference_type;
31    typedef T* pointer_type;
32
33    typedef typename SHARED_PTR(T) T_SP;
34
35    PriorityPointerPair() :
36    m_high(),
37    m_low()
38    {}
39
40    PriorityPointerPair(pointer_type high,
41                        pointer_type low) :
42    m_high(high),
43    m_low(low)
44    {}
45
46    PriorityPointerPair(pointer_type low) :
47    m_high(),
48    m_low(low)
49    {}
50
51    PriorityPointerPair(T_SP& high,
52                        T_SP& low) :
53    m_high(high),
54    m_low(low)
55    {}
56
57    PriorityPointerPair(T_SP& low) :
58    m_high(),
59    m_low(low)
60    {}
61
62    void
63    SwapLow(pointer_type l)
64    {
65        m_low.swap(l);
66    }
67
68    void
69    SwapHigh(pointer_type h)
70    {
71        m_high.swap(h);
72    }
73
74    void
75    SwapLow(T_SP l)
76    {
77        m_low.swap(l);
78    }
79
80    void
81    SwapHigh(T_SP h)
82    {
83        m_high.swap(h);
84    }
85
86    T_SP
87    GetLow()
88    {
89        return m_low;
90    }
91
92    T_SP
93    GetHigh()
94    {
95        return m_high;
96    }
97
98    T_SP
99    Get()
100    {
101        if (m_high.get())
102            return m_high;
103        return m_low;
104    }
105
106    void
107    ResetHigh()
108    {
109        m_high.reset();
110    }
111
112    void
113    ResetLow()
114    {
115        m_low.reset();
116    }
117
118    void
119    Reset()
120    {
121        ResetLow();
122        ResetHigh();
123    }
124
125    reference_type
126    operator*() const
127    {
128        return Get().operator*();
129    }
130
131    pointer_type
132    operator->() const
133    {
134        return Get().operator->();
135    }
136
137    ~PriorityPointerPair();
138
139private:
140
141    T_SP m_high;
142    T_SP m_low;
143
144    DISALLOW_COPY_AND_ASSIGN (PriorityPointerPair);
145
146};
147
148} // namespace lldb_utility
149
150#endif // #ifndef liblldb_PriorityPointerPair_h_
151