1 //===-- ThreadSafeSTLVector.h ------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ThreadSafeSTLVector_h_
11 #define liblldb_ThreadSafeSTLVector_h_
16 #include "lldb/lldb-defines.h"
18 namespace lldb_private {
20 template <typename _Object> class ThreadSafeSTLVector {
22 typedef std::vector<_Object> collection;
23 typedef typename collection::iterator iterator;
24 typedef typename collection::const_iterator const_iterator;
25 // Constructors and Destructors
26 ThreadSafeSTLVector() : m_collection(), m_mutex() {}
28 ~ThreadSafeSTLVector() = default;
30 bool IsEmpty() const {
31 std::lock_guard<std::recursive_mutex> guard(m_mutex);
32 return m_collection.empty();
36 std::lock_guard<std::recursive_mutex> guard(m_mutex);
37 return m_collection.clear();
41 std::lock_guard<std::recursive_mutex> guard(m_mutex);
42 return m_collection.size();
45 void AppendObject(_Object &object) {
46 std::lock_guard<std::recursive_mutex> guard(m_mutex);
47 m_collection.push_back(object);
50 _Object GetObject(size_t index) {
51 std::lock_guard<std::recursive_mutex> guard(m_mutex);
52 return m_collection.at(index);
55 void SetObject(size_t index, const _Object &object) {
56 std::lock_guard<std::recursive_mutex> guard(m_mutex);
57 m_collection.at(index) = object;
60 std::recursive_mutex &GetMutex() { return m_mutex; }
63 collection m_collection;
64 mutable std::recursive_mutex m_mutex;
66 // For ThreadSafeSTLVector only
67 DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLVector);
70 } // namespace lldb_private
72 #endif // liblldb_ThreadSafeSTLVector_h_