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