Home | History | Annotate | Download | only in util.smartptr.shared.create
      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 // template<class T, class A, class... Args>
     15 //    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
     16 
     17 #include <memory>
     18 #include <new>
     19 #include <cstdlib>
     20 #include <cassert>
     21 #include "test_allocator.h"
     22 #include "min_allocator.h"
     23 
     24 int new_count = 0;
     25 
     26 struct A
     27 {
     28     static int count;
     29 
     30     A(int i, char c) : int_(i), char_(c) {++count;}
     31     A(const A& a)
     32         : int_(a.int_), char_(a.char_)
     33         {++count;}
     34     ~A() {--count;}
     35 
     36     int get_int() const {return int_;}
     37     char get_char() const {return char_;}
     38 private:
     39     int int_;
     40     char char_;
     41 };
     42 
     43 int A::count = 0;
     44 
     45 int main()
     46 {
     47     {
     48     int i = 67;
     49     char c = 'e';
     50     std::shared_ptr<A> p = std::allocate_shared<A>(test_allocator<A>(54), i, c);
     51     assert(test_allocator<A>::alloc_count == 1);
     52     assert(A::count == 1);
     53     assert(p->get_int() == 67);
     54     assert(p->get_char() == 'e');
     55     }
     56     assert(A::count == 0);
     57     assert(test_allocator<A>::alloc_count == 0);
     58 #if __cplusplus >= 201103L
     59     {
     60     int i = 67;
     61     char c = 'e';
     62     std::shared_ptr<A> p = std::allocate_shared<A>(min_allocator<void>(), i, c);
     63     assert(A::count == 1);
     64     assert(p->get_int() == 67);
     65     assert(p->get_char() == 'e');
     66     }
     67     assert(A::count == 0);
     68     {
     69     int i = 68;
     70     char c = 'f';
     71     std::shared_ptr<A> p = std::allocate_shared<A>(bare_allocator<void>(), i, c);
     72     assert(A::count == 1);
     73     assert(p->get_int() == 68);
     74     assert(p->get_char() == 'f');
     75     }
     76     assert(A::count == 0);
     77 #endif
     78 }
     79