/usr/include/caf/intrusive_ptr.hpp is in libcaf-dev 0.13.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_INTRUSIVE_PTR_HPP
#define CAF_INTRUSIVE_PTR_HPP
#include <cstddef>
#include <algorithm>
#include <stdexcept>
#include <type_traits>
#include "caf/ref_counted.hpp"
#include "caf/detail/comparable.hpp"
namespace caf {
/**
* An intrusive, reference counting smart pointer impelementation.
* @relates ref_counted
*/
template <class T>
class intrusive_ptr : detail::comparable<intrusive_ptr<T>>,
detail::comparable<intrusive_ptr<T>, const T*>,
detail::comparable<intrusive_ptr<T>, std::nullptr_t> {
public:
using pointer = T*;
using const_pointer = const T*;
using element_type = T;
using reference = T&;
using const_reference = const T&;
constexpr intrusive_ptr() : m_ptr(nullptr) {
// nop
}
intrusive_ptr(pointer raw_ptr, bool add_ref = true) {
set_ptr(raw_ptr, add_ref);
}
intrusive_ptr(intrusive_ptr&& other) : m_ptr(other.release()) {
// nop
}
intrusive_ptr(const intrusive_ptr& other) {
set_ptr(other.get(), true);
}
template <class Y>
intrusive_ptr(intrusive_ptr<Y> other) : m_ptr(other.release()) {
static_assert(std::is_convertible<Y*, T*>::value,
"Y* is not assignable to T*");
}
~intrusive_ptr() {
if (m_ptr) {
intrusive_ptr_release(m_ptr);
}
}
void swap(intrusive_ptr& other) {
std::swap(m_ptr, other.m_ptr);
}
/**
* Returns the raw pointer without modifying reference
* count and sets this to `nullptr`.
*/
pointer release() {
auto result = m_ptr;
m_ptr = nullptr;
return result;
}
void reset(pointer new_value = nullptr, bool add_ref = true) {
auto old = m_ptr;
set_ptr(new_value, add_ref);
if (old) {
intrusive_ptr_release(old);
}
}
template <class... Ts>
void emplace(Ts&&... xs) {
reset(new T(std::forward<Ts>(xs)...));
}
intrusive_ptr& operator=(pointer ptr) {
reset(ptr);
return *this;
}
intrusive_ptr& operator=(intrusive_ptr&& other) {
swap(other);
return *this;
}
intrusive_ptr& operator=(const intrusive_ptr& other) {
intrusive_ptr tmp{other};
swap(tmp);
return *this;
}
template <class Y>
intrusive_ptr& operator=(intrusive_ptr<Y> other) {
intrusive_ptr tmp{std::move(other)};
swap(tmp);
return *this;
}
pointer get() const {
return m_ptr;
}
pointer operator->() const {
return m_ptr;
}
reference operator*() const {
return *m_ptr;
}
bool operator!() const {
return m_ptr == nullptr;
}
explicit operator bool() const {
return m_ptr != nullptr;
}
ptrdiff_t compare(const_pointer ptr) const {
return static_cast<ptrdiff_t>(get() - ptr);
}
ptrdiff_t compare(const intrusive_ptr& other) const {
return compare(other.get());
}
ptrdiff_t compare(std::nullptr_t) const {
return reinterpret_cast<ptrdiff_t>(get());
}
template <class C>
intrusive_ptr<C> downcast() const {
return (m_ptr) ? dynamic_cast<C*>(get()) : nullptr;
}
template <class C>
intrusive_ptr<C> upcast() const {
return (m_ptr) ? static_cast<C*>(get()) : nullptr;
}
private:
void set_ptr(pointer raw_ptr, bool add_ref) {
m_ptr = raw_ptr;
if (raw_ptr && add_ref) {
intrusive_ptr_add_ref(raw_ptr);
}
}
pointer m_ptr;
};
/**
* @relates intrusive_ptr
*/
template <class X, typename Y>
bool operator==(const intrusive_ptr<X>& lhs, const intrusive_ptr<Y>& rhs) {
return lhs.get() == rhs.get();
}
/**
* @relates intrusive_ptr
*/
template <class X, typename Y>
bool operator!=(const intrusive_ptr<X>& lhs, const intrusive_ptr<Y>& rhs) {
return !(lhs == rhs);
}
} // namespace caf
#endif // CAF_INTRUSIVE_PTR_HPP
|