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 
27 #include <exception>
28 #include <seastar/util/concepts.hh>
29 
31 namespace seastar {
32 
34 class checked_ptr_is_null_exception : public std::exception {};
35 
43  void operator()() const {
45  }
46 };
47 
50 namespace internal {
51 
55 
59 template <typename T>
61 SEASTAR_CONCEPT( requires requires (T ptr) {
62  ptr.get();
63 })
65 inline typename std::pointer_traits<std::remove_const_t<T>>::element_type* checked_ptr_do_get(T& ptr) {
66  return ptr.get();
67 }
68 
72 template <typename T>
73 inline T* checked_ptr_do_get(T* ptr) noexcept {
74  return ptr;
75 }
77 }
79 
95 template<typename Ptr, typename NullDerefAction = default_null_deref_action>
97 SEASTAR_CONCEPT( requires std::is_default_constructible<NullDerefAction>::value && requires (NullDerefAction action) {
98  NullDerefAction();
99 })
101 class checked_ptr {
102 public:
104  using element_type = typename std::pointer_traits<Ptr>::element_type;
105 
108 
109 private:
110  Ptr _ptr = nullptr;
111 
112 private:
114  void check() const {
115  if (!_ptr) {
116  NullDerefAction()();
117  }
118  }
119 
120 public:
121  checked_ptr() noexcept(noexcept(Ptr(nullptr))) = default;
122  checked_ptr(std::nullptr_t) noexcept(std::is_nothrow_default_constructible<checked_ptr<Ptr, NullDerefAction>>::value) : checked_ptr() {}
123  checked_ptr(Ptr&& ptr) noexcept(std::is_nothrow_move_constructible<Ptr>::value) : _ptr(std::move(ptr)) {}
124  checked_ptr(const Ptr& p) noexcept(std::is_nothrow_copy_constructible<Ptr>::value) : _ptr(p) {}
125 
129 
132  pointer get() const {
133  check();
134  return internal::checked_ptr_do_get(_ptr);
135  }
136 
139  const Ptr& operator->() const {
140  check();
141  return _ptr;
142  }
143 
146  Ptr& operator->() {
147  check();
148  return _ptr;
149  }
150 
153  const element_type& operator*() const {
154  check();
155  return *_ptr;
156  }
157 
161  check();
162  return *_ptr;
163  }
165 
169 
172  explicit operator bool() const { return bool(_ptr); }
173 
174  bool operator==(const checked_ptr& other) const { return _ptr == other._ptr; }
175  bool operator!=(const checked_ptr& other) const { return _ptr != other._ptr; }
176 
179  size_t hash() const {
180  return std::hash<Ptr>()(_ptr);
181  }
183 };
184 
185 }
186 
187 namespace std {
189 template<typename T>
190 struct hash<seastar::checked_ptr<T>> {
195  size_t operator()(const seastar::checked_ptr<T>& p) const {
196  return p.hash();
197  }
198 };
199 }
The exception thrown by a default_null_deref_action.
Definition: checked_ptr.hh:34
seastar::checked_ptr class is a wrapper class that may be used with any pointer type (smart like std:...
Definition: checked_ptr.hh:101
element_type * pointer
Type of the pointer to the underlying element.
Definition: checked_ptr.hh:107
Ptr & operator->()
Definition: checked_ptr.hh:146
size_t hash() const
Definition: checked_ptr.hh:179
element_type & operator*()
Definition: checked_ptr.hh:160
const Ptr & operator->() const
Definition: checked_ptr.hh:139
typename std::pointer_traits< Ptr >::element_type element_type
Underlying element type.
Definition: checked_ptr.hh:104
pointer get() const
Definition: checked_ptr.hh:132
const element_type & operator*() const
Definition: checked_ptr.hh:153
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
Default not engaged seastar::checked_ptr dereferencing action (functor).
Definition: checked_ptr.hh:41
void operator()() const
Definition: checked_ptr.hh:43
size_t operator()(const seastar::checked_ptr< T > &p) const
Definition: checked_ptr.hh:195