summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2013-11-09 04:06:02 +0000
committerChandler Carruth <chandlerc@gmail.com>2013-11-09 04:06:02 +0000
commitaec427786be4f4dd0c8ecc0aa17899e6c836e45d (patch)
treecb5b1da86d2d4d32929e7baecaabbdde7d300dfc
parent332cbf1d4503fcad3b5f3bf6ff73889feff03ad7 (diff)
downloadexternal_llvm-aec427786be4f4dd0c8ecc0aa17899e6c836e45d.zip
external_llvm-aec427786be4f4dd0c8ecc0aa17899e6c836e45d.tar.gz
external_llvm-aec427786be4f4dd0c8ecc0aa17899e6c836e45d.tar.bz2
Add a polymorphic_ptr<T> smart pointer data type. It's a somewhat silly
unique ownership smart pointer which is *deep* copyable by assuming it can call a T::clone() method to allocate a copy of the owned data. This is mostly useful with containers or other collections of uniquely owned data in C++98 where they *might* copy. With C++11 we can likely remove this in favor of move-only types and containers wrapped around those types. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194315 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/ADT/polymorphic_ptr.h117
-rw-r--r--unittests/ADT/CMakeLists.txt1
-rw-r--r--unittests/ADT/polymorphic_ptr_test.cpp71
3 files changed, 189 insertions, 0 deletions
diff --git a/include/llvm/ADT/polymorphic_ptr.h b/include/llvm/ADT/polymorphic_ptr.h
new file mode 100644
index 0000000..6114b65
--- /dev/null
+++ b/include/llvm/ADT/polymorphic_ptr.h
@@ -0,0 +1,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:
+ explicit 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
diff --git a/unittests/ADT/CMakeLists.txt b/unittests/ADT/CMakeLists.txt
index 2125657..8ad303a 100644
--- a/unittests/ADT/CMakeLists.txt
+++ b/unittests/ADT/CMakeLists.txt
@@ -35,6 +35,7 @@ set(ADTSources
TripleTest.cpp
TwineTest.cpp
VariadicFunctionTest.cpp
+ polymorphic_ptr_test.cpp
)
# They cannot be compiled on MSVC9 due to its bug.
diff --git a/unittests/ADT/polymorphic_ptr_test.cpp b/unittests/ADT/polymorphic_ptr_test.cpp
new file mode 100644
index 0000000..e1e9c42
--- /dev/null
+++ b/unittests/ADT/polymorphic_ptr_test.cpp
@@ -0,0 +1,71 @@
+//===- llvm/unittest/ADT/polymorphic_ptr.h - polymorphic_ptr<T> tests -----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/ADT/polymorphic_ptr.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(polymorphic_ptr_test, Basic) {
+ struct S {
+ S(int x) : x(x) {}
+ int x;
+ };
+
+ polymorphic_ptr<S> null;
+ EXPECT_FALSE((bool)null);
+ EXPECT_TRUE(!null);
+ EXPECT_EQ((S*)0, null.get());
+
+ S *s = new S(42);
+ polymorphic_ptr<S> p(s);
+ EXPECT_TRUE((bool)p);
+ EXPECT_FALSE(!p);
+ EXPECT_TRUE(p != null);
+ EXPECT_FALSE(p == null);
+ EXPECT_TRUE(p == s);
+ EXPECT_TRUE(s == p);
+ EXPECT_FALSE(p != s);
+ EXPECT_FALSE(s != p);
+ EXPECT_EQ(s, &*p);
+ EXPECT_EQ(s, p.operator->());
+ EXPECT_EQ(s, p.get());
+ EXPECT_EQ(42, p->x);
+
+ EXPECT_EQ(s, p.take());
+ EXPECT_FALSE((bool)p);
+ EXPECT_TRUE(!p);
+ p = s;
+ EXPECT_TRUE((bool)p);
+ EXPECT_FALSE(!p);
+ EXPECT_EQ(s, &*p);
+ EXPECT_EQ(s, p.operator->());
+ EXPECT_EQ(s, p.get());
+ EXPECT_EQ(42, p->x);
+
+ polymorphic_ptr<S> p2((llvm_move(p)));
+ EXPECT_FALSE((bool)p);
+ EXPECT_TRUE(!p);
+ EXPECT_TRUE((bool)p2);
+ EXPECT_FALSE(!p2);
+ EXPECT_EQ(s, &*p2);
+
+ using std::swap;
+ swap(p, p2);
+ EXPECT_TRUE((bool)p);
+ EXPECT_FALSE(!p);
+ EXPECT_EQ(s, &*p);
+ EXPECT_FALSE((bool)p2);
+ EXPECT_TRUE(!p2);
+}
+
+}