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 struct test_deleter_base
     23 {
     24     static int count;
     25     static int dealloc_count;
     26 };
     27 
     28 int test_deleter_base::count = 0;
     29 int test_deleter_base::dealloc_count = 0;
     30 
     31 template <class T>
     32 class test_deleter
     33     : public test_deleter_base
     34 {
     35     int state_;
     36 
     37 public:
     38 
     39     test_deleter() : state_(0) {++count;}
     40     explicit test_deleter(int s) : state_(s) {++count;}
     41     test_deleter(const test_deleter& d)
     42         : state_(d.state_) {++count;}
     43     ~test_deleter() {assert(state_ >= 0); --count; state_ = -1;}
     44 
     45     int state() const {return state_;}
     46     void set_state(int i) {state_ = i;}
     47 
     48     void operator()(T* p) {assert(state_ >= 0); ++dealloc_count; delete p;}
     49 };
     50 
     51 template <class T>
     52 void
     53 swap(test_deleter<T>& x, test_deleter<T>& y)
     54 {
     55     test_deleter<T> t(std::move(x));
     56     x = std::move(y);
     57     y = std::move(t);
     58 }
     59 
     60 #endif  // DELETER_H
     61