BasePtr.h 967 B

1234567891011121314151617181920212223242526272829
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. template <typename T>
  7. class BasePtr
  8. {
  9. public:
  10. BasePtr(T * ptr = nullptr) : ptr(ptr) {}
  11. T ** operator&() { Assert(ptr == nullptr); return &ptr; }
  12. T * operator->() const { Assert(ptr != nullptr); return ptr; }
  13. operator T*() const { return ptr; }
  14. // Detach currently owned ptr. WARNING: This object no longer owns/manages the ptr.
  15. T * Detach()
  16. {
  17. T * ret = ptr;
  18. ptr = nullptr;
  19. return ret;
  20. }
  21. protected:
  22. T * ptr;
  23. private:
  24. BasePtr(const BasePtr<T>& ptr); // Disable
  25. BasePtr& operator=(BasePtr<T> const& ptr); // Disable
  26. };