Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:40

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework includes
0015 #include "DD4hep/DetFactoryHelper.h"
0016 #include "DD4hep/Printout.h"
0017 #include "DD4hep/Detector.h"
0018 #include "DD4hep/Objects.h"
0019 #include "DD4hep/World.h"
0020 
0021 using namespace dd4hep;
0022 using namespace dd4hep::detail;
0023 
0024 namespace  {
0025   
0026   class base_0   {
0027   public:
0028     int data_0 {0};
0029     base_0() = default;
0030     base_0(base_0&& copy) = default;
0031     base_0(const base_0& copy) = default;
0032     virtual ~base_0() = default;
0033     base_0& operator=(base_0&& copy) = default;
0034     base_0& operator=(const base_0& copy) = default;
0035   };
0036   
0037   template <typename T> class base_1: virtual public base_0   {
0038   public:
0039     T data_1;
0040     base_1() = default;
0041     base_1(base_1&& copy) = default;
0042     base_1(const base_1& copy) = default;
0043     virtual ~base_1() = default;
0044     base_1& operator=(base_1&& copy) = default;
0045     base_1& operator=(const base_1& copy) = default;
0046   };
0047   template <typename T> class base_2: virtual public base_0   {
0048   public:
0049     T data_2;
0050     base_2() = default;
0051     base_2(base_2&& copy) = default;
0052     base_2(const base_2& copy) = default;
0053     virtual ~base_2() = default;
0054     base_2& operator=(base_2&& copy) = default;
0055     base_2& operator=(const base_2& copy) = default;
0056   };
0057   
0058   class payload : virtual public base_1<int>, virtual public base_2<double>  {
0059   public:
0060     payload() = default;
0061     payload(payload&& copy) = default;
0062     payload(const payload& copy) = default;
0063     virtual ~payload() = default;
0064     payload& operator=(payload&& copy) = default;
0065     payload& operator=(const payload& copy) = default;
0066   };
0067 }
0068 
0069 /// Plugin function: Condition program example
0070 /**
0071  *  Factory: DD4hep_Conditions_dynamic
0072  *
0073  *  \author  M.Frank
0074  *  \version 1.0
0075  *  \date    01/12/2016
0076  */
0077 static int test_example (Detector& /* description */, int /* argc */, char** /* argv */)  {
0078 #define DATA_0  12345
0079 #define DATA_1  54321
0080 #define DATA_2  2.2222e2
0081 
0082   using namespace std;
0083 
0084   payload p;
0085   p.data_0 = DATA_0;
0086   p.data_1 = DATA_1;
0087   p.data_2 = DATA_2;
0088 
0089   payload* pl = &p;
0090   base_0*  b0 = pl;
0091   base_1<int>* b1 = pl;
0092   base_2<double>* b2 = pl;
0093   cout << "Payload: " << (void*)pl
0094        << " b0: " << (void*)b0
0095        << " b1: " << (void*)b1
0096        << " b2: " << (void*)b2
0097        << endl;
0098 
0099   b0 = (base_0*)(Cast::instance<payload>().apply_dynCast(Cast::instance<base_0>(), pl));
0100   cout << "Payload(payload -> base_0): " << (void*)b0 << "  data: " << b0->data_0 << endl;
0101   if ( b0->data_0 != DATA_0 ) cout << "Test FAILED" << endl;
0102   
0103   b1 = (base_1<int>*)(Cast::instance<payload>().apply_dynCast(Cast::instance<base_1<int> >(), pl));
0104   cout << "Payload(payload -> base_1): " << (void*)b1 << "  data: " << b1->data_1 << endl;
0105   if ( b1->data_1 != DATA_1 ) cout << "Test FAILED" << endl;
0106   
0107   b2 = (base_2<double>*)(Cast::instance<payload>().apply_dynCast(Cast::instance<base_2<double> >(), pl));
0108   cout << "Payload(payload -> base_2): " << (void*)b2 << "  data: " << b2->data_2 << endl;
0109   if ( b2->data_2 != DATA_2 ) cout << "Test FAILED" << endl;
0110 
0111   try   {
0112     b0 = (base_0*)(Cast::instance<base_2<double> >().apply_dynCast(Cast::instance<base_0>(), b2));
0113     cout << "Payload(base_2 -> base_0): " << (void*)b2 << " -> " << (void*)b0 << "  data: " << b0->data_0 << endl;
0114     if ( b0->data_0 != DATA_0 ) cout << "Test FAILED" << endl;
0115 
0116     b2 = (base_2<double>*)(Cast::instance<base_0>().apply_dynCast(Cast::instance<base_2<double> >(), b0));
0117     cout << "Payload(base_0 -> base_2): " << (void*)b0 << " -> " << (void*)b2 << "  data: " << b2->data_2 << endl;
0118     // Up-casts do not work!
0119     // if ( b2->data_2 != DATA_2 ) cout << "Test FAILED" << endl;
0120 
0121     b0 = (base_0*)(Cast::instance<base_1<int> >().apply_dynCast(Cast::instance<base_0>(), b1));
0122     cout << "Payload(base_1 -> base_0): " << (void*)b2 << " -> " << (void*)b0 << "  data: " << b0->data_0 << endl;
0123 
0124     b1 = (base_1<int>*)(Cast::instance<base_0>().apply_dynCast(Cast::instance<base_1<int> >(), b0));
0125     cout << "Payload(base_0 -> base_1): " << (void*)b0 << " -> " << (void*)b1 << "  data: " << b1->data_1 << endl;
0126     // Up-casts do not work!
0127     //if ( b1->data_1 != DATA_1 ) cout << "Test FAILED" << endl;
0128 
0129 #if 0
0130     // No upcasts. They do not work
0131     pl = (payload*)(Cast::instance<base_0>().apply_dynCast(Cast::instance<payload>(), b0));
0132     cout << "Payload(base_0 -> payload): " << (void*)b0 << " -> " << (void*)pl
0133      << " data_0: " << pl->data_0 << " data_1: " << pl->data_1 << " data_2: " << pl->data_2
0134      << endl;
0135 
0136     /// Cross cast fails:
0137     b1 = (base_1<int>*)(Cast::instance<base_2<double> >().apply_dynCast(Cast::instance<base_1<int> >(), b2));
0138     cout << "Payload(base_2 -> base_1): " << (void*)b1 << "  data: " << b1->data_1 << endl;
0139 #endif
0140   }
0141   catch(const exception& e)   {
0142     cout << "bad cast: " << e.what() << endl;
0143   }
0144 
0145   cout << "Test PASSED" << endl;
0146   // All done.
0147   return 1;
0148 }
0149 
0150 // first argument is the type from the xml file
0151 DECLARE_APPLY(DD4hep_test_ABI_cast,test_example)