clone_ptr.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef UTILSPP_CLONEPTR_HPP
  24. #define UTILSPP_CLONEPTR_HPP
  25. #include <cassert>
  26. #include <stdexcept>
  27. namespace utilspp
  28. {
  29. // This class is meant to manage a pointer. This class will
  30. // ensure that when we go out of scope, it will delete the
  31. // pointer.
  32. //
  33. // However, contrary to the std::unique_ptr, instead of
  34. // transfering the ownership on copy construction, it clones
  35. // the content. This means that we can have STL containers
  36. // that uses that class for managing the pointers.
  37. //
  38. // So, it means that the class we stores, needs a "clone"
  39. // member.
  40. template <typename T>
  41. class clone_ptr
  42. {
  43. public:
  44. clone_ptr() : value_(NULL) {}
  45. // This constructor takes ownership of the pointer.
  46. //
  47. // Note that it isn't explicit. This might be a
  48. // problem.
  49. clone_ptr(T * value) : value_(value) {}
  50. ~clone_ptr() {if (value_) delete value_;}
  51. // This is the default constructor that takes his
  52. // value from cloning the content of the other
  53. // clone_ptr.
  54. clone_ptr(const clone_ptr & other)
  55. {value_ = other->clone();}
  56. T * operator->()
  57. {
  58. if (value_)
  59. return value_;
  60. throw std::runtime_error("using a null clone_ptr");
  61. }
  62. const T * operator->() const
  63. {
  64. assert(value_);
  65. return value_;
  66. }
  67. T * get() { return value_; }
  68. const T * get() const { return value_; }
  69. // This just releases the pointer. It means that the
  70. // pointer is no longer owned by the smart pointer.
  71. T * release()
  72. {
  73. T * r = value_;
  74. value_ = NULL;
  75. return r;
  76. }
  77. private:
  78. T * value_;
  79. };
  80. }
  81. #endif