Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
checked_ptr.hh
Go to the documentation of this file.
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2017 ScyllaDB
20 */
21
22#pragma once
23
26#ifndef SEASTAR_MODULE
27#include <exception>
28#include <memory>
29#include <seastar/util/modules.hh>
30#endif
31
33namespace seastar {
34
35SEASTAR_MODULE_EXPORT_BEGIN
36
38class checked_ptr_is_null_exception : public std::exception {};
39
47 void operator()() const {
49 }
50};
51SEASTAR_MODULE_EXPORT_END
52
55namespace internal {
56
60
64template <typename T>
65requires requires (T ptr) {
66 ptr.get();
67}
68inline typename std::pointer_traits<std::remove_const_t<T>>::element_type* checked_ptr_do_get(T& ptr) {
69 return ptr.get();
70}
71
75template <typename T>
76inline T* checked_ptr_do_get(T* ptr) noexcept {
77 return ptr;
78}
80}
82
98SEASTAR_MODULE_EXPORT
99template<typename Ptr, typename NullDerefAction = default_null_deref_action>
100requires std::is_default_constructible_v<NullDerefAction> && requires (NullDerefAction action) {
101 NullDerefAction();
102}
104public:
106 using element_type = typename std::pointer_traits<Ptr>::element_type;
107
110
111private:
112 Ptr _ptr = nullptr;
113
114private:
116 void check() const {
117 if (!_ptr) {
118 NullDerefAction()();
119 }
120 }
121
122public:
123 checked_ptr() noexcept(noexcept(Ptr(nullptr))) = default;
124 checked_ptr(std::nullptr_t) noexcept(std::is_nothrow_default_constructible_v<checked_ptr<Ptr, NullDerefAction>>) : checked_ptr() {}
125 checked_ptr(Ptr&& ptr) noexcept(std::is_nothrow_move_constructible_v<Ptr>) : _ptr(std::move(ptr)) {}
126 checked_ptr(const Ptr& p) noexcept(std::is_nothrow_copy_constructible_v<Ptr>) : _ptr(p) {}
127
131
134 pointer get() const {
135 check();
136 return internal::checked_ptr_do_get(_ptr);
137 }
138
141 const Ptr& operator->() const {
142 check();
143 return _ptr;
144 }
145
148 Ptr& operator->() {
149 check();
150 return _ptr;
151 }
152
155 const element_type& operator*() const {
156 check();
157 return *_ptr;
158 }
159
163 check();
164 return *_ptr;
165 }
167
171
174 explicit operator bool() const { return bool(_ptr); }
175
176 bool operator==(const checked_ptr& other) const { return _ptr == other._ptr; }
177 bool operator!=(const checked_ptr& other) const { return _ptr != other._ptr; }
178
181 size_t hash() const {
182 return std::hash<Ptr>()(_ptr);
183 }
185};
186
187}
188
189namespace std {
191SEASTAR_MODULE_EXPORT
192template<typename T>
193struct hash<seastar::checked_ptr<T>> {
198 size_t operator()(const seastar::checked_ptr<T>& p) const {
199 return p.hash();
200 }
201};
202}
The exception thrown by a default_null_deref_action.
Definition: checked_ptr.hh:38
seastar::checked_ptr class is a wrapper class that may be used with any pointer type (smart like std:...
Definition: checked_ptr.hh:103
element_type & operator*()
Definition: checked_ptr.hh:162
element_type * pointer
Type of the pointer to the underlying element.
Definition: checked_ptr.hh:109
Ptr & operator->()
Definition: checked_ptr.hh:148
const element_type & operator*() const
Definition: checked_ptr.hh:155
size_t hash() const
Definition: checked_ptr.hh:181
const Ptr & operator->() const
Definition: checked_ptr.hh:141
typename std::pointer_traits< Ptr >::element_type element_type
Underlying element type.
Definition: checked_ptr.hh:106
pointer get() const
Definition: checked_ptr.hh:134
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
STL namespace.
Default not engaged seastar::checked_ptr dereferencing action (functor).
Definition: checked_ptr.hh:45
void operator()() const
Definition: checked_ptr.hh:47
size_t operator()(const seastar::checked_ptr< T > &p) const
Definition: checked_ptr.hh:198