Loading...
Searching...
No Matches
pointer.h
Go to the documentation of this file.
1#ifndef _BBM_POINTER_H_
2#define _BBM_POINTER_H_
3
4#include <ostream>
5#include <memory>
6#include "util/typestring.h"
7
8/************************************************************************/
9/*! \file pointer.h
10
11 \brief Pointer wrapper that takes both shared and non-shared pointers. If
12 the pointer is 'managed' then the pointer is deleted when the reference
13 count hits zero. By default a pointer is not managed unless a shared ptr is
14 assigned or it is explicitely marked as managed during construction.
15************************************************************************/
16
17namespace bbm {
18
19 /*********************************************************************/
20 /*! \brief Pointer wrapper that takes handle both shared as well as regular
21 (unmanaged) pointers.
22
23 This is a wrapper around shared_ptr that replaced the deleter with an
24 empty deleter if the object should not be managed by the pointer.
25 *********************************************************************/
26 template<typename T>
27 class pointer : public std::shared_ptr<std::remove_reference_t<T>>
28 {
29 using ptr_t = std::add_pointer_t<std::remove_all_extents_t<T>>;
30 using base_type = std::shared_ptr<std::remove_reference_t<T>>;
31 static constexpr auto empty_delete = [](ptr_t){};
32 public:
33
34 //! \brief Init from a regular pointer
35 inline constexpr pointer(ptr_t ptr=nullptr, bool managed=false) noexcept : base_type()
36 {
37 if(managed) this->reset(ptr);
38 else this->reset(ptr, empty_delete);
39 }
40
41 //! @{ \name Init from a shared_ptr
42 template<typename Y>
43 inline constexpr pointer(const std::shared_ptr<Y>& ptr) noexcept : base_type(ptr) {}
44
45 template<typename Y>
46 inline constexpr pointer(std::shared_ptr<Y>&& ptr) noexcept : base_type(ptr) {}
47 //! @}
48
49 //! \brief Returns true if the pointer manages the object deallocation
50 inline constexpr bool is_managed(void) const
51 {
52 auto del = std::get_deleter<decltype(empty_delete)>(*this);
53 return !(del && (*del == empty_delete));
54 }
55 };
56
57} // end bbm namespace
58
59
60#endif /* _BBM_POINTER_H_ */
Pointer wrapper that takes handle both shared as well as regular (unmanaged) pointers.
Definition: pointer.h:28
constexpr pointer(const std::shared_ptr< Y > &ptr) noexcept
Definition: pointer.h:43
static constexpr auto empty_delete
Definition: pointer.h:31
std::shared_ptr< std::remove_reference_t< T > > base_type
Definition: pointer.h:30
constexpr bool is_managed(void) const
Returns true if the pointer manages the object deallocation.
Definition: pointer.h:50
constexpr pointer(ptr_t ptr=nullptr, bool managed=false) noexcept
Init from a regular pointer.
Definition: pointer.h:35
constexpr pointer(std::shared_ptr< Y > &&ptr) noexcept
Definition: pointer.h:46
std::add_pointer_t< std::remove_all_extents_t< T > > ptr_t
Definition: pointer.h:29
Definition: aggregatebsdf.h:29
produce stringview of type name of a type. Avoids using typeid for GCC, MSVC, and CLANG....