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 RooFitHS3_JSONInterface_h
14#define RooFitHS3_JSONInterface_h
15
16#include <iostream>
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <vector>
21
22namespace RooFit {
23namespace Experimental {
24
25class JSONNode {
26public:
27 template <class Nd>
29 public:
30 class Impl {
31 public:
32 virtual ~Impl() = default;
33 virtual std::unique_ptr<Impl> clone() const = 0;
34 virtual void forward() = 0;
35 virtual void backward() = 0;
36 virtual Nd &current() = 0;
37 virtual bool equal(const Impl &other) const = 0;
38 };
39
40 private:
41 std::unique_ptr<Impl> it;
42
43 public:
44 child_iterator_t(std::unique_ptr<Impl> impl) : it(std::move(impl)) {}
45 child_iterator_t(const child_iterator_t &other) : it(std::move(other.it->clone())) {}
46
48 {
49 it->forward();
50 return *this;
51 }
53 {
54 it->backward();
55 return *this;
56 }
57 Nd &operator*() const { return it->current(); }
58 Nd &operator->() const { return it->current(); }
59
60 bool operator!=(const child_iterator_t &that) const { return !this->it->equal(*that.it); }
61 bool operator==(const child_iterator_t &that) const { return this->it->equal(*that.it); }
62 };
63
66
67 template <class Nd>
70
71 public:
72 inline children_view_t(child_iterator_t<Nd> const &b_, child_iterator_t<Nd> const &e_) : b(b_), e(e_) {}
73
74 inline child_iterator_t<Nd> begin() const { return b; }
75 inline child_iterator_t<Nd> end() const { return e; }
76 };
77
78public:
79 virtual void writeJSON(std::ostream &os) const = 0;
80 virtual void writeYML(std::ostream &) const { throw std::runtime_error("YML not supported"); }
81
82public:
83 virtual JSONNode &operator<<(std::string const &s) = 0;
84 virtual JSONNode &operator<<(int i) = 0;
85 virtual JSONNode &operator<<(double d) = 0;
86 template <class T>
87 JSONNode &operator<<(const std::vector<T> &v)
88 {
89 this->set_seq();
90 for (const auto &e : v) {
91 this->append_child() << e;
92 };
93 return *this;
94 }
95 virtual const JSONNode &operator>>(std::string &v) const = 0;
96 virtual JSONNode &operator[](std::string const &k) = 0;
97 virtual JSONNode &operator[](size_t pos) = 0;
98 virtual const JSONNode &operator[](std::string const &k) const = 0;
99 virtual const JSONNode &operator[](size_t pos) const = 0;
100 virtual bool is_container() const = 0;
101 virtual bool is_map() const = 0;
102 virtual bool is_seq() const = 0;
103 virtual void set_map() = 0;
104 virtual void set_seq() = 0;
105
106 virtual std::string key() const = 0;
107 virtual std::string val() const = 0;
108 virtual int val_int() const { return atoi(this->val().c_str()); }
109 virtual float val_float() const { return atof(this->val().c_str()); }
110 virtual bool val_bool() const { return atoi(this->val().c_str()); }
111 template <class T>
112 T val_t() const;
113 virtual bool has_key() const = 0;
114 virtual bool has_val() const = 0;
115 virtual bool has_child(std::string const &) const = 0;
116 virtual JSONNode &append_child() = 0;
117 virtual size_t num_children() const = 0;
118
121
122 virtual children_view children();
123 virtual const_children_view children() const;
124 virtual JSONNode &child(size_t pos) = 0;
125 virtual const JSONNode &child(size_t pos) const = 0;
126};
127
128} // namespace Experimental
129} // namespace RooFit
130
131class JSONTree {
133};
134
135std::ostream &operator<<(std::ostream &os, RooFit::Experimental::JSONNode const &s);
136template <class T>
137std::vector<T> &operator<<(std::vector<T> &v, RooFit::Experimental::JSONNode const &n)
138{
139 if (!n.is_seq()) {
140 throw std::runtime_error("node " + n.key() + " is not of sequence type!");
141 }
142 for (const auto &e : n.children()) {
143 v.push_back(e.val_t<T>());
144 }
145 return v;
146}
147
148#endif
#define d(i)
Definition RSha256.hxx:102
#define e(i)
Definition RSha256.hxx:103
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
virtual RooFit::Experimental::JSONNode & rootnode()=0
virtual bool equal(const Impl &other) const =0
virtual std::unique_ptr< Impl > clone() const =0
bool operator==(const child_iterator_t &that) const
child_iterator_t(std::unique_ptr< Impl > impl)
child_iterator_t(const child_iterator_t &other)
bool operator!=(const child_iterator_t &that) const
children_view_t(child_iterator_t< Nd > const &b_, child_iterator_t< Nd > const &e_)
virtual JSONNode & child(size_t pos)=0
virtual void writeYML(std::ostream &) const
virtual std::string val() const =0
virtual JSONNode & operator[](size_t pos)=0
virtual std::string key() const =0
virtual bool has_child(std::string const &) const =0
virtual JSONNode & operator<<(std::string const &s)=0
virtual const JSONNode & child(size_t pos) const =0
virtual JSONNode & operator<<(int i)=0
virtual size_t num_children() const =0
virtual const JSONNode & operator[](size_t pos) const =0
virtual JSONNode & operator[](std::string const &k)=0
virtual bool is_seq() const =0
virtual float val_float() const
virtual bool val_bool() const
virtual bool has_val() const =0
virtual bool has_key() const =0
virtual const JSONNode & operator[](std::string const &k) const =0
virtual void writeJSON(std::ostream &os) const =0
virtual JSONNode & operator<<(double d)=0
virtual bool is_container() const =0
virtual JSONNode & append_child()=0
virtual const JSONNode & operator>>(std::string &v) const =0
virtual children_view children()
virtual bool is_map() const =0
JSONNode & operator<<(const std::vector< T > &v)
const Int_t n
Definition legend1.C:16
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18