Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONIO.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Carsten D. Burgard, DESY/ATLAS, Dec 2021
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include <RooFitHS3/JSONIO.h>
14
15#include <TClass.h>
16
17#include <iostream>
18
19namespace RooFit::JSONIO {
20
22{
23 static ImportMap _importers;
24 return _importers;
25}
26
27namespace {
28
29// An exporter that was registered by the name of its class, for which there might have been no TClass yet.
30struct ExporterByName {
31 std::string className;
32 std::unique_ptr<const Exporter> exporter;
33 bool topPriority = true;
34};
35
36std::vector<ExporterByName> &exportersByName()
37{
38 static std::vector<ExporterByName> _exportersByName;
39 return _exportersByName;
40}
41
42} // namespace
43
45{
46 static ExportMap _exporters;
47 // The exporters that were registered by class name are registered with their TClass now, in the same order.
48 std::vector<ExporterByName> byName;
49 byName.swap(exportersByName());
50 for (auto &item : byName) {
51 registerExporter(TClass::GetClass(item.className.c_str()), std::move(item.exporter), item.topPriority);
52 }
53 return _exporters;
54}
55
56bool registerImporter(const std::string &key, std::unique_ptr<const Importer> f, bool topPriority)
57{
58 auto &vec = importers()[key];
59 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
60 return true;
61}
62
63bool registerExporter(const TClass *key, std::unique_ptr<const Exporter> f, bool topPriority)
64{
65 auto &vec = exporters()[key];
66 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
67 return true;
68}
69
70bool registerExporter(const std::string &key, std::unique_ptr<const Exporter> f, bool topPriority)
71{
72 exportersByName().push_back({key, std::move(f), topPriority});
73 return true;
74}
75
76namespace {
77
78template <class Map>
79int removeByTypeName(Map &map, const std::string &needle)
80{
81 int n = 0;
82 for (auto &element : map) {
83 for (size_t i = element.second.size(); i > 0; --i) {
84 auto *imp = element.second[i - 1].get();
85 std::string name(typeid(*imp).name());
86 if (name.find(needle) != std::string::npos) {
87 element.second.erase(element.second.begin() + i - 1);
88 ++n;
89 }
90 }
91 }
92 return n;
93}
94
95template <class Map, class KeyPrinter>
96void printByTypeName(Map &map, KeyPrinter printKey)
97{
98 for (const auto &x : map) {
99 for (const auto &ePtr : x.second) {
100 // Passing *e directory to typeid results in clang warnings.
101 auto const &e = *ePtr;
102 std::cout << printKey(x.first) << "\t" << typeid(e).name() << std::endl;
103 }
104 }
105}
106
107} // namespace
108
109int removeImporters(const std::string &needle)
110{
111 return removeByTypeName(importers(), needle);
112}
113
114int removeExporters(const std::string &needle)
115{
116 return removeByTypeName(exporters(), needle);
117}
118
120{
121 printByTypeName(importers(), [](auto const &key) { return key; });
122}
124{
125 printByTypeName(exporters(), [](auto const &key) { return key->GetName(); });
126}
127
128} // namespace RooFit::JSONIO
std::string className
Definition JSONIO.cxx:31
bool topPriority
Definition JSONIO.cxx:33
std::unique_ptr< const Exporter > exporter
Definition JSONIO.cxx:32
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
char name[80]
Definition TGX11.cxx:142
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:2999
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
ImportMap & importers()
Definition JSONIO.cxx:21
ExportMap & exporters()
Definition JSONIO.cxx:44
static bool registerImporter(const std::string &key, bool topPriority=true)
Definition JSONIO.h:73
static bool registerExporter(const TClass *key, bool topPriority=true)
Definition JSONIO.h:78
int removeImporters(const std::string &needle)
Definition JSONIO.cxx:109
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:114
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
Definition JSONIO.h:67
void printExporters()
Definition JSONIO.cxx:123
void printImporters()
Definition JSONIO.cxx:119
std::map< const std::string, std::vector< std::unique_ptr< const Importer > > > ImportMap
Definition JSONIO.h:66