Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
JSONInterface.h
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#ifndef RooFit_Detail_JSONInterface_h
14#define RooFit_Detail_JSONInterface_h
15
16#include <iostream>
17#include <memory>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <vector>
22
23namespace RooFit {
24namespace Detail {
25
26class JSONNode {
27public:
28 template <class Nd>
30 public:
31 explicit child_iterator_t(Nd *const *pos) : _pos(pos) {}
32
34 {
35 ++_pos;
36 return *this;
37 }
38 Nd &operator*() const { return **_pos; }
39
40 friend bool operator!=(child_iterator_t const &lhs, child_iterator_t const &rhs) { return lhs._pos != rhs._pos; }
41 friend bool operator==(child_iterator_t const &lhs, child_iterator_t const &rhs) { return lhs._pos == rhs._pos; }
42
43 private:
44 Nd *const *_pos;
45 };
46
49
50 template <class Nd>
53
54 public:
55 inline children_view_t(child_iterator_t<Nd> const &b_, child_iterator_t<Nd> const &e_) : b(b_), e(e_) {}
56
57 inline child_iterator_t<Nd> begin() const { return b; }
58 inline child_iterator_t<Nd> end() const { return e; }
59 };
60
61 // A node refers to a value of its tree. Copying it would copy that reference, not the value.
62 JSONNode(JSONNode const &) = delete;
63 JSONNode &operator=(JSONNode const &) = delete;
64
65 void writeJSON(std::ostream &os) const;
66
67 JSONNode &operator<<(std::string const &s);
68 inline JSONNode &operator<<(const char *s) { return *this << std::string(s); }
69 JSONNode &operator<<(int i);
70 JSONNode &operator<<(double d);
71 JSONNode &operator<<(bool b);
72 JSONNode &operator[](std::string const &k);
73 const JSONNode &operator[](std::string const &k) const;
74 bool is_container() const;
75 bool is_map() const;
76 bool is_seq() const;
77 bool is_null() const;
78 bool is_number() const;
82 void clear();
83 /// Remove a child of a map, if it exists.
84 void erase(std::string const &key);
85
86 std::string key() const;
87 std::string val() const;
88 int val_int() const;
89 double val_double() const;
90 bool val_bool() const;
91 /// The value as a boolean, or std::nullopt if it is none. Like val_bool(), the integers 0 and 1 are accepted.
92 std::optional<bool> try_val_bool() const;
93 template <class T>
94 T val_t() const;
95 bool has_val() const;
96 bool has_child(std::string const &) const;
98 size_t num_children() const;
99
102
105 JSONNode &child(size_t pos);
106 const JSONNode &child(size_t pos) const;
107
108 template <typename Collection>
109 void fill_seq(Collection const &coll)
110 {
111 set_seq();
112 for (auto const &item : coll) {
113 append_child() << item;
114 }
115 }
116
117 template <typename Matrix>
118 void fill_mat(Matrix const &mat)
119 {
120 set_seq();
121 for (int i = 0; i < mat.GetNrows(); ++i) {
122 auto &row = append_child();
123 row.set_seq();
124 for (int j = 0; j < mat.GetNcols(); ++j) {
125 row.append_child() << mat(i, j);
126 }
127 }
128 }
129
130 /// The component at the end of a path of keys, or nullptr if a component on the path doesn't exist.
131 JSONNode const *find(std::string const &key) const { return has_child(key) ? &(*this)[key] : nullptr; }
132
133 template <typename... Keys_t>
134 JSONNode const *find(std::string const &key, Keys_t const &...keys) const
135 {
136 return has_child(key) ? (*this)[key].find(keys...) : nullptr;
137 }
138
139 /// The component at the end of a path of keys. The nodes on the path become maps.
140 JSONNode &get(std::string const &key) { return set_map()[key]; }
141
142 template <typename... Keys_t>
143 JSONNode &get(std::string const &key, Keys_t const &...keys)
144 {
145 return get(key).get(keys...);
146 }
147
148private:
149 friend class JSONTree;
150 friend class JSONReadRecorder;
151
152 // Owns the values and the nodes of a tree. It is defined in the only source file that knows the JSON library.
153 class Storage;
154
155 JSONNode(Storage &storage, void *value, std::string key);
156
158 void *_value; // of the value type of the JSON library
159 std::string _key;
160};
161
162class JSONTree {
163public:
165
167
168 static std::unique_ptr<JSONTree> create();
169 static std::unique_ptr<JSONTree> create(std::istream &is);
170 static std::unique_ptr<JSONTree> create(std::string const &str);
171
172private:
173 explicit JSONTree(std::unique_ptr<JSONNode::Storage> storage);
174
175 std::unique_ptr<JSONNode::Storage> _storage;
176};
177
178template <class T>
179std::vector<T> &operator<<(std::vector<T> &v, RooFit::Detail::JSONNode const &n)
180{
181 if (!n.is_seq()) {
182 throw std::runtime_error("node " + n.key() + " is not of sequence type!");
183 }
184 for (const auto &e : n.children()) {
185 v.push_back(e.val_t<T>());
186 }
187 return v;
188}
189
190template <>
191inline int JSONNode::val_t<int>() const
192{
193 return val_int();
194}
195template <>
196inline double JSONNode::val_t<double>() const
197{
198 return val_double();
199}
200template <>
201inline bool JSONNode::val_t<bool>() const
202{
203 return val_bool();
204}
205template <>
206inline std::string JSONNode::val_t<std::string>() const
207{
208 return val();
209}
210
211} // namespace Detail
212} // namespace RooFit
213
214#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define e(i)
Definition RSha256.hxx:103
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 Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
friend bool operator==(child_iterator_t const &lhs, child_iterator_t const &rhs)
friend bool operator!=(child_iterator_t const &lhs, child_iterator_t const &rhs)
child_iterator_t< Nd > begin() const
children_view_t(child_iterator_t< Nd > const &b_, child_iterator_t< Nd > const &e_)
child_iterator_t< Nd > end() const
JSONNode & operator[](std::string const &k)
JSONNode & get(std::string const &key)
The component at the end of a path of keys. The nodes on the path become maps.
void erase(std::string const &key)
Remove a child of a map, if it exists.
JSONNode & get(std::string const &key, Keys_t const &...keys)
void fill_seq(Collection const &coll)
JSONNode const * find(std::string const &key, Keys_t const &...keys) const
void writeJSON(std::ostream &os) const
JSONNode & operator<<(std::string const &s)
std::string val() const
JSONNode & operator=(JSONNode const &)=delete
JSONNode & operator<<(const char *s)
void fill_mat(Matrix const &mat)
JSONNode const * find(std::string const &key) const
The component at the end of a path of keys, or nullptr if a component on the path doesn't exist.
JSONNode(JSONNode const &)=delete
bool has_child(std::string const &) const
std::string key() const
std::optional< bool > try_val_bool() const
The value as a boolean, or std::nullopt if it is none. Like val_bool(), the integers 0 and 1 are acce...
Records which components of a JSON map are read while it is alive, through any node of the tree.
static std::unique_ptr< JSONTree > create()
JSONTree(std::unique_ptr< JSONNode::Storage > storage)
std::unique_ptr< JSONNode::Storage > _storage
Int_t GetNrows() const
Int_t GetNcols() const
const Int_t n
Definition legend1.C:16
std::vector< T > & operator<<(std::vector< T > &v, RooFit::Detail::JSONNode const &n)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73