[llvm] r203392 - [C++11] Now that we have C++11 and I've replaced the use of this
Chandler Carruth
chandlerc at gmail.com
Sun Mar 9 04:51:11 PDT 2014
Author: chandlerc
Date: Sun Mar 9 06:51:11 2014
New Revision: 203392
URL: http://llvm.org/viewvc/llvm-project?rev=203392&view=rev
Log:
[C++11] Now that we have C++11 and I've replaced the use of this
horrible smart pointer by std::unique_ptr and strict move semantics, rip
this out.
Removed:
llvm/trunk/include/llvm/ADT/polymorphic_ptr.h
llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp
Modified:
llvm/trunk/unittests/ADT/CMakeLists.txt
Removed: llvm/trunk/include/llvm/ADT/polymorphic_ptr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/polymorphic_ptr.h?rev=203391&view=auto
==============================================================================
--- llvm/trunk/include/llvm/ADT/polymorphic_ptr.h (original)
+++ llvm/trunk/include/llvm/ADT/polymorphic_ptr.h (removed)
@@ -1,115 +0,0 @@
-//===- 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 ? arg->clone() : 0) {}
- polymorphic_ptr(polymorphic_ptr &&arg) : ptr(arg.take()) {}
- ~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
Modified: llvm/trunk/unittests/ADT/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/CMakeLists.txt?rev=203392&r1=203391&r2=203392&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ADT/CMakeLists.txt Sun Mar 9 06:51:11 2014
@@ -40,7 +40,6 @@ set(ADTSources
TripleTest.cpp
TwineTest.cpp
VariadicFunctionTest.cpp
- polymorphic_ptr_test.cpp
)
add_llvm_unittest(ADTTests
Removed: llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp?rev=203391&view=auto
==============================================================================
--- llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp (original)
+++ llvm/trunk/unittests/ADT/polymorphic_ptr_test.cpp (removed)
@@ -1,125 +0,0 @@
-//===- 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 {
-
-struct S {
- S(int x) : x(x) {}
- S *clone() { return new S(*this); }
- int x;
-};
-
-// A function that forces the return of a copy.
-template <typename T>
-T dummy_copy(const T &arg) { return arg; }
-
-TEST(polymorphic_ptr_test, Basic) {
- 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((std::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);
-
- // Force copies and that everything survives.
- polymorphic_ptr<S> p3 = dummy_copy(polymorphic_ptr<S>(p));
- EXPECT_TRUE((bool)p3);
- EXPECT_FALSE(!p3);
- EXPECT_NE(s, &*p3);
- EXPECT_EQ(42, p3->x);
-
- // Force copies of null without trying to dereference anything.
- polymorphic_ptr<S> null_copy = dummy_copy(polymorphic_ptr<S>(null));
- EXPECT_FALSE((bool)null_copy);
- EXPECT_TRUE(!null_copy);
- EXPECT_EQ(null, null_copy);
-}
-
-struct Base {
- virtual ~Base() {}
- virtual Base *clone() = 0;
- virtual StringRef name() { return "Base"; }
-};
-
-struct DerivedA : Base {
- virtual DerivedA *clone() { return new DerivedA(); }
- virtual StringRef name() { return "DerivedA"; }
-};
-struct DerivedB : Base {
- virtual DerivedB *clone() { return new DerivedB(); }
- virtual StringRef name() { return "DerivedB"; }
-};
-
-TEST(polymorphic_ptr_test, Polymorphism) {
- polymorphic_ptr<Base> a(new DerivedA());
- polymorphic_ptr<Base> b(new DerivedB());
-
- EXPECT_EQ("DerivedA", a->name());
- EXPECT_EQ("DerivedB", b->name());
-
- polymorphic_ptr<Base> copy = dummy_copy(a);
- EXPECT_NE(a, copy);
- EXPECT_EQ("DerivedA", copy->name());
-
- copy = dummy_copy(b);
- EXPECT_NE(b, copy);
- EXPECT_EQ("DerivedB", copy->name());
-
- // Test creating a copy out of a temporary directly.
- copy = dummy_copy<polymorphic_ptr<Base> >(new DerivedA());
- EXPECT_NE(a, copy);
- EXPECT_EQ("DerivedA", copy->name());
-}
-
-}
More information about the llvm-commits
mailing list