Home | History | Annotate | Download | only in util.smartptr.ownerless
      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 // template <class T> struct owner_less;
     13 //
     14 // template <class T>
     15 // struct owner_less<shared_ptr<T> >
     16 //     : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
     17 // {
     18 //     typedef bool result_type;
     19 //     bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
     20 //     bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
     21 //     bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
     22 // };
     23 //
     24 // template <class T>
     25 // struct owner_less<weak_ptr<T> >
     26 //     : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
     27 // {
     28 //     typedef bool result_type;
     29 //     bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
     30 //     bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
     31 //     bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
     32 // };
     33 
     34 #include <memory>
     35 #include <cassert>
     36 
     37 int main()
     38 {
     39     const std::shared_ptr<int> p1(new int);
     40     const std::shared_ptr<int> p2 = p1;
     41     const std::shared_ptr<int> p3(new int);
     42     const std::weak_ptr<int> w1(p1);
     43     const std::weak_ptr<int> w2(p2);
     44     const std::weak_ptr<int> w3(p3);
     45 
     46     {
     47     typedef std::owner_less<std::shared_ptr<int> > CS;
     48     CS cs;
     49 
     50     assert(!cs(p1, p2));
     51     assert(!cs(p2, p1));
     52     assert(cs(p1 ,p3) || cs(p3, p1));
     53     assert(cs(p3, p1) == cs(p3, p2));
     54 
     55     assert(!cs(p1, w2));
     56     assert(!cs(p2, w1));
     57     assert(cs(p1, w3) || cs(p3, w1));
     58     assert(cs(p3, w1) == cs(p3, w2));
     59     }
     60     {
     61     typedef std::owner_less<std::weak_ptr<int> > CS;
     62     CS cs;
     63 
     64     assert(!cs(w1, w2));
     65     assert(!cs(w2, w1));
     66     assert(cs(w1, w3) || cs(w3, w1));
     67     assert(cs(w3, w1) == cs(w3, w2));
     68 
     69     assert(!cs(w1, p2));
     70     assert(!cs(w2, p1));
     71     assert(cs(w1, p3) || cs(w3, p1));
     72     assert(cs(w3, p1) == cs(w3, p2));
     73     }
     74 }
     75