/* * Copyright (c)2013-2020 ZeroTier, Inc. * * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * * Change Date: 2024-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. */ /****/ #ifndef ZT_SCOPEDPTR_HPP #define ZT_SCOPEDPTR_HPP #include "Constants.hpp" namespace ZeroTier { /** * Simple scoped pointer * * This is used in the core to avoid requiring C++11 and because auto_ptr is weird. */ template class ScopedPtr { public: explicit ZT_ALWAYS_INLINE ScopedPtr(T *const p) : _p(p) {} ZT_ALWAYS_INLINE ~ScopedPtr() { delete _p; } ZT_ALWAYS_INLINE T *operator->() const { return _p; } ZT_ALWAYS_INLINE T &operator*() const { return *_p; } explicit ZT_ALWAYS_INLINE operator bool() const { return (_p != (T *)0); } ZT_ALWAYS_INLINE T *ptr() const { return _p; } ZT_ALWAYS_INLINE void swap(const ScopedPtr &p) { T *const tmp = _p; _p = p._p; p._p = tmp; } ZT_ALWAYS_INLINE bool operator==(const ScopedPtr &p) const { return (_p == p._p); } ZT_ALWAYS_INLINE bool operator!=(const ScopedPtr &p) const { return (_p != p._p); } ZT_ALWAYS_INLINE bool operator==(T *const p) const { return (_p == p); } ZT_ALWAYS_INLINE bool operator!=(T *const p) const { return (_p != p); } private: ZT_ALWAYS_INLINE ScopedPtr() {} ZT_ALWAYS_INLINE ScopedPtr(const ScopedPtr &p) : _p(nullptr) {} ZT_ALWAYS_INLINE ScopedPtr &operator=(const ScopedPtr &p) { return *this; } T *const _p; }; } // namespace ZeroTier namespace std { template ZT_ALWAYS_INLINE void swap(ZeroTier::ScopedPtr &a,ZeroTier::ScopedPtr &b) { a.swap(b); } } #endif