LLDB mainline
NonNullSharedPtr.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_NONNULLSHAREDPTR_H
10#define LLDB_UTILITY_NONNULLSHAREDPTR_H
11
12#include <cassert>
13#include <memory>
14#include <utility>
15
16namespace lldb_private {
17
18/// A non-nullable shared pointer that always holds a valid object.
19///
20/// NonNullSharedPtr is a smart pointer wrapper around std::shared_ptr that
21/// guarantees the pointer is never null.
22///
23/// This class is used for enforcing invariants at the type level and
24/// eliminating entire classes of null pointer bugs.
25///
26/// @tparam T The type of object to manage. Must be default-constructible.
27template <typename T> class NonNullSharedPtr : private std::shared_ptr<T> {
28 using Base = std::shared_ptr<T>;
29
30public:
31 NonNullSharedPtr(const std::shared_ptr<T> &t)
32 : Base(t ? t : std::make_shared<T>()) {
33 assert(t && "NonNullSharedPtr constructed from nullptr");
34 }
35
36 NonNullSharedPtr(std::shared_ptr<T> &&t) : Base(std::move(t)) {
37 const auto b = static_cast<bool>(*this);
38 assert(b && "NonNullSharedPtr constructed from nullptr");
39 if (!b)
40 Base::operator=(std::make_shared<T>());
41 }
42
43 NonNullSharedPtr(const NonNullSharedPtr &other) : Base(other) {}
44
45 NonNullSharedPtr(NonNullSharedPtr &&other) : Base(std::move(other)) {}
46
48 Base::operator=(other);
49 return *this;
50 }
51
53 Base::operator=(std::move(other));
54 return *this;
55 }
56
57 using Base::operator*;
58 using Base::operator->;
59 using Base::get;
60 using Base::use_count;
61 using Base::operator bool;
62
63 void swap(NonNullSharedPtr &other) { Base::swap(other); }
64
65 /// Explicitly deleted operations that could introduce nullptr.
66 /// @{
67 void reset() = delete;
68 void reset(T *ptr) = delete;
69 /// @}
70};
71
72} // namespace lldb_private
73
74/// Specialized swap function for NonNullSharedPtr to enable argument-dependent
75/// lookup (ADL) and efficient swapping.
76template <typename T>
81
82#endif
void swap(lldb_private::NonNullSharedPtr< T > &lhs, lldb_private::NonNullSharedPtr< T > &rhs)
Specialized swap function for NonNullSharedPtr to enable argument-dependent lookup (ADL) and efficien...
A non-nullable shared pointer that always holds a valid object.
NonNullSharedPtr(const std::shared_ptr< T > &t)
NonNullSharedPtr(NonNullSharedPtr &&other)
void swap(NonNullSharedPtr &other)
void reset()=delete
Explicitly deleted operations that could introduce nullptr.
void reset(T *ptr)=delete
NonNullSharedPtr(const NonNullSharedPtr &other)
NonNullSharedPtr(std::shared_ptr< T > &&t)
NonNullSharedPtr & operator=(NonNullSharedPtr &&other)
NonNullSharedPtr & operator=(const NonNullSharedPtr &other)
std::shared_ptr< lldb_private::DataExtractor > Base
A class that represents a running process on the host machine.