daeSmartRef.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
  5. * file except in compliance with the License. You may obtain a copy of the License at:
  6. * http://research.scea.com/scea_shared_source_license.html
  7. *
  8. * Unless required by applicable law or agreed to in writing, software distributed under the License
  9. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing permissions and limitations under the
  11. * License.
  12. */
  13. #ifndef __DAE_SMARTREF_H__
  14. #define __DAE_SMARTREF_H__
  15. #include <assert.h>
  16. #include <dae/daeRefCountedObj.h>
  17. /**
  18. * The @c daeSmartRef template class automates reference counting for
  19. * objects derived from @c daeRefCountedObj.
  20. */
  21. template<class T> class daeSmartRef
  22. {
  23. public:
  24. /**
  25. * Constructor
  26. */
  27. inline daeSmartRef() : _ptr(NULL) { }
  28. /**
  29. * Destructor
  30. */
  31. inline ~daeSmartRef() {
  32. checkedRelease(_ptr);
  33. }
  34. /**
  35. * Copy Constructor that will convert from one template to the other.
  36. * @param smartRef a daeSmartRef to the object to copy from.
  37. */
  38. template<class U>
  39. inline daeSmartRef(const daeSmartRef<U>& smartRef) : _ptr(smartRef.cast()) {
  40. checkedRef(_ptr);
  41. }
  42. /**
  43. * Function that returns a pointer to object being reference counted.
  44. * @return the object being reference counted.
  45. */
  46. inline T* cast() const { return _ptr; }
  47. /**
  48. * Copy Constructor.
  49. * @param smartRef a daeSmartRef of the same template type to copy from
  50. */
  51. inline daeSmartRef(const daeSmartRef<T>& smartRef) : _ptr(smartRef._ptr) {
  52. checkedRef(_ptr);
  53. }
  54. /**
  55. * Constructor
  56. * @param ptr a pointer to an object of the same template type.
  57. */
  58. inline daeSmartRef(T* ptr) : _ptr(ptr) {
  59. checkedRef(_ptr);
  60. }
  61. /**
  62. * Overloaded assignment operator which will convert between template types.
  63. * @param smartRef a daeSmartRef to the object to copy from.
  64. * @return Returns a reference to this object.
  65. */
  66. template<class U>
  67. inline const daeSmartRef<T>& operator=(const daeSmartRef<U>& smartRef) {
  68. T* ptr = smartRef.cast();
  69. checkedRef(ptr);
  70. checkedRelease(_ptr);
  71. _ptr = ptr;
  72. return *this; }
  73. /**
  74. * Overloaded assignment operator.
  75. * @param other a daeSmartRef to the object to copy from. Must be of the same template type.
  76. * @return Returns a reference to this object.
  77. */
  78. inline const daeSmartRef<T>& operator=(const daeSmartRef<T>& other) {
  79. T* ptr = other._ptr;
  80. checkedRef(ptr);
  81. checkedRelease(_ptr);
  82. _ptr = ptr;
  83. return *this; }
  84. /**
  85. * Overloaded assignment operator.
  86. * @param ptr a pointer to the object to copy from. Must be of the same template type.
  87. * @return Returns a reference to this object.
  88. */
  89. inline const daeSmartRef<T>& operator=(T* ptr) {
  90. checkedRef(ptr);
  91. checkedRelease(_ptr);
  92. _ptr = ptr;
  93. return *this; }
  94. /**
  95. * Overloaded member selection operator.
  96. * @return a pointer of the template class to the object.
  97. */
  98. inline T* operator->() const {
  99. assert (_ptr != (T*)NULL); return _ptr; }
  100. /**
  101. * Overloaded cast operator.
  102. * @return a pointer of the template class to the object.
  103. */
  104. inline operator T*() const {
  105. return _ptr; }
  106. /**
  107. * Static cast function.
  108. * @param smartRef a smartRef to cast from
  109. * @return a pointer to an object of this template class
  110. */
  111. template<class U>
  112. inline static T* staticCast(const daeSmartRef<U>& smartRef) {
  113. return static_cast<T*>(smartRef.cast()); }
  114. private:
  115. /* The pointer to the element which is being reference counted */
  116. T* _ptr;
  117. };
  118. #endif // __DAE_SMARTREF_H__