Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.8 KiB

  1. //===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines and implements the OwningPtr class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_OWNINGPTR_H
  14. #define LLVM_ADT_OWNINGPTR_H
  15. #include "llvm/Support/Compiler.h"
  16. #include <cassert>
  17. #include <cstddef>
  18. namespace llvm {
  19. /// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
  20. /// guarantees deletion of the object pointed to, either on destruction of the
  21. /// OwningPtr or via an explicit reset(). Once created, ownership of the
  22. /// pointee object can be taken away from OwningPtr by using the take method.
  23. template<class T>
  24. class OwningPtr {
  25. OwningPtr(OwningPtr const &) LLVM_DELETED_FUNCTION;
  26. OwningPtr &operator=(OwningPtr const &) LLVM_DELETED_FUNCTION;
  27. T *Ptr;
  28. public:
  29. explicit OwningPtr(T *P = 0) : Ptr(P) {}
  30. #if LLVM_HAS_RVALUE_REFERENCES
  31. OwningPtr(OwningPtr &&Other) : Ptr(Other.take()) {}
  32. OwningPtr &operator=(OwningPtr &&Other) {
  33. reset(Other.take());
  34. return *this;
  35. }
  36. #endif
  37. ~OwningPtr() {
  38. delete Ptr;
  39. }
  40. /// reset - Change the current pointee to the specified pointer. Note that
  41. /// calling this with any pointer (including a null pointer) deletes the
  42. /// current pointer.
  43. void reset(T *P = 0) {
  44. if (P == Ptr) return;
  45. T *Tmp = Ptr;
  46. Ptr = P;
  47. delete Tmp;
  48. }
  49. /// take - Reset the owning pointer to null and return its pointer. This does
  50. /// not delete the pointer before returning it.
  51. T *take() {
  52. T *Tmp = Ptr;
  53. Ptr = 0;
  54. return Tmp;
  55. }
  56. T &operator*() const {
  57. assert(Ptr && "Cannot dereference null pointer");
  58. return *Ptr;
  59. }
  60. T *operator->() const { return Ptr; }
  61. T *get() const { return Ptr; }
  62. operator bool() const { return Ptr != 0; }
  63. bool operator!() const { return Ptr == 0; }
  64. void swap(OwningPtr &RHS) {
  65. T *Tmp = RHS.Ptr;
  66. RHS.Ptr = Ptr;
  67. Ptr = Tmp;
  68. }
  69. };
  70. template<class T>
  71. inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
  72. a.swap(b);
  73. }
  74. /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
  75. /// functionality as OwningPtr, except that it works for array types.
  76. template<class T>
  77. class OwningArrayPtr {
  78. OwningArrayPtr(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
  79. OwningArrayPtr &operator=(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
  80. T *Ptr;
  81. public:
  82. explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
  83. #if LLVM_HAS_RVALUE_REFERENCES
  84. OwningArrayPtr(OwningArrayPtr &&Other) : Ptr(Other.take()) {}
  85. OwningArrayPtr &operator=(OwningArrayPtr &&Other) {
  86. reset(Other.take());
  87. return *this;
  88. }
  89. #endif
  90. ~OwningArrayPtr() {
  91. delete [] Ptr;
  92. }
  93. /// reset - Change the current pointee to the specified pointer. Note that
  94. /// calling this with any pointer (including a null pointer) deletes the
  95. /// current pointer.
  96. void reset(T *P = 0) {
  97. if (P == Ptr) return;
  98. T *Tmp = Ptr;
  99. Ptr = P;
  100. delete [] Tmp;
  101. }
  102. /// take - Reset the owning pointer to null and return its pointer. This does
  103. /// not delete the pointer before returning it.
  104. T *take() {
  105. T *Tmp = Ptr;
  106. Ptr = 0;
  107. return Tmp;
  108. }
  109. T &operator[](std::ptrdiff_t i) const {
  110. assert(Ptr && "Cannot dereference null pointer");
  111. return Ptr[i];
  112. }
  113. T *get() const { return Ptr; }
  114. operator bool() const { return Ptr != 0; }
  115. bool operator!() const { return Ptr == 0; }
  116. void swap(OwningArrayPtr &RHS) {
  117. T *Tmp = RHS.Ptr;
  118. RHS.Ptr = Ptr;
  119. Ptr = Tmp;
  120. }
  121. };
  122. template<class T>
  123. inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
  124. a.swap(b);
  125. }
  126. } // end namespace llvm
  127. #endif