]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Core/ThreadSafeSTLVector.h
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / include / lldb / Core / ThreadSafeSTLVector.h
1 //===-- ThreadSafeSTLVector.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_ThreadSafeSTLVector_h_
11 #define liblldb_ThreadSafeSTLVector_h_
12
13 // C Includes
14 // C++ Includes
15 #include <vector>
16 #include <mutex>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-defines.h"
21
22 namespace lldb_private {
23     
24     template <typename _Object>
25     class ThreadSafeSTLVector
26     {
27     public:
28         typedef std::vector<_Object> collection;
29         typedef typename collection::iterator iterator;
30         typedef typename collection::const_iterator const_iterator;
31         //------------------------------------------------------------------
32         // Constructors and Destructors
33         //------------------------------------------------------------------
34         ThreadSafeSTLVector() : m_collection(), m_mutex() {}
35         
36         ~ThreadSafeSTLVector() = default;
37         
38         bool
39         IsEmpty() const
40         {
41             std::lock_guard<std::recursive_mutex> guard(m_mutex);
42             return m_collection.empty();
43         }
44         
45         void
46         Clear()
47         {
48             std::lock_guard<std::recursive_mutex> guard(m_mutex);
49             return m_collection.clear();
50         }
51         
52         size_t
53         GetCount()
54         {
55             std::lock_guard<std::recursive_mutex> guard(m_mutex);
56             return m_collection.size();
57         }
58         
59         void
60         AppendObject (_Object& object)
61         {
62             std::lock_guard<std::recursive_mutex> guard(m_mutex);
63             m_collection.push_back(object);
64         }
65         
66         _Object
67         GetObject (size_t index)
68         {
69             std::lock_guard<std::recursive_mutex> guard(m_mutex);
70             return m_collection.at(index);
71         }
72         
73         void
74         SetObject (size_t index, const _Object& object)
75         {
76             std::lock_guard<std::recursive_mutex> guard(m_mutex);
77             m_collection.at(index) = object;
78         }
79         
80         std::recursive_mutex &
81         GetMutex()
82         {
83             return m_mutex;
84         }
85         
86     private:
87         collection m_collection;
88         mutable std::recursive_mutex m_mutex;
89         
90         //------------------------------------------------------------------
91         // For ThreadSafeSTLVector only
92         //------------------------------------------------------------------
93         DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLVector);
94     };
95     
96     
97 } // namespace lldb_private
98
99 #endif  // liblldb_ThreadSafeSTLVector_h_