Seastar
High performance C++ framework for concurrent servers
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/concepts.hh>
30 #include <seastar/util/modules.hh>
31 #endif
32 
34 namespace seastar {
35 
36 SEASTAR_MODULE_EXPORT_BEGIN
37 
39 class checked_ptr_is_null_exception : public std::exception {};
40 
48  void operator()() const {
50  }
51 };
52 SEASTAR_MODULE_EXPORT_END
53 
56 namespace internal {
57 
61 
65 template <typename T>
67 SEASTAR_CONCEPT( requires requires (T ptr) {
68  ptr.get();
69 })
71 inline typename std::pointer_traits<std::remove_const_t<T>>::element_type* checked_ptr_do_get(T& ptr) {
72  return ptr.get();
73 }
74 
78 template <typename T>
79 inline T* checked_ptr_do_get(T* ptr) noexcept {
80  return ptr;
81 }
83 }
85 
101 SEASTAR_MODULE_EXPORT
102 template<typename Ptr, typename NullDerefAction = default_null_deref_action>
104 SEASTAR_CONCEPT( requires std::is_default_constructible_v<NullDerefAction> && requires (NullDerefAction action) {
105  NullDerefAction();
106 })
108 class checked_ptr {
109 public:
111  using element_type = typename std::pointer_traits<Ptr>::element_type;
112 
115 
116 private:
117  Ptr _ptr = nullptr;
118 
119 private:
121  void check() const {
122  if (!_ptr) {
123  NullDerefAction()();
124  }
125  }
126 
127 public:
128  checked_ptr() noexcept(noexcept(Ptr(nullptr))) = default;
129  checked_ptr(std::nullptr_t) noexcept(std::is_nothrow_default_constructible_v<checked_ptr<Ptr, NullDerefAction>>) : checked_ptr() {}
130  checked_ptr(Ptr&& ptr) noexcept(std::is_nothrow_move_constructible_v<Ptr>) : _ptr(std::move(ptr)) {}
131  checked_ptr(const Ptr& p) noexcept(std::is_nothrow_copy_constructible_v<Ptr>) : _ptr(p) {}
132 
136 
139  pointer get() const {
140  check();
141  return internal::checked_ptr_do_get(_ptr);
142  }
143 
146  const Ptr& operator->() const {
147  check();
148  return _ptr;
149  }
150 
153  Ptr& operator->() {
154  check();
155  return _ptr;
156  }
157 
160  const element_type& operator*() const {
161  check();
162  return *_ptr;
163  }
164 
168  check();
169  return *_ptr;
170  }
172 
176 
179  explicit operator bool() const { return bool(_ptr); }
180 
181  bool operator==(const checked_ptr& other) const { return _ptr == other._ptr; }
182  bool operator!=(const checked_ptr& other) const { return _ptr != other._ptr; }
183 
186  size_t hash() const {
187  return std::hash<Ptr>()(_ptr);
188  }
190 };
191 
192 }
193 
194 namespace std {
196 SEASTAR_MODULE_EXPORT
197 template<typename T>
198 struct hash<seastar::checked_ptr<T>> {
203  size_t operator()(const seastar::checked_ptr<T>& p) const {
204  return p.hash();
205  }
206 };
207 }
The exception thrown by a default_null_deref_action.
Definition: checked_ptr.hh:39
seastar::checked_ptr class is a wrapper class that may be used with any pointer type (smart like std:...
Definition: checked_ptr.hh:108
element_type * pointer
Type of the pointer to the underlying element.
Definition: checked_ptr.hh:114
Ptr & operator->()
Definition: checked_ptr.hh:153
size_t hash() const
Definition: checked_ptr.hh:186
element_type & operator*()
Definition: checked_ptr.hh:167
const Ptr & operator->() const
Definition: checked_ptr.hh:146
typename std::pointer_traits< Ptr >::element_type element_type
Underlying element type.
Definition: checked_ptr.hh:111
pointer get() const
Definition: checked_ptr.hh:139
const element_type & operator*() const
Definition: checked_ptr.hh:160
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Default not engaged seastar::checked_ptr dereferencing action (functor).
Definition: checked_ptr.hh:46
void operator()() const
Definition: checked_ptr.hh:48
size_t operator()(const seastar::checked_ptr< T > &p) const
Definition: checked_ptr.hh:203