/win32/shellext/auto_buffer.h

https://bitbucket.org/tortoisehg/hgtk/ · C Header · 91 lines · 49 code · 19 blank · 23 comment · 3 complexity · fdb4a8d4a8fced6ffcd04ea6b68ed64b MD5 · raw file

  1. // TortoiseSVN - a Windows shell extension for easy version control
  2. // Copyright (C) 2009 - TortoiseSVN
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software Foundation,
  13. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. //
  15. #pragma once
  16. /**
  17. * A simplified analog to std::auto_ptr<> that encapsulates
  18. * an array allocated dynamically via new[].
  19. *
  20. * Use this where you could not use a std::auto_ptr<> (works
  21. * for single elements only) nor a std::vector<> (no guarantees
  22. * w.r.t. to internal organization, i.e. no access to mem buffer).
  23. */
  24. template<class T>
  25. class auto_buffer
  26. {
  27. private:
  28. T* buffer;
  29. /// no copy nor assignment
  30. auto_buffer(const auto_buffer&);
  31. auto_buffer& operator=(const auto_buffer&);
  32. public:
  33. explicit auto_buffer (size_t size = 0) throw()
  34. : buffer (size == 0 ? NULL : new T[size])
  35. {
  36. }
  37. ~auto_buffer()
  38. {
  39. delete[] buffer;
  40. }
  41. operator T*() const throw()
  42. {
  43. return buffer;
  44. }
  45. operator void*() const throw()
  46. {
  47. return buffer;
  48. }
  49. operator bool() const throw()
  50. {
  51. return buffer != NULL;
  52. }
  53. T* operator->() const throw()
  54. {
  55. return buffer;
  56. }
  57. T *get() const throw()
  58. {
  59. return buffer;
  60. }
  61. T* release() throw()
  62. {
  63. T* temp = buffer;
  64. buffer = NULL;
  65. return temp;
  66. }
  67. void reset (size_t newSize = 0)
  68. {
  69. delete[] buffer;
  70. buffer = (newSize == 0 ? NULL : new T[newSize]);
  71. }
  72. };