Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DataFormats_Portable_interface_PortableHostObjectReadRules_h
0002 #define DataFormats_Portable_interface_PortableHostObjectReadRules_h
0003 
0004 #include <TGenericClassInfo.h>
0005 #include <TVirtualObject.h>
0006 
0007 #include "DataFormats/Portable/interface/PortableHostObject.h"
0008 #include "FWCore/Utilities/interface/concatenate.h"
0009 #include "FWCore/Utilities/interface/stringize.h"
0010 
0011 // read function for PortableHostObject, called for every event
0012 template <typename T>
0013 static void readPortableHostObject_v1(char *target, TVirtualObject *from_buffer) {
0014   // extract the actual types
0015   using Object = T;
0016   using Product = typename Object::Product;
0017 
0018   // valid only for PortableHostObject<T>
0019   static_assert(std::is_same_v<Object, PortableHostObject<Product>>);
0020 
0021   // proxy for the object being read from file
0022   struct OnFile {
0023     Product *product_;
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 "product_" data member
0029   static ptrdiff_t product_offset = from_buffer->GetClass()->GetDataMemberOffset("product_");
0030   // pointer to the Product object being read from file
0031   OnFile onfile = {*(Product **)(address + product_offset)};
0032 
0033   // pointer to the Object object being constructed in memory
0034   Object *newObj = (Object *)target;
0035 
0036   // move the data from the on-file layout to the newly constructed object
0037   Object::ROOTReadStreamer(newObj, *onfile.product_);
0038 }
0039 
0040 // put set_PortableHostObject_read_rules in the ROOT namespace to let it forward declare GenerateInitInstance
0041 namespace ROOT {
0042 
0043   // set the read rules for PortableHostObject<T>;
0044   // this is called only once, when the dictionary is loaded.
0045   template <typename T>
0046   static bool set_PortableHostObject_read_rules(std::string const &type) {
0047     // forward declaration
0048     TGenericClassInfo *GenerateInitInstance(T const *);
0049 
0050     // build the read rules
0051     std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
0052     ROOT::Internal::TSchemaHelper &rule = readrules[0];
0053     rule.fTarget = "buffer_,product_";
0054     rule.fSourceClass = type;
0055     rule.fSource = type + "::Product* product_;";
0056     rule.fCode = type + "::ROOTReadStreamer(newObj, *onfile.product_)";
0057     rule.fVersion = "[1-]";
0058     rule.fChecksum = "";
0059     rule.fInclude = "";
0060     rule.fEmbed = false;
0061     rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostObject_v1<T>);
0062     rule.fAttributes = "";
0063 
0064     // set the read rules
0065     TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
0066     instance->SetReadRules(readrules);
0067 
0068     return true;
0069   }
0070 }  // namespace ROOT
0071 
0072 #define SET_PORTABLEHOSTOBJECT_READ_RULES(OBJECT)                                                      \
0073   static bool EDM_CONCATENATE(set_PortableHostObject_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
0074       ROOT::set_PortableHostObject_read_rules<OBJECT>(EDM_STRINGIZE(OBJECT))
0075 
0076 #endif  // DataFormats_Portable_interface_PortableHostObjectReadRules_h