8 #ifndef ATLAS_OBJECTS_SMARTPTR_H 9 #define ATLAS_OBJECTS_SMARTPTR_H 11 #include <Atlas/Exception.h> 14 namespace Atlas {
namespace Objects {
20 template<
class U>
friend 25 SmartPtr() noexcept : ptr(T::allocator.alloc()) {
27 SmartPtr(
const SmartPtr<T>& a) noexcept : ptr(a.get()) {
30 SmartPtr(SmartPtr<T>&& a) noexcept : ptr(a.get()) {
33 SmartPtr(T *a_ptr) noexcept : ptr(a_ptr)
37 template<
class oldType>
38 explicit SmartPtr(
const SmartPtr<oldType>& a) noexcept : ptr(a.get()) {
41 template<
class oldType>
42 explicit SmartPtr(SmartPtr<oldType>&& a) noexcept : ptr(a.get()) {
48 SmartPtr& operator=(
const SmartPtr<T>& a) noexcept {
49 if (a.get() != this->
get()) {
57 SmartPtr& operator=(SmartPtr<T>&& a) noexcept {
58 if (a.get() != this->
get()) {
69 template<
class newType>
70 operator SmartPtr<newType>()
const noexcept {
71 return SmartPtr<newType>(ptr);
73 template<
class newType>
74 operator SmartPtr<const newType>()
const noexcept {
75 return SmartPtr<const newType>(ptr);
77 constexpr
bool isValid() const noexcept {
78 return ptr !=
nullptr;
80 constexpr
bool operator!() const noexcept {
81 return this->ptr ==
nullptr;
84 explicit constexpr
operator bool () const noexcept
86 return !this->operator!();
89 constexpr T& operator*() const noexcept {
93 constexpr T* operator->() const noexcept {
97 constexpr T*
get()
const noexcept {
100 SmartPtr<T> copy() const noexcept
102 return SmartPtr(ptr->copy());
108 void decRef() const noexcept {
109 if (ptr !=
nullptr) {
113 void incRef() const noexcept {
114 if (ptr !=
nullptr) {
121 template<
typename returnPtrType,
class fromType>
122 returnPtrType smart_dynamic_cast(
const SmartPtr<fromType> & o)
124 return returnPtrType(dynamic_cast<typename returnPtrType::DataT*>(o.get()));
127 template<
typename returnPtrType,
class fromType>
128 returnPtrType smart_static_cast(
const SmartPtr<fromType> & o)
130 return returnPtrType((
typename returnPtrType::DataT *)o.get());
135 #endif // ATLAS_OBJECTS_SMARTPTR_H