icptrholder_cxx.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file icptrholder_cxx.h
  3. * @author wyau (08/29/02)
  4. * @brief C++ template classes for managing C++ pointers returned by
  5. * VComponent::get_..._component, VComponent::get_..._property,
  6. * ICalProperty::get_..._value.
  7. *
  8. * @remarks VComponent::get... functions returns a C++ oject that wraps the
  9. * libical implementation. It is important to note that the wrapped
  10. * implementation still belongs to the original component. To stop memory leak,
  11. * caller must delete the pointer. However, the destructor will call the
  12. * appropriate free function. eg. ~VComponent calls icalcomponent_free(imp).
  13. *
  14. * As stated previously, imp stil belongs to the original component. To avoid
  15. * freeing the wrapped "imp", caller must set the "imp" to null before deleting
  16. * the pointer.
  17. *
  18. * The template class relieves the burden of memory management when used as a
  19. * stack based object. The class holds a pointer to the C++ Wrapper.
  20. * The destructor set the imp to null before deleting the pointer.
  21. *
  22. * Each C++ Wrapper instantiates a template class in it's corresponding .h file.
  23. *
  24. * Usage example:
  25. * VComponentTmpPtr p;// VComponentTmpPtr is an instantiation of this template
  26. * for (p=component.get_first_component; p!= 0; p=component.get_next_component) {
  27. *
  28. * (C) COPYRIGHT 2001, Critical Path
  29. This program is free software; you can redistribute it and/or modify
  30. it under the terms of either:
  31. The LGPL as published by the Free Software Foundation, version
  32. 2.1, available at: http://www.gnu.org/licenses/lgpl-2.1.html
  33. Or:
  34. The Mozilla Public License Version 1.0. You may obtain a copy of
  35. the License at http://www.mozilla.org/MPL/
  36. */
  37. #ifndef ICPTRHOLDER_CXX_H
  38. #define ICPTRHOLDER_CXX_H
  39. #include <cassert>
  40. template < class T > class ICPointerHolder {
  41. public:
  42. ICPointerHolder()
  43. : ptr(0)
  44. {
  45. }
  46. ICPointerHolder(T *p)
  47. : ptr(p)
  48. {
  49. }
  50. // copy constructor to support assignment
  51. ICPointerHolder(const ICPointerHolder &ip)
  52. : ptr(ip.ptr)
  53. {
  54. // We need to transfer ownership of ptr to this object by setting
  55. // ip's ptr to null. Otherwise, ptr will de deleted twice.
  56. // const ugliness requires us to do the const_cast.
  57. ICPointerHolder *ipp = const_cast < ICPointerHolder * >(&ip);
  58. ipp->ptr = 0;
  59. };
  60. ~ICPointerHolder()
  61. {
  62. release();
  63. }
  64. ICPointerHolder & operator=(T *p)
  65. {
  66. this->release();
  67. ptr = p;
  68. return *this;
  69. }
  70. ICPointerHolder &operator=(ICPointerHolder &p)
  71. {
  72. this->release();
  73. ptr = p.ptr; // this transfer ownership of the pointer
  74. p.ptr = 0; // set it to null so the pointer won't get delete twice.
  75. return *this;
  76. }
  77. int operator!=(T *p)
  78. {
  79. return (ptr != p);
  80. }
  81. int operator==(T *p)
  82. {
  83. return (ptr == p);
  84. }
  85. operator T *() const
  86. {
  87. return ptr;
  88. }
  89. T *operator->() const
  90. {
  91. assert(ptr);
  92. return ptr;
  93. }
  94. T &operator*()
  95. {
  96. assert(ptr);
  97. return *ptr;
  98. }
  99. private:
  100. void release()
  101. {
  102. if (ptr != 0) {
  103. ptr->detach();
  104. delete ptr;
  105. ptr = 0;
  106. }
  107. }
  108. T *ptr;
  109. };
  110. #endif