Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:23

0001 #include "FWCore/Utilities/interface/value_ptr.h"
0002 
0003 #include <memory>
0004 #include <cassert>
0005 #include <atomic>
0006 
0007 class simple {
0008   int i;
0009 
0010 public:
0011   static std::atomic<int> count;
0012   static std::atomic<int> count1;
0013   simple() : i(0) {
0014     ++count;
0015     ++count1;
0016   }
0017   explicit simple(int j) : i(j) {
0018     ++count;
0019     ++count1;
0020   }
0021   simple(simple const& s) : i(s.i) {
0022     ++count;
0023     ++count1;
0024   }
0025   ~simple() { --count; }
0026   bool operator==(simple const& o) const { return i == o.i; }
0027   bool isSame(simple const& o) const { return &o == this; }
0028   simple& operator=(simple const& o) {
0029     simple temp(o);
0030     std::swap(*this, temp);
0031     return *this;
0032   }
0033 };
0034 
0035 std::atomic<int> simple::count{0};
0036 std::atomic<int> simple::count1{0};
0037 
0038 int main() {
0039   assert(simple::count == 0);
0040   {
0041     edm::value_ptr<simple> a(new simple(10));
0042     assert(simple::count == 1);
0043     edm::value_ptr<simple> b(a);
0044     assert(simple::count == 2);
0045 
0046     assert(*a == *b);
0047     assert(a->isSame(*b) == false);
0048   }  // a and b destroyed
0049   assert(simple::count == 0);
0050 
0051   {
0052     auto c = std::make_unique<simple>(11);
0053     auto d = std::make_unique<simple>(11);
0054     assert(c.get() != nullptr);
0055     assert(d.get() != nullptr);
0056     simple* pc = c.get();
0057     simple* pd = d.get();
0058 
0059     edm::value_ptr<simple> e(std::move(c));
0060     assert(c.get() == nullptr);
0061     assert(*d == *e);
0062     assert(e.operator->() == pc);
0063 
0064     edm::value_ptr<simple> f;
0065     if (f) {
0066       assert(0);
0067     } else {
0068     }
0069     f = std::move(d);
0070     assert(d.get() == nullptr);
0071     assert(*e == *f);
0072     assert(f.operator->() == pd);
0073     if (f) {
0074     } else {
0075       assert(0);
0076     }
0077 
0078     assert(simple::count == 2);
0079     assert(simple::count1 == 4);
0080   }
0081   assert(simple::count == 0);
0082   assert(simple::count1 == 4);
0083 }