CMS 3D CMS Logo

PortableHostCollectionReadRules.h
Go to the documentation of this file.
1 #ifndef DataFormats_Portable_interface_PortableHostCollectionReadRules_h
2 #define DataFormats_Portable_interface_PortableHostCollectionReadRules_h
3 
4 #include <TGenericClassInfo.h>
5 #include <TVirtualObject.h>
6 
10 
11 // read function for PortableHostCollection, called for every event
12 template <typename T>
13 static void readPortableHostCollection_v1(char *target, TVirtualObject *from_buffer) {
14  // extract the actual types
15  using Collection = T;
16  using Layout = typename Collection::Layout;
17 
18  // valid only for PortableHostCollection<T>
19  static_assert(std::is_same_v<Collection, PortableHostCollection<Layout>>);
20 
21  // proxy for the object being read from file
22  struct OnFile {
23  Layout &layout_;
24  };
25 
26  // address in memory of the buffer containing the object being read from file
27  char *address = static_cast<char *>(from_buffer->GetObject());
28  // offset of the "layout_" data member
29  static ptrdiff_t layout_offset = from_buffer->GetClass()->GetDataMemberOffset("layout_");
30  // reference to the Layout object being read from file
31  OnFile onfile = {*(Layout *)(address + layout_offset)};
32 
33  // pointer to the Collection object being constructed in memory
34  Collection *newObj = (Collection *)target;
35 
36  // move the data from the on-file layout to the newly constructed object
37  Collection::ROOTReadStreamer(newObj, onfile.layout_);
38 }
39 
40 // put set_PortableHostCollection_read_rules in the ROOT namespace to let it forward declare GenerateInitInstance
41 namespace ROOT {
42 
43  // set the read rules for PortableHostCollection<T>;
44  // this is called only once, when the dictionary is loaded.
45  template <typename T>
47  // forward declaration
48  TGenericClassInfo *GenerateInitInstance(T const *);
49 
50  // build the read rules
51  std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
52  ROOT::Internal::TSchemaHelper &rule = readrules[0];
53  rule.fTarget = "buffer_,layout_,view_";
54  rule.fSourceClass = type;
55  rule.fSource = type + "::Layout layout_;";
56  rule.fCode = type + "::ROOTReadStreamer(newObj, onfile.layout_)";
57  rule.fVersion = "[1-]";
58  rule.fChecksum = "";
59  rule.fInclude = "";
60  rule.fEmbed = false;
61  rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostCollection_v1<T>);
62  rule.fAttributes = "";
63 
64  // set the read rules
65  TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
66  instance->SetReadRules(readrules);
67 
68  return true;
69  }
70 } // namespace ROOT
71 
72 #define SET_PORTABLEHOSTCOLLECTION_READ_RULES(COLLECTION) \
73  static bool EDM_CONCATENATE(set_PortableHostCollection_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
74  ROOT::set_PortableHostCollection_read_rules<COLLECTION>(EDM_STRINGIZE(COLLECTION))
75 
76 #endif // DataFormats_Portable_interface_PortableHostCollectionReadRules_h
static PFTauRenderPlugin instance
static bool set_PortableHostCollection_read_rules(std::string const &type)
long double T
static void readPortableHostCollection_v1(char *target, TVirtualObject *from_buffer)