Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-12 03:09:56

0001 #include "TrackingTools/TrajectoryState/interface/ProxyBase11.h"
0002 
0003 #include <iostream>
0004 
0005 struct A {
0006   virtual ~A() {}
0007   virtual std::shared_ptr<A> clone() const = 0;
0008 
0009   template <typename T, typename... Args>
0010   static std::shared_ptr<A> build(Args &&...args) {
0011     return std::allocate_shared<T>(std::allocator<T>(), std::forward<Args>(args)...);
0012   }
0013 
0014   template <typename T, typename... Args>
0015   static std::shared_ptr<A> churn(Args &&...args) {
0016     return std::allocate_shared<T>(churn_allocator<T>(), std::forward<Args>(args)...);
0017   }
0018 };
0019 template <class T>
0020 struct ACloned : public A {
0021   std::shared_ptr<A> clone() const { return std::allocate_shared<T>(std::allocator<T>(), *this); }
0022 };
0023 struct B final : public A {
0024   ~B() { std::cout << "D B " << this << std::endl; }
0025   explicit B(int) { std::cout << "C B " << this << std::endl; }
0026   std::shared_ptr<A> clone() const { return build<B>(*this); }
0027 };
0028 struct C final : public A {
0029   ~C() { std::cout << "D C " << this << std::endl; }
0030   explicit C(int, float) { std::cout << "C C " << this << std::endl; }
0031   std::shared_ptr<A> clone() const { return build<C>(*this); }
0032 };
0033 
0034 struct BB final : public A {
0035   ~BB() { std::cout << "D BB " << this << std::endl; }
0036   explicit BB(int) { std::cout << "C BB " << this << std::endl; }
0037   std::shared_ptr<A> clone() const { return churn<BB>(*this); }
0038 };
0039 struct CC final : public A {
0040   ~CC() { std::cout << "D CC " << this << std::endl; }
0041   explicit CC(int, float) { std::cout << "C CC " << this << std::endl; }
0042   std::shared_ptr<A> clone() const { return churn<CC>(*this); }
0043 };
0044 
0045 using Proxy = ProxyBase11<A>;
0046 
0047 void astd(int k) {
0048   std::cout << "\nstd allocator\n" << std::endl;
0049 
0050   using PA = Proxy;
0051   //  using PB = std::shared_ptr<B>;
0052   // using PC = std::shared_ptr<C>;
0053 
0054   PA b = A::build<B>(3);
0055   PA c = A::build<C>(3, -2.3);
0056   std::cout << "more " << std::endl;
0057   PA b1 = A::build<B>(3);
0058   PA c1 = A::build<C>(3, -2.3);
0059   if (k < 3) {
0060     b1.reset();
0061     c.reset();
0062     std::cout << "churn " << std::endl;
0063     b1 = b.data().clone();
0064     c = c1.data().clone();
0065     b1.reset();
0066     c.reset();
0067   }
0068 
0069   std::cout << b.references() << ' ' << &b.data() << ' ';
0070   std::cout << &b.unsharedData() << std::endl;
0071   c = b;
0072   std::cout << b.references() << ' ' << &b.data() << ' ';
0073   std::cout << &b.unsharedData() << std::endl;
0074   std::cout << b.references() << ' ' << &b.data() << ' ';
0075   std::cout << &b.unsharedData() << std::endl;
0076   std::cout << c.references() << ' ' << &c.data() << ' ';
0077   std::cout << &c.unsharedData() << std::endl;
0078 
0079   std::cout << "end " << std::endl;
0080 }
0081 
0082 void achurn(int k) {
0083   std::cout << "\nchurn allocator\n" << std::endl;
0084 
0085   using PA = Proxy;
0086   //  using PB = std::shared_ptr<B>;
0087   // using PC = std::shared_ptr<C>;
0088 
0089   PA b = A::churn<BB>(3);
0090   PA c = A::churn<CC>(3, -2.3);
0091   std::cout << "more " << std::endl;
0092   PA b1 = A::churn<BB>(3);
0093   PA c1 = A::churn<CC>(3, -2.3);
0094   if (k < 3) {
0095     b1.reset();
0096     c.reset();
0097     std::cout << "churn " << std::endl;
0098     b1 = b.data().clone();
0099     c = c1.data().clone();
0100     b1.reset();
0101     c.reset();
0102   }
0103 
0104   std::cout << b.references() << ' ' << &b.data() << ' ';
0105   std::cout << &b.unsharedData() << std::endl;
0106   c = b;
0107   std::cout << b.references() << ' ' << &b.data() << ' ';
0108   std::cout << &b.unsharedData() << std::endl;
0109   std::cout << b.references() << ' ' << &b.data() << ' ';
0110   std::cout << &b.unsharedData() << std::endl;
0111   std::cout << c.references() << ' ' << &c.data() << ' ';
0112   std::cout << &c.unsharedData() << std::endl;
0113 
0114   std::cout << "end " << std::endl;
0115 }
0116 
0117 int main(int k, const char **) {
0118   astd(k);
0119   achurn(k);
0120 
0121   return 0;
0122 }