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 Experimental {
34namespace Detail {
35
36// clang-format off
37/**
38\class ROOT::Experimental::Detail::RRawPtrWriteEntry
39\ingroup NTuple
40\brief A container of const raw pointers, corresponding to a row in the data set
41
42This class can be used to write constant data products in frameworks. All other users are encouraged to use the API
43provided by REntry, with safe interfaces, type checks, and shared object ownership.
44*/
45// clang-format on
47 friend class ROOT::RNTupleModel;
49
50private:
51 /// The entry must be linked to a specific model, identified by a model ID
52 std::uint64_t fModelId = 0;
53 /// The entry and its tokens are also linked to a specific schema, identified by a schema ID
54 std::uint64_t fSchemaId = 0;
55 /// Corresponds to the fields of the linked model
56 std::vector<ROOT::RFieldBase *> fFields;
57 /// The raw pointers corresponding to the fields
58 std::vector<const void *> fRawPtrs;
59 /// For fast lookup of token IDs given a (sub)field name present in the entry
60 std::unordered_map<std::string, std::size_t> fFieldName2Token;
61
62 explicit RRawPtrWriteEntry(std::uint64_t modelId, std::uint64_t schemaId) : fModelId(modelId), fSchemaId(schemaId) {}
63
65 {
66 fFieldName2Token[field.GetQualifiedFieldName()] = fFields.size();
67 fFields.emplace_back(&field);
68 fRawPtrs.emplace_back(nullptr);
69 }
70
71 std::size_t Append()
72 {
73 std::size_t bytesWritten = 0;
74 for (std::size_t i = 0; i < fFields.size(); i++) {
75 bytesWritten += fFields[i]->Append(fRawPtrs[i]);
76 }
77 return bytesWritten;
78 }
79
81 {
82 if (fSchemaId != token.fSchemaId) {
83 throw RException(R__FAIL("invalid token for this entry, "
84 "make sure to use a token from a model with the same schema as this entry."));
85 }
86 }
87
88public:
93 ~RRawPtrWriteEntry() = default;
94
95 /// The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding value
96 RFieldToken GetToken(std::string_view fieldName) const
97 {
98 auto it = fFieldName2Token.find(std::string(fieldName));
99 if (it == fFieldName2Token.end()) {
100 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
101 }
102 return RFieldToken(it->second, fSchemaId);
103 }
104
105 template <typename T>
106 void BindRawPtr(RFieldToken token, const T *rawPtr)
107 {
108 EnsureMatchingModel(token);
109 fRawPtrs[token.fIndex] = rawPtr;
110 }
111
112 template <typename T>
113 void BindRawPtr(std::string_view fieldName, const T *rawPtr)
114 {
116 }
117
118 std::uint64_t GetModelId() const { return fModelId; }
119 std::uint64_t GetSchemaId() const { return fSchemaId; }
120};
121
122} // namespace Detail
123} // namespace Experimental
124} // namespace ROOT
125
126#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=(const RRawPtrWriteEntry &other)=delete
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 BindRawPtr(std::string_view fieldName, const T *rawPtr)
std::uint64_t fModelId
The entry must be linked to a specific model, identified by a model ID.
RRawPtrWriteEntry(const RRawPtrWriteEntry &other)=delete
RRawPtrWriteEntry(std::uint64_t modelId, std::uint64_t schemaId)
std::vector< ROOT::RFieldBase * > fFields
Corresponds to the fields of the linked model.
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.
void EnsureMatchingModel(RFieldToken token) const
RRawPtrWriteEntry(RRawPtrWriteEntry &&other)=default
void BindRawPtr(RFieldToken token, const T *rawPtr)
RRawPtrWriteEntry & operator=(RRawPtrWriteEntry &&other)=default
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...
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.