Home | History | Annotate | Download | only in util.smartptr.shared
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <memory>
     11 
     12 // shared_ptr
     13 
     14 // Example move-only deleter
     15 
     16 #ifndef DELETER_H
     17 #define DELETER_H
     18 
     19 #include <type_traits>
     20 #include <cassert>
     21 
     22 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
     23 #define DELETE_FUNCTION = delete
     24 #else
     25 #define DELETE_FUNCTION { assert(false); }
     26 #endif
     27 
     28 struct test_deleter_base
     29 {
     30     static int count;
     31     static int dealloc_count;
     32 };
     33 
     34 int test_deleter_base::count = 0;
     35 int test_deleter_base::dealloc_count = 0;
     36 
     37 template <class T>
     38 class test_deleter
     39     : public test_deleter_base
     40 {
     41     int state_;
     42 
     43 public:
     44 
     45     test_deleter() : state_(0) {++count;}
     46     explicit test_deleter(int s) : state_(s) {++count;}
     47     test_deleter(const test_deleter& d)
     48         : state_(d.state_) {++count;}
     49     ~test_deleter() {assert(state_ >= 0); --count; state_ = -1;}
     50 
     51     int state() const {return state_;}
     52     void set_state(int i) {state_ = i;}
     53 
     54     void operator()(T* p) {assert(state_ >= 0); ++dealloc_count; delete p;}
     55 
     56     test_deleter* operator&() const DELETE_FUNCTION;
     57 };
     58 
     59 template <class T>
     60 void
     61 swap(test_deleter<T>& x, test_deleter<T>& y)
     62 {
     63     test_deleter<T> t(std::move(x));
     64     x = std::move(y);
     65     y = std::move(t);
     66 }
     67 
     68 #endif  // DELETER_H
     69