aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT/polymorphic_ptr.h
blob: a1687474ef41c5c2a7ab636b40ee78ac36d26e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===- llvm/ADT/polymorphic_ptr.h - Smart copyable owned ptr ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
/// \file
/// This file provides a polymorphic_ptr class template. See the class comments
/// for details about this API, its intended use cases, etc.
///
/// The primary motivation here is to work around the necessity of copy
/// semantics in C++98. This is typically used where any actual copies are
/// incidental or unnecessary. As a consequence, it is expected to cease to be
/// useful and be removed when we can directly rely on move-only types.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_POLYMORPHIC_PTR_H
#define LLVM_ADT_POLYMORPHIC_PTR_H

#include "llvm/Support/Compiler.h"

namespace llvm {

/// \brief An owning, copyable polymorphic smart pointer.
///
/// This pointer exists to provide copyable owned smart pointer. Rather than
/// shared ownership semantics, it has unique ownership semantics and deep copy
/// semantics. It is copyable by requiring that the underlying type exposes
/// a method which can produce a (heap allocated) clone.
///
/// Note that in almost all scenarios use of this could be avoided if we could
/// build move-only containers of a std::unique_ptr, but until then this
/// provides an effective way to place polymorphic objects in a container.
template <typename T> class polymorphic_ptr {
  T *ptr;

public:
  polymorphic_ptr(T *ptr = 0) : ptr(ptr) {}
  polymorphic_ptr(const polymorphic_ptr &arg) : ptr(arg->clone()) {}
#if LLVM_HAS_RVALUE_REFERENCES
  polymorphic_ptr(polymorphic_ptr &&arg) : ptr(arg.take()) {}
#endif
  ~polymorphic_ptr() { delete ptr; }

  polymorphic_ptr &operator=(polymorphic_ptr arg) {
    swap(arg);
    return *this;
  }
  polymorphic_ptr &operator=(T *arg) {
    if (arg != ptr) {
      delete ptr;
      ptr = arg;
    }
    return *this;
  }

  T &operator*() const { return *ptr; }
  T *operator->() const { return ptr; }
  LLVM_EXPLICIT operator bool() const { return ptr != 0; }
  bool operator!() const { return ptr == 0; }

  T *get() const { return ptr; }

  T *take() {
    T *tmp = ptr;
    ptr = 0;
    return tmp;
  }

  void swap(polymorphic_ptr &arg) {
    T *tmp = ptr;
    ptr = arg.ptr;
    arg.ptr = tmp;
  }
};

template <typename T>
void swap(polymorphic_ptr<T> &lhs, polymorphic_ptr<T> &rhs) {
  lhs.swap(rhs);
}

template <typename T, typename U>
bool operator==(const polymorphic_ptr<T> &lhs, const polymorphic_ptr<U> &rhs) {
  return lhs.get() == rhs.get();
}

template <typename T, typename U>
bool operator!=(const polymorphic_ptr<T> &lhs, const polymorphic_ptr<U> &rhs) {
  return lhs.get() != rhs.get();
}

template <typename T, typename U>
bool operator==(const polymorphic_ptr<T> &lhs, U *rhs) {
  return lhs.get() == rhs;
}

template <typename T, typename U>
bool operator!=(const polymorphic_ptr<T> &lhs, U *rhs) {
  return lhs.get() != rhs;
}

template <typename T, typename U>
bool operator==(T *lhs, const polymorphic_ptr<U> &rhs) {
  return lhs == rhs.get();
}

template <typename T, typename U>
bool operator!=(T *lhs, const polymorphic_ptr<U> &rhs) {
  return lhs != rhs.get();
}

}

#endif