Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:01

0001 #ifndef DataFormats_Portable_interface_PortableHostCollectionReadRules_h
0002 #define DataFormats_Portable_interface_PortableHostCollectionReadRules_h
0003 
0004 #include <TGenericClassInfo.h>
0005 #include <TVirtualObject.h>
0006 
0007 #include "DataFormats/Portable/interface/PortableHostCollection.h"
0008 #include "FWCore/Utilities/interface/concatenate.h"
0009 #include "FWCore/Utilities/interface/stringize.h"
0010 
0011 // read function for PortableHostCollection, called for every event
0012 template <typename T>
0013 static void readPortableHostCollection_v1(char *target, TVirtualObject *from_buffer) {
0014   // extract the actual types
0015   using Collection = T;
0016   using Layout = typename Collection::Layout;
0017 
0018   // valid only for PortableHostCollection<T>
0019   static_assert(std::is_same_v<Collection, PortableHostCollection<Layout>>);
0020 
0021   // proxy for the object being read from file
0022   struct OnFile {
0023     Layout &layout_;
0024   };
0025 
0026   // address in memory of the buffer containing the object being read from file
0027   char *address = static_cast<char *>(from_buffer->GetObject());
0028   // offset of the "layout_" data member
0029   static ptrdiff_t layout_offset = from_buffer->GetClass()->GetDataMemberOffset("layout_");
0030   // reference to the Layout object being read from file
0031   OnFile onfile = {*(Layout *)(address + layout_offset)};
0032 
0033   // pointer to the Collection object being constructed in memory
0034   Collection *newObj = (Collection *)target;
0035 
0036   // move the data from the on-file layout to the newly constructed object
0037   Collection::ROOTReadStreamer(newObj, onfile.layout_);
0038 }
0039 
0040 // read function for PortableHostCollection, called for every event
0041 template <typename T>
0042 static void readPortableHostMultiCollection_v1(char *target, TVirtualObject *from_buffer) {
0043   // extract the actual types
0044   using Collection = T;
0045   using Implementation = typename Collection::Implementation;
0046 
0047   // valid only for PortableHostCollection<T>
0048   //static_assert(std::is_same_v<Collection, PortableHostCollection<Layout>>);
0049 
0050   // proxy for the object being read from file
0051   struct OnFile {
0052     Implementation &impl_;
0053   };
0054 
0055   // address in memory of the buffer containing the object being read from file
0056   char *address = static_cast<char *>(from_buffer->GetObject());
0057   // offset of the "layout_" data member
0058   static ptrdiff_t impl_offset = from_buffer->GetClass()->GetDataMemberOffset("impl_");
0059   // reference to the Layout object being read from file
0060   OnFile onfile = {*(Implementation *)(address + impl_offset)};
0061 
0062   // pointer to the Collection object being constructed in memory
0063   Collection *newObj = (Collection *)target;
0064 
0065   // move the data from the on-file layout to the newly constructed object
0066   Collection::ROOTReadStreamer(newObj, onfile.impl_);
0067 }
0068 
0069 // put set_PortableHostCollection_read_rules in the ROOT namespace to let it forward declare GenerateInitInstance
0070 namespace ROOT {
0071 
0072   // set the read rules for PortableHostCollection<T>;
0073   // this is called only once, when the dictionary is loaded.
0074   template <typename T>
0075   static bool set_PortableHostCollection_read_rules(std::string const &type) {
0076     // forward declaration
0077     TGenericClassInfo *GenerateInitInstance(T const *);
0078 
0079     // build the read rules
0080     std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
0081     ROOT::Internal::TSchemaHelper &rule = readrules[0];
0082     rule.fTarget = "buffer_,layout_,view_";
0083     rule.fSourceClass = type;
0084     rule.fSource = type + "::Layout layout_;";
0085     rule.fCode = type + "::ROOTReadStreamer(newObj, onfile.layout_)";
0086     rule.fVersion = "[1-]";
0087     rule.fChecksum = "";
0088     rule.fInclude = "";
0089     rule.fEmbed = false;
0090     rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostCollection_v1<T>);
0091     rule.fAttributes = "";
0092 
0093     // set the read rules
0094     TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
0095     instance->SetReadRules(readrules);
0096 
0097     return true;
0098   }
0099 
0100   // set the read rules for PortableHostMultiCollection<T>;
0101   // this is called only once, when the dictionary is loaded.
0102   template <typename T>
0103   static bool set_PortableHostMultiCollection_read_rules(std::string const &type) {
0104     // forward declaration
0105     TGenericClassInfo *GenerateInitInstance(T const *);
0106 
0107     // build the read rules
0108     std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
0109     ROOT::Internal::TSchemaHelper &rule = readrules[0];
0110     rule.fTarget = "buffer_,impl_";
0111     rule.fSourceClass = type;
0112     rule.fSource = type + "::Implementation impl_;";
0113     rule.fCode = type + "::ROOTReadStreamer(newObj, onfile.impl_)";
0114     rule.fVersion = "[1-]";
0115     rule.fChecksum = "";
0116     rule.fInclude = "";
0117     rule.fEmbed = false;
0118     rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostMultiCollection_v1<T>);
0119     rule.fAttributes = "";
0120 
0121     // set the read rules
0122     TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
0123     instance->SetReadRules(readrules);
0124 
0125     return true;
0126   }
0127 }  // namespace ROOT
0128 
0129 #define SET_PORTABLEHOSTCOLLECTION_READ_RULES(COLLECTION)                                                  \
0130   static bool EDM_CONCATENATE(set_PortableHostCollection_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
0131       ROOT::set_PortableHostCollection_read_rules<COLLECTION>(EDM_STRINGIZE(COLLECTION))
0132 
0133 #define SET_PORTABLEHOSTMULTICOLLECTION_READ_RULES(COLLECTION)                                                  \
0134   static bool EDM_CONCATENATE(set_PortableHostMultiCollection_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
0135       ROOT::set_PortableHostMultiCollection_read_rules<COLLECTION>(EDM_STRINGIZE(COLLECTION))
0136 
0137 #endif  // DataFormats_Portable_interface_PortableHostCollectionReadRules_h