CLAutoBuffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //-----------------------------------------------------------------------------
  2. // (c) 2008 by Basler Vision Technologies
  3. // Section: Vision Components
  4. // Project: GenApi
  5. // Author: Fritz Dierks
  6. // $Header$
  7. //
  8. // License: This file is published under the license of the EMVA GenICam Standard Group.
  9. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  10. // If for some reason you are missing this file please contact the EMVA or visit the website
  11. // (http://www.genicam.org) for a full copy.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  17. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. // POSSIBILITY OF SUCH DAMAGE.
  24. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. \brief C++ class wrapping CLSerialAll and CLProtocolDriver
  28. */
  29. #ifndef CLPROTOCOL_CLAUTOBUFFER_H
  30. #define CLPROTOCOL_CLAUTOBUFFER_H
  31. namespace CLProtocol
  32. {
  33. /*! \internal
  34. \brief Simple helper class to manage automatic deletion of arrays to provide exception safety see std:auto_ptr but works only with arrays mainly used for strings. Do not use directly
  35. */
  36. template<class T>
  37. class CLAutoBuffer
  38. {
  39. public:
  40. explicit CLAutoBuffer(T* p = NULL)
  41. : m_p(p)
  42. {
  43. }
  44. CLAutoBuffer(CLAutoBuffer<T>& rhs)
  45. : m_p(rhs.Release())
  46. {
  47. }
  48. ~CLAutoBuffer()
  49. {
  50. DeleteSafe();
  51. }
  52. CLAutoBuffer<T>& operator= (T* p)
  53. {
  54. if (m_p != p)
  55. {
  56. DeleteSafe();
  57. m_p = p;
  58. }
  59. return *this;
  60. }
  61. CLAutoBuffer<T>& operator= (CLAutoBuffer<T>& rhs)
  62. {
  63. if (m_p != rhs.m_p)
  64. {
  65. DeleteSafe();
  66. m_p = rhs.Release();
  67. }
  68. return *this;
  69. }
  70. T* operator->() const
  71. {
  72. return m_p;
  73. }
  74. T* Get() const
  75. {
  76. return m_p;
  77. }
  78. T* Release()
  79. {
  80. T* p(m_p);
  81. m_p = NULL;
  82. return p;
  83. }
  84. private:
  85. void DeleteSafe()
  86. {
  87. if (m_p != NULL)
  88. {
  89. delete[] m_p;
  90. m_p = NULL;
  91. }
  92. }
  93. T* m_p; // the wrapped object pointer
  94. };
  95. }
  96. #endif // CLPROTOCOL_CLAUTOBUFFER_H