LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
polymorphic_ptr.h
Go to the documentation of this file.
1 //===- llvm/ADT/polymorphic_ptr.h - Smart copyable owned ptr ----*- 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 /// \file
10 /// This file provides a polymorphic_ptr class template. See the class comments
11 /// for details about this API, its intended use cases, etc.
12 ///
13 /// The primary motivation here is to work around the necessity of copy
14 /// semantics in C++98. This is typically used where any actual copies are
15 /// incidental or unnecessary. As a consequence, it is expected to cease to be
16 /// useful and be removed when we can directly rely on move-only types.
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_ADT_POLYMORPHIC_PTR_H
21 #define LLVM_ADT_POLYMORPHIC_PTR_H
22 
23 #include "llvm/Support/Compiler.h"
24 
25 namespace llvm {
26 
27 /// \brief An owning, copyable polymorphic smart pointer.
28 ///
29 /// This pointer exists to provide copyable owned smart pointer. Rather than
30 /// shared ownership semantics, it has unique ownership semantics and deep copy
31 /// semantics. It is copyable by requiring that the underlying type exposes
32 /// a method which can produce a (heap allocated) clone.
33 ///
34 /// Note that in almost all scenarios use of this could be avoided if we could
35 /// build move-only containers of a std::unique_ptr, but until then this
36 /// provides an effective way to place polymorphic objects in a container.
37 template <typename T> class polymorphic_ptr {
38  T *ptr;
39 
40 public:
41  polymorphic_ptr(T *ptr = 0) : ptr(ptr) {}
42  polymorphic_ptr(const polymorphic_ptr &arg) : ptr(arg ? arg->clone() : 0) {}
43 #if LLVM_HAS_RVALUE_REFERENCES
44  polymorphic_ptr(polymorphic_ptr &&arg) : ptr(arg.take()) {}
45 #endif
46  ~polymorphic_ptr() { delete ptr; }
47 
49  swap(arg);
50  return *this;
51  }
53  if (arg != ptr) {
54  delete ptr;
55  ptr = arg;
56  }
57  return *this;
58  }
59 
60  T &operator*() const { return *ptr; }
61  T *operator->() const { return ptr; }
62  LLVM_EXPLICIT operator bool() const { return ptr != 0; }
63  bool operator!() const { return ptr == 0; }
64 
65  T *get() const { return ptr; }
66 
67  T *take() {
68  T *tmp = ptr;
69  ptr = 0;
70  return tmp;
71  }
72 
73  void swap(polymorphic_ptr &arg) {
74  T *tmp = ptr;
75  ptr = arg.ptr;
76  arg.ptr = tmp;
77  }
78 };
79 
80 template <typename T>
82  lhs.swap(rhs);
83 }
84 
85 template <typename T, typename U>
86 bool operator==(const polymorphic_ptr<T> &lhs, const polymorphic_ptr<U> &rhs) {
87  return lhs.get() == rhs.get();
88 }
89 
90 template <typename T, typename U>
91 bool operator!=(const polymorphic_ptr<T> &lhs, const polymorphic_ptr<U> &rhs) {
92  return lhs.get() != rhs.get();
93 }
94 
95 template <typename T, typename U>
96 bool operator==(const polymorphic_ptr<T> &lhs, U *rhs) {
97  return lhs.get() == rhs;
98 }
99 
100 template <typename T, typename U>
101 bool operator!=(const polymorphic_ptr<T> &lhs, U *rhs) {
102  return lhs.get() != rhs;
103 }
104 
105 template <typename T, typename U>
106 bool operator==(T *lhs, const polymorphic_ptr<U> &rhs) {
107  return lhs == rhs.get();
108 }
109 
110 template <typename T, typename U>
111 bool operator!=(T *lhs, const polymorphic_ptr<U> &rhs) {
112  return lhs != rhs.get();
113 }
114 
115 }
116 
117 #endif
An owning, copyable polymorphic smart pointer.
polymorphic_ptr & operator=(T *arg)
void swap(OwningPtr< T > &a, OwningPtr< T > &b)
Definition: OwningPtr.h:85
void swap(polymorphic_ptr &arg)
polymorphic_ptr & operator=(polymorphic_ptr arg)
polymorphic_ptr(const polymorphic_ptr &arg)
bool operator!() const
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1686
#define LLVM_EXPLICIT
Expands to explicit on compilers which support explicit conversion operators. Otherwise expands to no...
Definition: Compiler.h:381
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1684