Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RRawPtrWriteEntry.hxx
Go to the documentation of this file.
1/// \file ROOT/RRawPtrWriteEntry.hxx
2/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
3/// \date 2025-03-19
4/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
5/// is welcome!
6
7/*************************************************************************
8 * Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. *
9 * All rights reserved. *
10 * *
11 * For the licensing terms see $ROOTSYS/LICENSE. *
12 * For the list of contributors see $ROOTSYS/README/CREDITS. *
13 *************************************************************************/
14
15#ifndef ROOT_RRawPtrWriteEntry
16#define ROOT_RRawPtrWriteEntry
17
18#include <ROOT/RFieldBase.hxx>
19#include <ROOT/RFieldToken.hxx>
20#include <ROOT/RError.hxx>
21
22#include <cstdint>
23#include <unordered_map>
24#include <vector>
25
26namespace ROOT {
27
28class RNTupleModel;
29
31
32namespace Detail {
33
34// clang-format off
35/**
36\class ROOT::Detail::RRawPtrWriteEntry
37\ingroup NTuple
38\brief A container of const raw pointers, corresponding to a row in the data set
39
40This class can be used to write constant data products in frameworks. All other users are encouraged to use the API
41provided by REntry, with safe interfaces, type checks, and shared object ownership.
42*/
43// clang-format on
45 friend class ROOT::RNTupleModel;
47
48private:
49 /// The entry must be linked to a specific model, identified by a model ID
50 std::uint64_t fModelId = 0;
51 /// The entry and its tokens are also linked to a specific schema, identified by a schema ID
52 std::uint64_t fSchemaId = 0;
53 /// Corresponds to the fields of the linked model
54 std::vector<ROOT::RFieldBase *> fFields;
55 /// The raw pointers corresponding to the fields
56 std::vector<const void *> fRawPtrs;
57 /// For fast lookup of token IDs given a (sub)field name present in the entry
58 std::unordered_map<std::string, std::size_t> fFieldName2Token;
59
60 explicit RRawPtrWriteEntry(std::uint64_t modelId, std::uint64_t schemaId) : fModelId(modelId), fSchemaId(schemaId) {}
61
63 {
65 fFields.emplace_back(&field);
66 fRawPtrs.emplace_back(nullptr);
67 }
68
69 std::size_t Append()
70 {
71 std::size_t bytesWritten = 0;
72 for (std::size_t i = 0; i < fFields.size(); i++) {
73 bytesWritten += fFields[i]->Append(fRawPtrs[i]);
74 }
75 return bytesWritten;
76 }
77
79 {
80 if (fSchemaId != token.fSchemaId) {
81 throw RException(R__FAIL("invalid token for this entry, "
82 "make sure to use a token from a model with the same schema as this entry."));
83 }
84 }
85
86public:
87 RRawPtrWriteEntry(const RRawPtrWriteEntry &other) = delete;
91 ~RRawPtrWriteEntry() = default;
92
93 /// The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding value
94 RFieldToken GetToken(std::string_view fieldName) const
95 {
96 auto it = fFieldName2Token.find(std::string(fieldName));
97 if (it == fFieldName2Token.end()) {
98 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
99 }
100 return RFieldToken(it->second, fSchemaId);
101 }
102
103 template <typename T>
104 void BindRawPtr(RFieldToken token, const T *rawPtr)
105 {
106 EnsureMatchingModel(token);
107 fRawPtrs[token.fIndex] = rawPtr;
108 }
109
110 template <typename T>
111 void BindRawPtr(std::string_view fieldName, const T *rawPtr)
112 {
113 BindRawPtr(GetToken(fieldName), rawPtr);
114 }
115
116 std::uint64_t GetModelId() const { return fModelId; }
117 std::uint64_t GetSchemaId() const { return fSchemaId; }
118};
119
120} // namespace Detail
121} // namespace ROOT
122
123#endif
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:299
RRawPtrWriteEntry & operator=(RRawPtrWriteEntry &&other)=default
std::vector< ROOT::RFieldBase * > fFields
Corresponds to the fields of the linked model.
void EnsureMatchingModel(RFieldToken token) const
RRawPtrWriteEntry(std::uint64_t modelId, std::uint64_t schemaId)
RRawPtrWriteEntry(RRawPtrWriteEntry &&other)=default
RRawPtrWriteEntry & operator=(const RRawPtrWriteEntry &other)=delete
RRawPtrWriteEntry(const RRawPtrWriteEntry &other)=delete
RFieldToken GetToken(std::string_view fieldName) const
The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding va...
void BindRawPtr(std::string_view fieldName, const T *rawPtr)
void BindRawPtr(RFieldToken token, const T *rawPtr)
std::uint64_t fSchemaId
The entry and its tokens are also linked to a specific schema, identified by a schema ID.
std::vector< const void * > fRawPtrs
The raw pointers corresponding to the fields.
std::uint64_t fModelId
The entry must be linked to a specific model, identified by a model ID.
std::unordered_map< std::string, std::size_t > fFieldName2Token
For fast lookup of token IDs given a (sub)field name present in the entry.
void AddField(ROOT::RFieldBase &field)
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
A field translates read and write calls from/to underlying columns to/from tree values.
std::string GetQualifiedFieldName() const
Returns the field name and parent field names separated by dots (grandparent.parent....
A field token identifies a (sub)field in an entry.
std::size_t fIndex
The index of the field (top-level or registered subfield).
std::uint64_t fSchemaId
Safety check to prevent tokens from other models being used.
A context for filling entries (data) into clusters of an RNTuple.
The RNTupleModel encapulates the schema of an RNTuple.
Special implementation of ROOT::RRangeCast for TCollection, including a check that the cast target ty...
Definition TObject.h:395