Ptr.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_PTR_H
  7. #define VOXEL_PTR_H
  8. #include <memory>
  9. namespace Voxel
  10. {
  11. /**
  12. * \addtogroup Util
  13. * @{
  14. */
  15. template <typename T>
  16. void deleter(T *data)
  17. {
  18. delete data;
  19. }
  20. /**
  21. * NOTE: This is a simple way to ensure that matching new/delete are used.
  22. * WARNING:
  23. * 1. DO NOT CREATE the pointer "data" in some other library (so/dll) context and pass it to Ptr<> created in another library (so/dll).
  24. * Always. create Ptr<> and allocate "data" in the same so/dll.
  25. * 2. DO NOT USE THIS CLASS FOR array data, that is, allocated with new[]
  26. */
  27. template <typename T>
  28. class Ptr: public std::shared_ptr<T>
  29. {
  30. public:
  31. Ptr(T *data): std::shared_ptr<T>(data, deleter<T>) {}
  32. template <typename _Deleter>
  33. Ptr(T *data, _Deleter d): std::shared_ptr<T>(data, d) {}
  34. Ptr(): std::shared_ptr<T>() {}
  35. Ptr(const std::shared_ptr<T> &p): std::shared_ptr<T>(p) {}
  36. #ifdef SWIG
  37. T *operator ->() { return this->std::shared_ptr<T>::operator->(); }
  38. const T *operator ->() const { return this->std::shared_ptr<T>::operator->(); }
  39. #endif
  40. virtual ~Ptr() {}
  41. };
  42. /**
  43. * @}
  44. */
  45. }
  46. #endif // VOXEL_PTR_H