2
0

UdrCppExample.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * The contents of this file are subject to the Initial
  3. * Developer's Public License Version 1.0 (the "License");
  4. * you may not use this file except in compliance with the
  5. * License. You may obtain a copy of the License at
  6. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  7. *
  8. * Software distributed under the License is distributed AS IS,
  9. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing rights
  11. * and limitations under the License.
  12. *
  13. * The Original Code was created by Adriano dos Santos Fernandes
  14. * for the Firebird Open Source RDBMS project.
  15. *
  16. * Copyright (c) 2015 Adriano dos Santos Fernandes <[email protected]>
  17. * and all contributors signed below.
  18. *
  19. * All Rights Reserved.
  20. * Contributor(s): ______________________________________.
  21. */
  22. #ifndef UDR_CPP_EXAMPLE_H
  23. #define UDR_CPP_EXAMPLE_H
  24. #define FB_UDR_STATUS_TYPE ::Firebird::ThrowStatusWrapper
  25. #include "ibase.h"
  26. #include "firebird/UdrCppEngine.h"
  27. #include <assert.h>
  28. #include <stdio.h>
  29. namespace
  30. {
  31. template <typename T>
  32. class AutoReleaseClear
  33. {
  34. public:
  35. static void clear(T* ptr)
  36. {
  37. if (ptr)
  38. ptr->release();
  39. }
  40. };
  41. template <typename T>
  42. class AutoDisposeClear
  43. {
  44. public:
  45. static void clear(T* ptr)
  46. {
  47. if (ptr)
  48. ptr->dispose();
  49. }
  50. };
  51. template <typename T>
  52. class AutoDeleteClear
  53. {
  54. public:
  55. static void clear(T* ptr)
  56. {
  57. delete ptr;
  58. }
  59. };
  60. template <typename T>
  61. class AutoArrayDeleteClear
  62. {
  63. public:
  64. static void clear(T* ptr)
  65. {
  66. delete [] ptr;
  67. }
  68. };
  69. template <typename T, typename Clear>
  70. class AutoImpl
  71. {
  72. public:
  73. AutoImpl<T, Clear>(T* aPtr = NULL)
  74. : ptr(aPtr)
  75. {
  76. }
  77. ~AutoImpl()
  78. {
  79. Clear::clear(ptr);
  80. }
  81. AutoImpl<T, Clear>& operator =(T* aPtr)
  82. {
  83. Clear::clear(ptr);
  84. ptr = aPtr;
  85. return *this;
  86. }
  87. operator T*()
  88. {
  89. return ptr;
  90. }
  91. operator const T*() const
  92. {
  93. return ptr;
  94. }
  95. bool operator !() const
  96. {
  97. return !ptr;
  98. }
  99. bool hasData() const
  100. {
  101. return ptr != NULL;
  102. }
  103. T* operator ->()
  104. {
  105. return ptr;
  106. }
  107. T* release()
  108. {
  109. T* tmp = ptr;
  110. ptr = NULL;
  111. return tmp;
  112. }
  113. void reset(T* aPtr = NULL)
  114. {
  115. if (aPtr != ptr)
  116. {
  117. Clear::clear(ptr);
  118. ptr = aPtr;
  119. }
  120. }
  121. private:
  122. // not implemented
  123. AutoImpl<T, Clear>(AutoImpl<T, Clear>&);
  124. void operator =(AutoImpl<T, Clear>&);
  125. private:
  126. T* ptr;
  127. };
  128. template <typename T> class AutoDispose : public AutoImpl<T, AutoDisposeClear<T> >
  129. {
  130. public:
  131. AutoDispose(T* ptr = NULL)
  132. : AutoImpl<T, AutoDisposeClear<T> >(ptr)
  133. {
  134. }
  135. };
  136. template <typename T> class AutoRelease : public AutoImpl<T, AutoReleaseClear<T> >
  137. {
  138. public:
  139. AutoRelease(T* ptr = NULL)
  140. : AutoImpl<T, AutoReleaseClear<T> >(ptr)
  141. {
  142. }
  143. };
  144. template <typename T> class AutoDelete : public AutoImpl<T, AutoDeleteClear<T> >
  145. {
  146. public:
  147. AutoDelete(T* ptr = NULL)
  148. : AutoImpl<T, AutoDeleteClear<T> >(ptr)
  149. {
  150. }
  151. };
  152. template <typename T> class AutoArrayDelete : public AutoImpl<T, AutoArrayDeleteClear<T> >
  153. {
  154. public:
  155. AutoArrayDelete(T* ptr = NULL)
  156. : AutoImpl<T, AutoArrayDeleteClear<T> >(ptr)
  157. {
  158. }
  159. };
  160. }
  161. #endif // UDR_CPP_EXAMPLE_H