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 <RooAbsPdf.h>
16
17#include <TClass.h>
18
19#include <iostream>
20
21namespace RooFit::JSONIO {
22
24{
25 static ImportMap _importers;
26 return _importers;
27}
28
29namespace {
30
31auto &exportersToAdd()
32{
33 static std::map<const std::string, std::vector<std::unique_ptr<const Exporter>>> toAdd;
34 return toAdd;
35}
36
37ExportMap &exportersImpl()
38{
39 static ExportMap _exporters;
40 return _exporters;
41}
42
43} // namespace
44
46{
47 // If there are exporters to be added, do this now.
48 for (auto &item : exportersToAdd()) {
49 TClass *klass = TClass::GetClass(item.first.c_str());
50 auto &exporters = exportersImpl()[klass]; // registered exporters so far
51 auto &toAdd = item.second; // exporters to add
52
53 // Find the nullptr separator
54 auto nullIt = std::find(toAdd.begin(), toAdd.end(), nullptr);
55
56 if (nullIt == toAdd.end()) {
57 throw std::runtime_error("toAdd does not contain nullptr separator");
58 }
59
60 // Move elements after nullptr to the back
61 exporters.insert(exporters.end(), std::make_move_iterator(nullIt + 1), std::make_move_iterator(toAdd.end()));
62
63 // Move elements before nullptr to the front
64 exporters.insert(exporters.begin(), std::make_move_iterator(toAdd.begin()), std::make_move_iterator(nullIt));
65
66 toAdd.clear();
67 }
68 exportersToAdd().clear();
69 return exportersImpl();
70}
71
72bool registerImporter(const std::string &key, std::unique_ptr<const Importer> f, bool topPriority)
73{
74 auto &vec = importers()[key];
75 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
76 return true;
77}
78
79bool registerExporter(const TClass *key, std::unique_ptr<const Exporter> f, bool topPriority)
80{
81 auto &vec = exporters()[key];
82 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
83 return true;
84}
85
86bool registerExporter(const std::string &key, std::unique_ptr<const Exporter> f, bool topPriority)
87{
88 auto &vec = exportersToAdd()[key];
89 // The vector starts out with just a nullptr separator. Elements before it
90 // will be added to the top of the exporter queue, and the elements after
91 // get appended to the end.
92 if (vec.empty()) {
93 vec.emplace_back(nullptr);
94 }
95 vec.insert(topPriority ? vec.begin() : vec.end(), std::move(f));
96 return true;
97}
98
99namespace {
100
101template <class Map>
102int removeByTypeName(Map &map, const std::string &needle)
103{
104 int n = 0;
105 for (auto &element : map) {
106 for (size_t i = element.second.size(); i > 0; --i) {
107 auto *imp = element.second[i - 1].get();
108 std::string name(typeid(*imp).name());
109 if (name.find(needle) != std::string::npos) {
110 element.second.erase(element.second.begin() + i - 1);
111 ++n;
112 }
113 }
114 }
115 return n;
116}
117
118template <class Map, class KeyPrinter>
119void printByTypeName(Map &map, KeyPrinter printKey)
120{
121 for (const auto &x : map) {
122 for (const auto &ePtr : x.second) {
123 // Passing *e directory to typeid results in clang warnings.
124 auto const &e = *ePtr;
125 std::cout << printKey(x.first) << "\t" << typeid(e).name() << std::endl;
126 }
127 }
128}
129
130} // namespace
131
132int removeImporters(const std::string &needle)
133{
134 return removeByTypeName(importers(), needle);
135}
136
137int removeExporters(const std::string &needle)
138{
139 return removeByTypeName(exporters(), needle);
140}
141
143{
144 printByTypeName(importers(), [](auto const &key) { return key; });
145}
147{
148 printByTypeName(exporters(), [](auto const &key) { return key->GetName(); });
149}
150
151} // namespace RooFit::JSONIO
#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:23
ExportMap & exporters()
Definition JSONIO.cxx:45
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:132
int removeExporters(const std::string &needle)
Definition JSONIO.cxx:137
std::map< TClass const *, std::vector< std::unique_ptr< const Exporter > > > ExportMap
Definition JSONIO.h:67
void printExporters()
Definition JSONIO.cxx:146
void printImporters()
Definition JSONIO.cxx:142
std::map< const std::string, std::vector< std::unique_ptr< const Importer > > > ImportMap
Definition JSONIO.h:66