ScopedPtr.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SCOPEDPTR_HPP
  14. #define ZT_SCOPEDPTR_HPP
  15. #include "Constants.hpp"
  16. namespace ZeroTier {
  17. /**
  18. * Simple scoped pointer
  19. *
  20. * This is used in the core to avoid requiring C++11 and because auto_ptr is weird.
  21. */
  22. template<typename T>
  23. class ScopedPtr
  24. {
  25. public:
  26. ZT_ALWAYS_INLINE ScopedPtr(T *const p) : _p(p) {}
  27. ZT_ALWAYS_INLINE ~ScopedPtr() { delete _p; }
  28. ZT_ALWAYS_INLINE T *operator->() const { return _p; }
  29. ZT_ALWAYS_INLINE T &operator*() const { return *_p; }
  30. ZT_ALWAYS_INLINE operator bool() const { return (_p != (T *)0); }
  31. ZT_ALWAYS_INLINE T *ptr() const { return _p; }
  32. ZT_ALWAYS_INLINE bool operator==(const ScopedPtr &p) const { return (_p == p._p); }
  33. ZT_ALWAYS_INLINE bool operator!=(const ScopedPtr &p) const { return (_p != p._p); }
  34. ZT_ALWAYS_INLINE bool operator==(T *const p) const { return (_p == p); }
  35. ZT_ALWAYS_INLINE bool operator!=(T *const p) const { return (_p != p); }
  36. private:
  37. ZT_ALWAYS_INLINE ScopedPtr() {}
  38. T *const _p;
  39. };
  40. } // namespace ZeroTier
  41. #endif