Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldUtils.cxx
Go to the documentation of this file.
1/// \file RFieldUtils.cxx
2/// \ingroup NTuple
3/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
4/// \date 2024-11-19
5
7
8#include <ROOT/RField.hxx>
9#include <ROOT/RLogger.hxx>
11#include <ROOT/RNTupleTypes.hxx>
12#include <ROOT/RNTupleUtils.hxx>
13
14#include <TClass.h>
15#include <TClassEdit.h>
16#include <TDictAttributeMap.h>
17
18#include <algorithm>
19#include <charconv>
20#include <limits>
21#include <string>
22#include <string_view>
23#include <system_error>
24#include <unordered_map>
25#include <utility>
26#include <vector>
27
28namespace {
29
30std::string GetRenormalizedDemangledTypeName(const std::string &demangledName, bool renormalizeStdString);
31
32const std::unordered_map<std::string_view, std::string_view> typeTranslationMap{
33 {"Bool_t", "bool"},
34 {"Float_t", "float"},
35 {"Double_t", "double"},
36 {"string", "std::string"},
37
38 {"byte", "std::byte"},
39 {"Char_t", "char"},
40 {"int8_t", "std::int8_t"},
41 {"UChar_t", "unsigned char"},
42 {"uint8_t", "std::uint8_t"},
43
44 {"Short_t", "short"},
45 {"int16_t", "std::int16_t"},
46 {"UShort_t", "unsigned short"},
47 {"uint16_t", "std::uint16_t"},
48
49 {"Int_t", "int"},
50 {"int32_t", "std::int32_t"},
51 {"UInt_t", "unsigned int"},
52 {"unsigned", "unsigned int"},
53 {"uint32_t", "std::uint32_t"},
54
55 // Long_t and ULong_t follow the platform's size of long and unsigned long: They are 64 bit on 64-bit Linux and
56 // macOS, but 32 bit on 32-bit platforms and Windows (regardless of pointer size).
57 {"Long_t", "long"},
58 {"ULong_t", "unsigned long"},
59
60 {"Long64_t", "long long"},
61 {"int64_t", "std::int64_t"},
62 {"ULong64_t", "unsigned long long"},
63 {"uint64_t", "std::uint64_t"}};
64
65// Natively supported types drop the default template arguments and the CV qualifiers in template arguments.
66// Any types used as a template argument of user classes will keep [U]Long64_t template arguments for the type alias,
67// e.g. MyClass<std::vector<Long64_t>> will normalize to `MyClass<std::vector<std::int64_t>>` but keep the original
68// spelling in the type alias.
69bool IsUserClass(const std::string &typeName)
70{
71 return typeName.rfind("std::", 0) != 0 && typeName.rfind("ROOT::VecOps::RVec<", 0) != 0;
72}
73
74/// Parse a type name of the form `T[n][m]...` and return the base type `T` and a vector that contains,
75/// in order, the declared size for each dimension, e.g. for `unsigned char[1][2][3]` it returns the tuple
76/// `{"unsigned char", {1, 2, 3}}`. Extra whitespace in `typeName` should be removed before calling this function.
77///
78/// If `typeName` is not an array type, it returns a tuple `{T, {}}`. On error, it returns a default-constructed tuple.
79std::tuple<std::string, std::vector<std::size_t>> ParseArrayType(const std::string &typeName)
80{
81 std::vector<std::size_t> sizeVec;
82
83 // Only parse outer array definition, i.e. the right `]` should be at the end of the type name
84 std::string prefix{typeName};
85 while (prefix.back() == ']') {
86 auto posRBrace = prefix.size() - 1;
87 auto posLBrace = prefix.rfind('[', posRBrace);
88 if (posLBrace == std::string_view::npos) {
89 throw ROOT::RException(R__FAIL(std::string("invalid array type: ") + typeName));
90 }
91 if (posRBrace - posLBrace <= 1) {
92 throw ROOT::RException(R__FAIL(std::string("invalid array type: ") + typeName));
93 }
94
95 const std::size_t size =
97 if (size == 0) {
98 throw ROOT::RException(R__FAIL(std::string("invalid array size: ") + typeName));
99 }
100
101 sizeVec.insert(sizeVec.begin(), size);
102 prefix.resize(posLBrace);
103 }
104 return std::make_tuple(prefix, sizeVec);
105}
106
107/// Assembles a (nested) std::array<> based type based on the dimensions retrieved from ParseArrayType(). Returns
108/// baseType if there are no dimensions.
109std::string GetStandardArrayType(const std::string &baseType, const std::vector<std::size_t> &dimensions)
110{
111 std::string typeName = baseType;
112 for (auto i = dimensions.rbegin(), iEnd = dimensions.rend(); i != iEnd; ++i) {
113 typeName = "std::array<" + typeName + "," + std::to_string(*i) + ">";
114 }
115 return typeName;
116}
117
118// Recursively normalizes a template argument using the regular type name normalizer F as a helper.
119template <typename F>
120std::string GetNormalizedTemplateArg(const std::string &arg, bool keepQualifier, F fnTypeNormalizer)
121{
122 R__ASSERT(!arg.empty());
123
124 if (std::isdigit(arg[0]) || arg[0] == '-') {
125 // Integer template argument
127 }
128
129 if (!keepQualifier)
130 return fnTypeNormalizer(arg);
131
132 std::string qualifier;
133 // Type name template argument; template arguments must keep their CV qualifier. We assume that fnTypeNormalizer
134 // strips the qualifier.
135 // Demangled names may have the CV qualifiers suffixed and not prefixed (but const always before volatile).
136 // Note that in the latter case, we may have the CV qualifiers before array brackets, e.g. `int const[2]`.
137 const auto [base, _] = ParseArrayType(arg);
138 if (base.rfind("const ", 0) == 0 || base.rfind("volatile const ", 0) == 0 ||
139 base.find(" const", base.length() - 6) != std::string::npos ||
140 base.find(" const volatile", base.length() - 15) != std::string::npos) {
141 qualifier += "const ";
142 }
143 if (base.rfind("volatile ", 0) == 0 || base.rfind("const volatile ", 0) == 0 ||
144 base.find(" volatile", base.length() - 9) != std::string::npos) {
145 qualifier += "volatile ";
146 }
147 return qualifier + fnTypeNormalizer(arg);
148}
149
150using AnglePos = std::pair<std::string::size_type, std::string::size_type>;
151std::vector<AnglePos> FindTemplateAngleBrackets(const std::string &typeName)
152{
153 std::vector<AnglePos> result;
154 std::string::size_type currentPos = 0;
155 while (currentPos < typeName.size()) {
156 const auto posOpen = typeName.find('<', currentPos);
157 if (posOpen == std::string::npos) {
158 // If there are no more templates, the function is done.
159 break;
160 }
161
162 auto posClose = posOpen + 1;
163 int level = 1;
164 while (posClose < typeName.size()) {
165 const auto c = typeName[posClose];
166 if (c == '<') {
167 level++;
168 } else if (c == '>') {
169 if (level == 1) {
170 break;
171 }
172 level--;
173 }
174 posClose++;
175 }
176 // We should have found a closing angle bracket at the right level.
177 R__ASSERT(posClose < typeName.size());
178 result.emplace_back(posOpen, posClose);
179
180 // If we are not at the end yet, the following two characeters should be :: for nested types.
181 if (posClose < typeName.size() - 1) {
182 R__ASSERT(typeName.substr(posClose + 1, 2) == "::");
183 }
184 currentPos = posClose + 1;
185 }
186
187 return result;
188}
189
190// TClassEdit::CleanType and the name demangling insert blanks between closing angle brackets,
191// as they were required before C++11. Name demangling introduces a blank before array dimensions,
192// which should also be removed.
193void RemoveSpaceBefore(std::string &typeName, char beforeChar)
194{
195 auto dst = typeName.begin();
196 auto end = typeName.end();
197 for (auto src = dst; src != end; ++src) {
198 if (*src == ' ') {
199 auto next = src + 1;
200 if (next != end && *next == beforeChar) {
201 // Skip this space before a closing angle bracket.
202 continue;
203 }
204 }
205 *(dst++) = *src;
206 }
207 typeName.erase(dst, end);
208}
209
210// The demangled name adds spaces after commas
211void RemoveSpaceAfter(std::string &typeName, char afterChar)
212{
213 auto dst = typeName.begin();
214 auto end = typeName.end();
215 for (auto src = dst; src != end; ++src) {
216 *(dst++) = *src;
217 if (*src == afterChar) {
218 auto next = src + 1;
219 if (next != end && *next == ' ') {
220 // Skip this space before a closing angle bracket.
221 ++src;
222 }
223 }
224 }
225 typeName.erase(dst, end);
226}
227
228// We normalize typenames to omit any `class`, `struct`, `enum` prefix
229void RemoveLeadingKeyword(std::string &typeName)
230{
231 if (typeName.rfind("class ", 0) == 0) {
232 typeName.erase(0, 6);
233 } else if (typeName.rfind("struct ", 0) == 0) {
234 typeName.erase(0, 7);
235 } else if (typeName.rfind("enum ", 0) == 0) {
236 typeName.erase(0, 5);
237 }
238}
239
240// Needed for template arguments in demangled names
241void RemoveCVQualifiers(std::string &typeName)
242{
243 if (typeName.rfind("const ", 0) == 0)
244 typeName.erase(0, 6);
245 if (typeName.rfind("volatile ", 0) == 0)
246 typeName.erase(0, 9);
247 if (typeName.find(" volatile", typeName.length() - 9) != std::string::npos)
248 typeName.erase(typeName.length() - 9);
249 if (typeName.find(" const", typeName.length() - 6) != std::string::npos)
250 typeName.erase(typeName.length() - 6);
251}
252
253// Map fundamental integer types to stdint integer types (e.g. int --> std::int32_t)
254void MapIntegerType(std::string &typeName)
255{
256 if (typeName == "signed char") {
258 } else if (typeName == "unsigned char") {
260 } else if (typeName == "short" || typeName == "short int" || typeName == "signed short" ||
261 typeName == "signed short int") {
263 } else if (typeName == "unsigned short" || typeName == "unsigned short int") {
265 } else if (typeName == "int" || typeName == "signed" || typeName == "signed int") {
266 typeName = ROOT::RField<int>::TypeName();
267 } else if (typeName == "unsigned" || typeName == "unsigned int") {
269 } else if (typeName == "long" || typeName == "long int" || typeName == "signed long" ||
270 typeName == "signed long int") {
272 } else if (typeName == "unsigned long" || typeName == "unsigned long int") {
274 } else if (typeName == "long long" || typeName == "long long int" || typeName == "signed long long" ||
275 typeName == "signed long long int") {
277 } else if (typeName == "unsigned long long" || typeName == "unsigned long long int") {
279 } else {
280 // The following two types are 64-bit integers on Windows that we can encounter during renormalization of
281 // demangled std::type_info names.
282 if (typeName == "__int64") {
283 typeName = "std::int64_t";
284 } else if (typeName == "unsigned __int64") {
285 typeName = "std::uint64_t";
286 }
287 }
288}
289
290// Note that ROOT Meta already defines GetDemangledTypeName(), which does both demangling and normalizing.
291std::string GetRawDemangledTypeName(const std::type_info &ti)
292{
293 int e;
294 char *str = TClassEdit::DemangleName(ti.name(), e);
295 R__ASSERT(str && e == 0);
296 std::string result{str};
297 free(str);
298
299 return result;
300}
301
302// Reverse std::string --> std::basic_string<char> from the demangling
304{
305 static const std::string gStringName =
306 GetRenormalizedDemangledTypeName(GetRawDemangledTypeName(typeid(std::string)), false /* renormalizeStdString */);
307
308 // For real nested types of std::string (not typedefs like std::string::size_type), we would need to also check
309 // something like (normalizedTypeName + "::" == gStringName + "::") and replace only the prefix. However, since
310 // such a nested type is not standardized, it currently does not seem necessary to add the logic.
312 normalizedTypeName = "std::string";
313 }
314}
315
316// Reverse "internal" namespace prefix found in demangled names, such as std::vector<T> --> std::__1::vector<T>
318{
319 static std::vector<std::pair<std::string, std::string>> gDistortedStdlibNames = []() {
320 // clang-format off
321 // Listed in order of appearance in the BinaryFormatSpecification.md
322 static const std::vector<std::pair<const std::type_info &, std::string>> gCandidates =
323 {{typeid(std::vector<char>), "std::vector<"},
324 {typeid(std::array<char, 1>), "std::array<"},
325 {typeid(std::variant<char>), "std::variant<"},
326 {typeid(std::pair<char, char>), "std::pair<"},
327 {typeid(std::tuple<char>), "std::tuple<"},
328 {typeid(std::bitset<1>), "std::bitset<"},
329 {typeid(std::unique_ptr<char>), "std::unique_ptr<"},
330 {typeid(std::optional<char>), "std::optional<"},
331 {typeid(std::set<char>), "std::set<"},
332 {typeid(std::unordered_set<char>), "std::unordered_set<"},
333 {typeid(std::multiset<char>), "std::multiset<"},
334 {typeid(std::unordered_multiset<char>), "std::unordered_multiset<"},
335 {typeid(std::map<char, char>), "std::map<"},
336 {typeid(std::unordered_map<char, char>), "std::unordered_map<"},
337 {typeid(std::multimap<char, char>), "std::multimap<"},
338 {typeid(std::unordered_multimap<char, char>), "std::unordered_multimap<"},
339 {typeid(std::atomic<char>), "std::atomic<"}};
340 // clang-format on
341
342 std::vector<std::pair<std::string, std::string>> result;
343 for (const auto &[ti, prefix] : gCandidates) {
344 const auto dm = GetRawDemangledTypeName(ti);
345 if (dm.rfind(prefix, 0) == std::string::npos)
346 result.push_back(std::make_pair(dm.substr(0, dm.find('<') + 1), prefix));
347 }
348
349 return result;
350 }();
351
352 for (const auto &[seenPrefix, canonicalPrefix] : gDistortedStdlibNames) {
353 if (normalizedTypeName.rfind(seenPrefix, 0) == 0) {
355 break;
356 }
357 }
358}
359
360template <typename F>
362{
364 R__ASSERT(!angleBrackets.empty());
365
366 std::string normName;
367 std::string::size_type currentPos = 0;
368 for (std::size_t i = 0; i < angleBrackets.size(); i++) {
369 const auto [posOpen, posClose] = angleBrackets[i];
370 // Append the type prefix until the open angle bracket.
372
373 const auto argList = templatedTypeName.substr(posOpen + 1, posClose - posOpen - 1);
375 R__ASSERT(!templateArgs.empty());
376
378 for (const auto &a : templateArgs) {
380 }
381
382 normName[normName.size() - 1] = '>';
383 currentPos = posClose + 1;
384 }
385
386 // Append the rest of the type from the last closing angle bracket.
387 const auto lastClosePos = angleBrackets.back().second;
389
391}
392
393// Given a type name normalized by ROOT Meta, return the type name normalized according to the RNTuple rules.
394std::string GetRenormalizedMetaTypeName(const std::string &metaNormalizedName)
395{
397 // RNTuple resolves Double32_t for the normalized type name but keeps Double32_t for the type alias
398 // (also in template parameters)
399 if (canonicalTypePrefix == "Double32_t")
400 return "double";
401
402 if (canonicalTypePrefix.find('<') == std::string::npos) {
403 // If there are no templates, the function is done.
404 return canonicalTypePrefix;
405 }
406
407 std::string normName{canonicalTypePrefix};
409
410 return normName;
411}
412
413// Given a demangled name ("normalized by the compiler"), return the type name normalized according to the
414// RNTuple rules.
415std::string GetRenormalizedDemangledTypeName(const std::string &demangledName, bool renormalizeStdString)
416{
417 std::string tn{demangledName};
418 RemoveSpaceBefore(tn, '[');
423
424 if (canonicalTypePrefix.find('<') == std::string::npos) {
425 // If there are no templates, the function is done.
427 }
430 RemoveSpaceBefore(canonicalTypePrefix, ','); // MSVC fancies spaces before commas in the demangled name
432
433 // Remove optional stdlib template arguments
434 int maxTemplateArgs = 0;
435 if (canonicalTypePrefix.rfind("std::vector<", 0) == 0 || canonicalTypePrefix.rfind("std::set<", 0) == 0 ||
436 canonicalTypePrefix.rfind("std::unordered_set<", 0) == 0 ||
437 canonicalTypePrefix.rfind("std::multiset<", 0) == 0 ||
438 canonicalTypePrefix.rfind("std::unordered_multiset<", 0) == 0 ||
439 canonicalTypePrefix.rfind("std::unique_ptr<", 0) == 0) {
440 maxTemplateArgs = 1;
441 } else if (canonicalTypePrefix.rfind("std::map<", 0) == 0 ||
442 canonicalTypePrefix.rfind("std::unordered_map<", 0) == 0 ||
443 canonicalTypePrefix.rfind("std::multimap<", 0) == 0 ||
444 canonicalTypePrefix.rfind("std::unordered_multimap<", 0) == 0) {
445 maxTemplateArgs = 2;
446 }
447
448 std::string normName{canonicalTypePrefix};
451 });
452 // In RenormalizeStdString(), we normalize the demangled type name of `std::string`,
453 // so we need to prevent an endless recursion.
456 }
457
459}
460
461} // namespace
462
463std::string ROOT::Internal::GetCanonicalTypePrefix(const std::string &typeName)
464{
465 // Remove outer cv qualifiers and extra white spaces
466 const std::string cleanedType = TClassEdit::CleanType(typeName.c_str(), /*mode=*/1);
467
468 // Can happen when called from RFieldBase::Create() and is caught there
469 if (cleanedType.empty())
470 return "";
471
473
475 if (canonicalType.substr(0, 2) == "::") {
476 canonicalType.erase(0, 2);
477 }
478
480
481 if (canonicalType.substr(0, 6) == "array<") {
482 canonicalType = "std::" + canonicalType;
483 } else if (canonicalType.substr(0, 7) == "atomic<") {
484 canonicalType = "std::" + canonicalType;
485 } else if (canonicalType.substr(0, 7) == "bitset<") {
486 canonicalType = "std::" + canonicalType;
487 } else if (canonicalType.substr(0, 4) == "map<") {
488 canonicalType = "std::" + canonicalType;
489 } else if (canonicalType.substr(0, 9) == "multimap<") {
490 canonicalType = "std::" + canonicalType;
491 } else if (canonicalType.substr(0, 9) == "multiset<") {
492 canonicalType = "std::" + canonicalType;
493 }
494 if (canonicalType.substr(0, 5) == "pair<") {
495 canonicalType = "std::" + canonicalType;
496 } else if (canonicalType.substr(0, 4) == "set<") {
497 canonicalType = "std::" + canonicalType;
498 } else if (canonicalType.substr(0, 6) == "tuple<") {
499 canonicalType = "std::" + canonicalType;
500 } else if (canonicalType.substr(0, 11) == "unique_ptr<") {
501 canonicalType = "std::" + canonicalType;
502 } else if (canonicalType.substr(0, 14) == "unordered_map<") {
503 canonicalType = "std::" + canonicalType;
504 } else if (canonicalType.substr(0, 19) == "unordered_multimap<") {
505 canonicalType = "std::" + canonicalType;
506 } else if (canonicalType.substr(0, 19) == "unordered_multiset<") {
507 canonicalType = "std::" + canonicalType;
508 } else if (canonicalType.substr(0, 14) == "unordered_set<") {
509 canonicalType = "std::" + canonicalType;
510 } else if (canonicalType.substr(0, 8) == "variant<") {
511 canonicalType = "std::" + canonicalType;
512 } else if (canonicalType.substr(0, 7) == "vector<") {
513 canonicalType = "std::" + canonicalType;
514 } else if (canonicalType.substr(0, 11) == "ROOT::RVec<") {
515 canonicalType = "ROOT::VecOps::RVec<" + canonicalType.substr(11);
516 }
517
518 if (auto it = typeTranslationMap.find(canonicalType); it != typeTranslationMap.end()) {
519 canonicalType = it->second;
520 }
521
523
525}
526
527std::string ROOT::Internal::GetRenormalizedTypeName(const std::type_info &ti)
528{
529 return GetRenormalizedDemangledTypeName(GetRawDemangledTypeName(ti), true /* renormalizeStdString */);
530}
531
536
539{
541 if (canonicalTypePrefix.find('<') == std::string::npos) {
542 // If there are no templates, the function is done.
543 return false;
544 }
545
546 bool result = false;
548 auto fnCheckLong64 = [&](const std::string &arg) -> std::string {
549 if ((arg == "Long64_t" || arg == "ULong64_t") && hasTemplatedUserClassParent) {
550 result = true;
551 return arg;
552 }
553
554 std::string renormalizedArgAlias;
556 result = true;
558 }
559
560 return GetRenormalizedMetaTypeName(arg);
561 };
562
565
566 return result;
567}
568
570{
574 std::string shortType;
575 splitname.ShortType(shortType, modType);
577
578 if (canonicalTypePrefix.find('<') == std::string::npos) {
579 // If there are no templates, the function is done.
580 return canonicalTypePrefix;
581 }
582
584 R__ASSERT(!angleBrackets.empty());
585
586 // For user-defined class types, we will need to get the default-initialized template arguments.
588
589 std::string normName;
590 std::string::size_type currentPos = 0;
591 for (std::size_t i = 0; i < angleBrackets.size(); i++) {
592 const auto [posOpen, posClose] = angleBrackets[i];
593 // Append the type prefix until the open angle bracket.
595
596 const auto argList = canonicalTypePrefix.substr(posOpen + 1, posClose - posOpen - 1);
597 const auto templateArgs = TokenizeTypeList(argList);
598 R__ASSERT(!templateArgs.empty());
599
600 for (const auto &a : templateArgs) {
602 }
603
604 // For user-defined classes, append default-initialized template arguments.
605 if (isUserClass) {
606 const auto cl = TClass::GetClass(canonicalTypePrefix.substr(0, posClose + 1).c_str());
607 if (cl) {
608 const std::string expandedName = cl->GetName();
610 // We can have fewer pairs than angleBrackets, for example in case of type aliases.
612
614 const auto expandedArgList =
617 // Note that we may be in a sitation where expandedTemplateArgs.size() is _smaller_ than
618 // templateArgs.size(), which is when the input type name has the optional template arguments explicitly
619 // spelled out but ROOT Meta is told to ignore some template arguments.
620
621 for (std::size_t j = templateArgs.size(); j < expandedTemplateArgs.size(); ++j) {
622 normName +=
624 }
625 }
626 }
627
628 normName[normName.size() - 1] = '>';
629 currentPos = posClose + 1;
630 }
631
632 // Append the rest of the type from the last closing angle bracket.
633 const auto lastClosePos = angleBrackets.back().second;
635
636 return normName;
637}
638
639std::string ROOT::Internal::GetNormalizedInteger(long long val)
640{
641 return std::to_string(val);
642}
643
644std::string ROOT::Internal::GetNormalizedInteger(unsigned long long val)
645{
646 if (val > std::numeric_limits<std::int64_t>::max())
647 return std::to_string(val) + "u";
648 return std::to_string(val);
649}
650
658
659long long ROOT::Internal::ParseIntTypeToken(const std::string &intToken)
660{
661 std::size_t nChars = 0;
662 long long res = std::stoll(intToken, &nChars);
663 if (nChars == intToken.size())
664 return res;
665
666 assert(nChars < intToken.size());
667 if (nChars == 0) {
668 throw RException(R__FAIL("invalid integer type token: " + intToken));
669 }
670
671 auto suffix = intToken.substr(nChars);
672 std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::toupper);
673 if (suffix == "L" || suffix == "LL")
674 return res;
675 if (res >= 0 && (suffix == "U" || suffix == "UL" || suffix == "ULL"))
676 return res;
677
678 throw RException(R__FAIL("invalid integer type token: " + intToken));
679}
680
681unsigned long long ROOT::Internal::ParseUIntTypeToken(const std::string &uintToken)
682{
683 std::size_t nChars = 0;
684 unsigned long long res = std::stoull(uintToken, &nChars);
685 if (nChars == uintToken.size())
686 return res;
687
688 assert(nChars < uintToken.size());
689 if (nChars == 0) {
690 throw RException(R__FAIL("invalid integer type token: " + uintToken));
691 }
692
693 auto suffix = uintToken.substr(nChars);
694 std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::toupper);
695 if (suffix == "U" || suffix == "L" || suffix == "LL" || suffix == "UL" || suffix == "ULL")
696 return res;
697
698 throw RException(R__FAIL("invalid integer type token: " + uintToken));
699}
700
702{
703 auto am = cl->GetAttributeMap();
704 if (!am || !am->HasKey("rntuple.streamerMode"))
705 return ERNTupleSerializationMode::kUnset;
706
707 std::string value = am->GetPropertyAsString("rntuple.streamerMode");
708 std::transform(value.begin(), value.end(), value.begin(), ::toupper);
709 if (value == "TRUE") {
710 return ERNTupleSerializationMode::kForceStreamerMode;
711 } else if (value == "FALSE") {
712 return ERNTupleSerializationMode::kForceNativeMode;
713 } else {
714 R__LOG_WARNING(ROOT::Internal::NTupleLog()) << "invalid setting for 'rntuple.streamerMode' class attribute: "
715 << am->GetPropertyAsString("rntuple.streamerMode");
716 return ERNTupleSerializationMode::kUnset;
717 }
718}
719
720std::vector<std::string> ROOT::Internal::TokenizeTypeList(std::string_view templateType, std::size_t maxArgs)
721{
722 std::vector<std::string> result;
723 if (templateType.empty())
724 return result;
725
726 const char *eol = templateType.data() + templateType.length();
727 const char *typeBegin = templateType.data();
728 const char *typeCursor = templateType.data();
729 unsigned int nestingLevel = 0;
730 while (typeCursor != eol) {
731 switch (*typeCursor) {
732 case '<': ++nestingLevel; break;
733 case '>': --nestingLevel; break;
734 case ',':
735 if (nestingLevel == 0) {
736 result.push_back(std::string(typeBegin, typeCursor - typeBegin));
737 if (maxArgs && result.size() == maxArgs)
738 return result;
739 typeBegin = typeCursor + 1;
740 }
741 break;
742 }
743 typeCursor++;
744 }
745 result.push_back(std::string(typeBegin, typeCursor - typeBegin));
746 return result;
747}
748
750 const std::type_info &ti)
751{
752 // Fast path: the caller provided the expected type name (from RField<T>::TypeName())
754 return true;
755
756 // The type name may be equal to the alternative, short type name issued by Meta. This is a rare case used, e.g.,
757 // by the ATLAS DataVector class to hide a default template parameter from the on-disk type name.
758 // Thus, we check again using first ROOT Meta normalization followed by RNTuple re-normalization.
760}
761
763{
764 // Information to print in a single line of the type trace
765 struct RFieldInfo {
766 std::string fFieldName;
767 std::string fTypeName;
769 std::uint32_t fTypeVersion = 0;
770 std::optional<std::uint32_t> fTypeChecksum;
771 };
772
773 std::vector<const RFieldBase *> inMemoryStack;
774 std::vector<const RFieldDescriptor *> onDiskStack;
775
776 auto fnGetLine = [](const RFieldInfo &fieldInfo, int level) -> std::string {
777 std::string line = std::string(2 * level, ' ') + fieldInfo.fFieldName + " [" + fieldInfo.fTypeName;
778 if (fieldInfo.fTypeVersion > 0)
779 line += ", type version: " + std::to_string(fieldInfo.fTypeVersion);
780 if (fieldInfo.fTypeChecksum)
781 line += ", type checksum: " + std::to_string(*fieldInfo.fTypeChecksum);
782 line += "] (id: " + std::to_string(fieldInfo.fFieldId) + ")\n";
783 return line;
784 };
785
786 const RFieldBase *fieldPtr = &field;
787 while (fieldPtr->GetParent()) {
788 inMemoryStack.push_back(fieldPtr);
789 fieldPtr = fieldPtr->GetParent();
790 }
791
792 auto fieldId = field.GetOnDiskId();
793 while (fieldId != kInvalidDescriptorId && fieldId != desc.GetFieldZeroId()) {
794 const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
795 onDiskStack.push_back(&fieldDesc);
796 fieldId = fieldDesc.GetParentId();
797 }
798
799 std::string report = "In-memory field/type hierarchy:\n";
800 int indentLevel = 0;
801 for (auto itr = inMemoryStack.rbegin(); itr != inMemoryStack.rend(); ++itr, ++indentLevel) {
802 RFieldInfo fieldInfo;
803 fieldInfo.fFieldName = (*itr)->GetFieldName();
804 fieldInfo.fTypeName = (*itr)->GetTypeName();
805 fieldInfo.fFieldId = (*itr)->GetOnDiskId();
806 fieldInfo.fTypeVersion = (*itr)->GetTypeVersion();
807 if ((*itr)->GetTraits() & RFieldBase::kTraitTypeChecksum)
808 fieldInfo.fTypeChecksum = (*itr)->GetTypeChecksum();
809
811 }
812
813 report += "On-disk field/type hierarchy:\n";
814 indentLevel = 0;
815 for (auto itr = onDiskStack.rbegin(); itr != onDiskStack.rend(); ++itr, ++indentLevel) {
816 RFieldInfo fieldInfo;
817 fieldInfo.fFieldName = (*itr)->GetFieldName();
818 fieldInfo.fTypeName = (*itr)->GetTypeName();
819 fieldInfo.fFieldId = (*itr)->GetId();
820 fieldInfo.fTypeVersion = (*itr)->GetTypeVersion();
821 fieldInfo.fTypeChecksum = (*itr)->GetTypeChecksum();
822
824 }
825
826 return report;
827}
#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
#define R__LOG_WARNING(...)
Definition RLogger.hxx:358
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
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.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
#define _(A, B)
Definition cfortran.h:108
#define free
Definition civetweb.c:1578
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.
static std::string TypeName()
Definition RField.hxx:321
The on-storage metadata of an RNTuple.
const RFieldDescriptor & GetFieldDescriptor(ROOT::DescriptorId_t fieldId) const
ROOT::DescriptorId_t GetFieldZeroId() const
Returns the logical parent of all top-level RNTuple data fields.
const_iterator begin() const
const_iterator end() const
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2973
TDictAttributeMap * GetAttributeMap() const
TLine * line
const Int_t n
Definition legend1.C:16
ERNTupleSerializationMode
Possible settings for the "rntuple.streamerMode" class attribute in the dictionary.
std::vector< std::string > TokenizeTypeList(std::string_view templateType, std::size_t maxArgs=0)
Used in RFieldBase::Create() in order to get the comma-separated list of template types E....
ROOT::RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
unsigned long long ParseUIntTypeToken(const std::string &uintToken)
std::string GetNormalizedInteger(const std::string &intTemplateArg)
Appends 'll' or 'ull' to the where necessary and strips the suffix if not needed.
bool NeedsMetaNameAsAlias(const std::string &metaNormalizedName, std::string &renormalizedAlias, bool isArgInTemplatedUserClass=false)
Checks if the meta normalized name is different from the RNTuple normalized name in a way that would ...
ERNTupleSerializationMode GetRNTupleSerializationMode(TClass *cl)
std::string GetTypeTraceReport(const RFieldBase &field, const RNTupleDescriptor &desc)
Prints the hierarchy of types with their field names and field IDs for the given in-memory field and ...
std::string GetCanonicalTypePrefix(const std::string &typeName)
Applies RNTuple specific type name normalization rules (see specs) that help the string parsing in RF...
std::string GetNormalizedUnresolvedTypeName(const std::string &origName)
Applies all RNTuple type normalization rules except typedef resolution.
bool IsMatchingFieldType(const std::string &actualTypeName)
Helper to check if a given type name is the one expected of Field<T>.
Definition RField.hxx:557
std::string GetRenormalizedTypeName(const std::string &metaNormalizedName)
Given a type name normalized by ROOT meta, renormalize it for RNTuple. E.g., insert std::prefix.
long long ParseIntTypeToken(const std::string &intToken)
std::string GetDemangledTypeName(const std::type_info &t)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr DescriptorId_t kInvalidDescriptorId
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 ...
char * DemangleName(const char *mangled_name, int &errorCode)
Definition TClassEdit.h:255
@ kDropComparator
Definition TClassEdit.h:84
@ kDropStlDefault
Definition TClassEdit.h:83