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 // read function for PortableHostCollection, called for every event
41 template <typename T>
42 static void readPortableHostMultiCollection_v1(char *target, TVirtualObject *from_buffer) {
43  // extract the actual types
44  using Collection = T;
45  using Implementation = typename Collection::Implementation;
46 
47  // valid only for PortableHostCollection<T>
48  //static_assert(std::is_same_v<Collection, PortableHostCollection<Layout>>);
49 
50  // proxy for the object being read from file
51  struct OnFile {
52  Implementation &impl_;
53  };
54 
55  // address in memory of the buffer containing the object being read from file
56  char *address = static_cast<char *>(from_buffer->GetObject());
57  // offset of the "layout_" data member
58  static ptrdiff_t impl_offset = from_buffer->GetClass()->GetDataMemberOffset("impl_");
59  // reference to the Layout object being read from file
60  OnFile onfile = {*(Implementation *)(address + impl_offset)};
61 
62  // pointer to the Collection object being constructed in memory
63  Collection *newObj = (Collection *)target;
64 
65  // move the data from the on-file layout to the newly constructed object
66  Collection::ROOTReadStreamer(newObj, onfile.impl_);
67 }
68 
69 // put set_PortableHostCollection_read_rules in the ROOT namespace to let it forward declare GenerateInitInstance
70 namespace ROOT {
71 
72  // set the read rules for PortableHostCollection<T>;
73  // this is called only once, when the dictionary is loaded.
74  template <typename T>
76  // forward declaration
77  TGenericClassInfo *GenerateInitInstance(T const *);
78 
79  // build the read rules
80  std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
81  ROOT::Internal::TSchemaHelper &rule = readrules[0];
82  rule.fTarget = "buffer_,layout_,view_";
83  rule.fSourceClass = type;
84  rule.fSource = type + "::Layout layout_;";
85  rule.fCode = type + "::ROOTReadStreamer(newObj, onfile.layout_)";
86  rule.fVersion = "[1-]";
87  rule.fChecksum = "";
88  rule.fInclude = "";
89  rule.fEmbed = false;
90  rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostCollection_v1<T>);
91  rule.fAttributes = "";
92 
93  // set the read rules
94  TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
95  instance->SetReadRules(readrules);
96 
97  return true;
98  }
99 
100  // set the read rules for PortableHostMultiCollection<T>;
101  // this is called only once, when the dictionary is loaded.
102  template <typename T>
104  // forward declaration
105  TGenericClassInfo *GenerateInitInstance(T const *);
106 
107  // build the read rules
108  std::vector<ROOT::Internal::TSchemaHelper> readrules(1);
109  ROOT::Internal::TSchemaHelper &rule = readrules[0];
110  rule.fTarget = "buffer_,impl_";
111  rule.fSourceClass = type;
112  rule.fSource = type + "::Implementation impl_;";
113  rule.fCode = type + "::ROOTReadStreamer(newObj, onfile.impl_)";
114  rule.fVersion = "[1-]";
115  rule.fChecksum = "";
116  rule.fInclude = "";
117  rule.fEmbed = false;
118  rule.fFunctionPtr = reinterpret_cast<void *>(::readPortableHostMultiCollection_v1<T>);
119  rule.fAttributes = "";
120 
121  // set the read rules
122  TGenericClassInfo *instance = GenerateInitInstance((T const *)nullptr);
123  instance->SetReadRules(readrules);
124 
125  return true;
126  }
127 } // namespace ROOT
128 
129 #define SET_PORTABLEHOSTCOLLECTION_READ_RULES(COLLECTION) \
130  static bool EDM_CONCATENATE(set_PortableHostCollection_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
131  ROOT::set_PortableHostCollection_read_rules<COLLECTION>(EDM_STRINGIZE(COLLECTION))
132 
133 #define SET_PORTABLEHOSTMULTICOLLECTION_READ_RULES(COLLECTION) \
134  static bool EDM_CONCATENATE(set_PortableHostMultiCollection_read_rules_done_at_, __LINE__) [[maybe_unused]] = \
135  ROOT::set_PortableHostMultiCollection_read_rules<COLLECTION>(EDM_STRINGIZE(COLLECTION))
136 
137 #endif // DataFormats_Portable_interface_PortableHostCollectionReadRules_h
static PFTauRenderPlugin instance
static bool set_PortableHostMultiCollection_read_rules(std::string const &type)
static bool set_PortableHostCollection_read_rules(std::string const &type)
static void readPortableHostMultiCollection_v1(char *target, TVirtualObject *from_buffer)
long double T
static void readPortableHostCollection_v1(char *target, TVirtualObject *from_buffer)