Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/ROOT/RRawPtrWriteEntry.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /// \file ROOT/RRawPtrWriteEntry.hxx
0002 /// \ingroup NTuple
0003 /// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
0004 /// \date 2025-03-19
0005 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
0006 /// is welcome!
0007 
0008 /*************************************************************************
0009  * Copyright (C) 1995-2025, Rene Brun and Fons Rademakers.               *
0010  * All rights reserved.                                                  *
0011  *                                                                       *
0012  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0013  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0014  *************************************************************************/
0015 
0016 #ifndef ROOT_RRawPtrWriteEntry
0017 #define ROOT_RRawPtrWriteEntry
0018 
0019 #include <ROOT/RFieldBase.hxx>
0020 #include <ROOT/RFieldToken.hxx>
0021 #include <ROOT/RError.hxx>
0022 
0023 #include <cstdint>
0024 #include <unordered_map>
0025 #include <vector>
0026 
0027 namespace ROOT {
0028 
0029 class RNTupleModel;
0030 
0031 class RNTupleFillContext;
0032 
0033 namespace Experimental {
0034 namespace Detail {
0035 
0036 // clang-format off
0037 /**
0038 \class ROOT::Experimental::Detail::RRawPtrWriteEntry
0039 \ingroup NTuple
0040 \brief A container of const raw pointers, corresponding to a row in the data set
0041 
0042 This class can be used to write constant data products in frameworks.  All other users are encouraged to use the API
0043 provided by REntry, with safe interfaces, type checks, and shared object ownership.
0044 */
0045 // clang-format on
0046 class RRawPtrWriteEntry {
0047    friend class ROOT::RNTupleModel;
0048    friend class ROOT::RNTupleFillContext;
0049 
0050 private:
0051    /// The entry must be linked to a specific model, identified by a model ID
0052    std::uint64_t fModelId = 0;
0053    /// The entry and its tokens are also linked to a specific schema, identified by a schema ID
0054    std::uint64_t fSchemaId = 0;
0055    /// Corresponds to the fields of the linked model
0056    std::vector<ROOT::RFieldBase *> fFields;
0057    /// The raw pointers corresponding to the fields
0058    std::vector<const void *> fRawPtrs;
0059    /// For fast lookup of token IDs given a (sub)field name present in the entry
0060    std::unordered_map<std::string, std::size_t> fFieldName2Token;
0061 
0062    explicit RRawPtrWriteEntry(std::uint64_t modelId, std::uint64_t schemaId) : fModelId(modelId), fSchemaId(schemaId) {}
0063 
0064    void AddField(ROOT::RFieldBase &field)
0065    {
0066       fFieldName2Token[field.GetQualifiedFieldName()] = fFields.size();
0067       fFields.emplace_back(&field);
0068       fRawPtrs.emplace_back(nullptr);
0069    }
0070 
0071    std::size_t Append()
0072    {
0073       std::size_t bytesWritten = 0;
0074       for (std::size_t i = 0; i < fFields.size(); i++) {
0075          bytesWritten += fFields[i]->Append(fRawPtrs[i]);
0076       }
0077       return bytesWritten;
0078    }
0079 
0080    void EnsureMatchingModel(RFieldToken token) const
0081    {
0082       if (fSchemaId != token.fSchemaId) {
0083          throw RException(R__FAIL("invalid token for this entry, "
0084                                   "make sure to use a token from a model with the same schema as this entry."));
0085       }
0086    }
0087 
0088 public:
0089    RRawPtrWriteEntry(const RRawPtrWriteEntry &other) = delete;
0090    RRawPtrWriteEntry &operator=(const RRawPtrWriteEntry &other) = delete;
0091    RRawPtrWriteEntry(RRawPtrWriteEntry &&other) = default;
0092    RRawPtrWriteEntry &operator=(RRawPtrWriteEntry &&other) = default;
0093    ~RRawPtrWriteEntry() = default;
0094 
0095    /// The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding value
0096    RFieldToken GetToken(std::string_view fieldName) const
0097    {
0098       auto it = fFieldName2Token.find(std::string(fieldName));
0099       if (it == fFieldName2Token.end()) {
0100          throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
0101       }
0102       return RFieldToken(it->second, fSchemaId);
0103    }
0104 
0105    template <typename T>
0106    void BindRawPtr(RFieldToken token, const T *rawPtr)
0107    {
0108       EnsureMatchingModel(token);
0109       fRawPtrs[token.fIndex] = rawPtr;
0110    }
0111 
0112    template <typename T>
0113    void BindRawPtr(std::string_view fieldName, const T *rawPtr)
0114    {
0115       BindRawPtr(GetToken(fieldName), rawPtr);
0116    }
0117 
0118    std::uint64_t GetModelId() const { return fModelId; }
0119    std::uint64_t GetSchemaId() const { return fSchemaId; }
0120 };
0121 
0122 } // namespace Detail
0123 } // namespace Experimental
0124 } // namespace ROOT
0125 
0126 #endif