Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldUtils.cxx
Go to the documentation of this file.
1/// \file RFieldUtils.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
4/// \date 2024-11-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#include "RFieldUtils.hxx"
9
10#include <ROOT/RLogger.hxx>
11#include <ROOT/RNTupleUtil.hxx>
12
13#include <TClass.h>
14#include <TClassEdit.h>
15#include <TDictAttributeMap.h>
16
17#include <algorithm>
18#include <charconv>
19#include <string>
20#include <string_view>
21#include <system_error>
22#include <unordered_map>
23#include <vector>
24
25namespace {
26
27const std::unordered_map<std::string_view, std::string_view> typeTranslationMap{
28 {"Bool_t", "bool"},
29 {"Float_t", "float"},
30 {"Double_t", "double"},
31 {"string", "std::string"},
32
33 {"byte", "std::byte"},
34 {"Char_t", "char"},
35 {"int8_t", "std::int8_t"},
36 {"UChar_t", "unsigned char"},
37 {"uint8_t", "std::uint8_t"},
38
39 {"Short_t", "short"},
40 {"int16_t", "std::int16_t"},
41 {"UShort_t", "unsigned short"},
42 {"uint16_t", "std::uint16_t"},
43
44 {"Int_t", "int"},
45 {"int32_t", "std::int32_t"},
46 {"UInt_t", "unsigned int"},
47 {"unsigned", "unsigned int"},
48 {"uint32_t", "std::uint32_t"},
49
50 // Long_t and ULong_t follow the platform's size of long and unsigned long: They are 64 bit on 64-bit Linux and
51 // macOS, but 32 bit on 32-bit platforms and Windows (regardless of pointer size).
52 {"Long_t", "long"},
53 {"ULong_t", "unsigned long"},
54
55 {"Long64_t", "long long"},
56 {"int64_t", "std::int64_t"},
57 {"ULong64_t", "unsigned long long"},
58 {"uint64_t", "std::uint64_t"}};
59
60}
61
62std::string ROOT::Experimental::Internal::GetNormalizedTypeName(const std::string &typeName)
63{
64 std::string normalizedType{TClassEdit::CleanType(typeName.c_str(), /*mode=*/2)};
65
66 if (auto it = typeTranslationMap.find(normalizedType); it != typeTranslationMap.end())
67 normalizedType = it->second;
68
69 if (normalizedType.substr(0, 7) == "vector<")
71 if (normalizedType.substr(0, 6) == "array<")
73 if (normalizedType.substr(0, 8) == "variant<")
75 if (normalizedType.substr(0, 5) == "pair<")
77 if (normalizedType.substr(0, 6) == "tuple<")
79 if (normalizedType.substr(0, 7) == "bitset<")
81 if (normalizedType.substr(0, 11) == "unique_ptr<")
83 if (normalizedType.substr(0, 4) == "set<")
85 if (normalizedType.substr(0, 14) == "unordered_set<")
87 if (normalizedType.substr(0, 9) == "multiset<")
89 if (normalizedType.substr(0, 19) == "unordered_multiset<")
91 if (normalizedType.substr(0, 4) == "map<")
93 if (normalizedType.substr(0, 14) == "unordered_map<")
95 if (normalizedType.substr(0, 9) == "multimap<")
97 if (normalizedType.substr(0, 19) == "unordered_multimap<")
99 if (normalizedType.substr(0, 7) == "atomic<")
100 normalizedType = "std::" + normalizedType;
101
102 if (normalizedType.substr(0, 11) == "ROOT::RVec<")
103 normalizedType = "ROOT::VecOps::RVec<" + normalizedType.substr(11);
104
105 return normalizedType;
106}
107
110{
111 auto am = cl->GetAttributeMap();
112 if (!am || !am->HasKey("rntuple.streamerMode"))
113 return ERNTupleSerializationMode::kUnset;
114
115 std::string value = am->GetPropertyAsString("rntuple.streamerMode");
116 std::transform(value.begin(), value.end(), value.begin(), ::toupper);
117 if (value == "TRUE") {
118 return ERNTupleSerializationMode::kForceStreamerMode;
119 } else if (value == "FALSE") {
120 return ERNTupleSerializationMode::kForceNativeMode;
121 } else {
122 R__LOG_WARNING(ROOT::Experimental::NTupleLog()) << "invalid setting for 'rntuple.streamerMode' class attribute: "
123 << am->GetPropertyAsString("rntuple.streamerMode");
124 return ERNTupleSerializationMode::kUnset;
125 }
126}
127
128std::tuple<std::string, std::vector<size_t>> ROOT::Experimental::Internal::ParseArrayType(std::string_view typeName)
129{
130 std::vector<size_t> sizeVec;
131
132 // Only parse outer array definition, i.e. the right `]` should be at the end of the type name
133 while (typeName.back() == ']') {
134 auto posRBrace = typeName.size() - 1;
135 auto posLBrace = typeName.find_last_of('[', posRBrace);
136 if (posLBrace == std::string_view::npos)
137 return {};
138
139 size_t size;
140 if (std::from_chars(typeName.data() + posLBrace + 1, typeName.data() + posRBrace, size).ec != std::errc{})
141 return {};
142 sizeVec.insert(sizeVec.begin(), size);
143 typeName.remove_suffix(typeName.size() - posLBrace);
144 }
145 return std::make_tuple(std::string{typeName}, sizeVec);
146}
147
148std::vector<std::string> ROOT::Experimental::Internal::TokenizeTypeList(std::string_view templateType)
149{
150 std::vector<std::string> result;
151 if (templateType.empty())
152 return result;
153
154 const char *eol = templateType.data() + templateType.length();
155 const char *typeBegin = templateType.data();
156 const char *typeCursor = templateType.data();
157 unsigned int nestingLevel = 0;
158 while (typeCursor != eol) {
159 switch (*typeCursor) {
160 case '<': ++nestingLevel; break;
161 case '>': --nestingLevel; break;
162 case ',':
163 if (nestingLevel == 0) {
164 result.push_back(std::string(typeBegin, typeCursor - typeBegin));
165 typeBegin = typeCursor + 1;
166 }
167 break;
168 }
169 typeCursor++;
170 }
171 result.push_back(std::string(typeBegin, typeCursor - typeBegin));
172 return result;
173}
#define R__LOG_WARNING(...)
Definition RLogger.hxx:358
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
const_iterator begin() const
const_iterator end() const
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
TDictAttributeMap * GetAttributeMap() const
std::vector< std::string > TokenizeTypeList(std::string_view templateType)
Used in RFieldBase::Create() in order to get the comma-separated list of template types E....
std::string GetNormalizedTypeName(const std::string &typeName)
Applies type name normalization rules that lead to the final name used to create a RField,...
ERNTupleSerializationMode
Possible settings for the "rntuple.streamerMode" class attribute in the dictionary.
ERNTupleSerializationMode GetRNTupleSerializationMode(TClass *cl)
std::tuple< std::string, std::vector< size_t > > ParseArrayType(std::string_view typeName)
Parse a type name of the form T[n][m]... and return the base type T and a vector that contains,...
ROOT::RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
std::string CleanType(const char *typeDesc, int mode=0, const char **tail=nullptr)
Cleanup type description, redundant blanks removed and redundant tail ignored return *tail = pointer ...