daeSmartRef.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. #ifndef __DAE_SMARTREF_H__
  9. #define __DAE_SMARTREF_H__
  10. #include <assert.h>
  11. #include <dae/daeRefCountedObj.h>
  12. /**
  13. * The @c daeSmartRef template class automates reference counting for
  14. * objects derived from @c daeRefCountedObj.
  15. */
  16. template<class T> class daeSmartRef
  17. {
  18. public:
  19. /**
  20. * Constructor
  21. */
  22. inline daeSmartRef() : _ptr(NULL) { }
  23. /**
  24. * Destructor
  25. */
  26. inline ~daeSmartRef() {
  27. checkedRelease(_ptr);
  28. }
  29. /**
  30. * Copy Constructor that will convert from one template to the other.
  31. * @param smartRef a daeSmartRef to the object to copy from.
  32. */
  33. template<class U>
  34. inline daeSmartRef(const daeSmartRef<U>& smartRef) : _ptr(smartRef.cast()) {
  35. checkedRef(_ptr);
  36. }
  37. /**
  38. * Function that returns a pointer to object being reference counted.
  39. * @return the object being reference counted.
  40. */
  41. inline T* cast() const { return _ptr; }
  42. /**
  43. * Copy Constructor.
  44. * @param smartRef a daeSmartRef of the same template type to copy from
  45. */
  46. inline daeSmartRef(const daeSmartRef<T>& smartRef) : _ptr(smartRef._ptr) {
  47. checkedRef(_ptr);
  48. }
  49. /**
  50. * Constructor
  51. * @param ptr a pointer to an object of the same template type.
  52. */
  53. inline daeSmartRef(T* ptr) : _ptr(ptr) {
  54. checkedRef(_ptr);
  55. }
  56. /**
  57. * Overloaded assignment operator which will convert between template types.
  58. * @param smartRef a daeSmartRef to the object to copy from.
  59. * @return Returns a reference to this object.
  60. */
  61. template<class U>
  62. inline const daeSmartRef<T>& operator=(const daeSmartRef<U>& smartRef) {
  63. T* ptr = smartRef.cast();
  64. checkedRef(ptr);
  65. checkedRelease(_ptr);
  66. _ptr = ptr;
  67. return *this; }
  68. /**
  69. * Overloaded assignment operator.
  70. * @param other a daeSmartRef to the object to copy from. Must be of the same template type.
  71. * @return Returns a reference to this object.
  72. */
  73. inline const daeSmartRef<T>& operator=(const daeSmartRef<T>& other) {
  74. T* ptr = other._ptr;
  75. checkedRef(ptr);
  76. checkedRelease(_ptr);
  77. _ptr = ptr;
  78. return *this; }
  79. /**
  80. * Overloaded assignment operator.
  81. * @param ptr a pointer to the object to copy from. Must be of the same template type.
  82. * @return Returns a reference to this object.
  83. */
  84. inline const daeSmartRef<T>& operator=(T* ptr) {
  85. checkedRef(ptr);
  86. checkedRelease(_ptr);
  87. _ptr = ptr;
  88. return *this; }
  89. /**
  90. * Overloaded member selection operator.
  91. * @return a pointer of the template class to the object.
  92. */
  93. inline T* operator->() const {
  94. assert (_ptr != (T*)NULL); return _ptr; }
  95. /**
  96. * Overloaded cast operator.
  97. * @return a pointer of the template class to the object.
  98. */
  99. inline operator T*() const {
  100. return _ptr; }
  101. /**
  102. * Static cast function.
  103. * @param smartRef a smartRef to cast from
  104. * @return a pointer to an object of this template class
  105. */
  106. template<class U>
  107. inline static T* staticCast(const daeSmartRef<U>& smartRef) {
  108. return static_cast<T*>(smartRef.cast()); }
  109. private:
  110. /* The pointer to the element which is being reference counted */
  111. T* _ptr;
  112. };
  113. #endif // __DAE_SMARTREF_H__