Logo ROOT  
Reference Guide
RElement.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
10
12#include <ROOT/RLogger.hxx>
13#include "TBufferJSON.h"
14
15using namespace ROOT::Experimental::Browsable;
16using namespace std::string_literals;
17
19 static RLogChannel sLog("ROOT.Browsable");
20 return sLog;
21}
22
23
24/////////////////////////////////////////////////////////////////////
25/// Returns child iterator (if any)
26
27std::unique_ptr<RLevelIter> RElement::GetChildsIter()
28{
29 return nullptr;
30}
31
32/////////////////////////////////////////////////////////////////////
33/// Returns number of childs
34/// By default creates iterator and iterates over all items
35
37{
38 auto iter = GetChildsIter();
39 if (!iter) return 0;
40 int cnt = 0;
41 while (iter->Next()) cnt++;
42 return cnt;
43}
44
45/////////////////////////////////////////////////////////////////////
46/// Find item with specified name
47/// Default implementation, should work for all
48
50{
51 std::string lkind = kind;
52 std::transform(lkind.begin(), lkind.end(), lkind.begin(), ::tolower);
53
54 if (lkind == "text") return kText;
55 if ((lkind == "image") || (lkind == "image64")) return kImage;
56 if (lkind == "png") return kPng;
57 if ((lkind == "jpg") || (lkind == "jpeg")) return kJpeg;
58 if (lkind == "json") return kJson;
59 if (lkind == "filename") return kFileName;
60 return kNone;
61}
62
63/////////////////////////////////////////////////////////////////////
64/// Returns sub element
65
66std::shared_ptr<RElement> RElement::GetSubElement(std::shared_ptr<RElement> &elem, const RElementPath_t &path)
67{
68 auto curr = elem;
69
70 for (auto &itemname : path) {
71 if (!curr)
72 return nullptr;
73
74 auto iter = curr->GetChildsIter();
75 if (!iter || !iter->Find(itemname))
76 return nullptr;
77
78 curr = iter->GetElement();
79 }
80
81 return curr;
82}
83
84/////////////////////////////////////////////////////////////////////
85/// Returns string content like text file content or json representation
86
87std::string RElement::GetContent(const std::string &kind)
88{
89 if (GetContentKind(kind) == kJson) {
90 auto obj = GetObject();
91 if (obj)
92 return TBufferJSON::ConvertToJSON(obj->GetObject(), obj->GetClass()).Data();
93 }
94
95 return ""s;
96}
97
98/////////////////////////////////////////////////////////////////////
99/// Parse string path to produce RElementPath_t
100/// One should avoid to use string pathes as much as possible
101
102RElementPath_t RElement::ParsePath(const std::string &strpath)
103{
104 RElementPath_t arr;
105 if (strpath.empty())
106 return arr;
107
108 std::string slash = "/";
109
110 std::string::size_type previous = 0;
111 if (strpath[0] == slash[0]) previous++;
112
113 auto current = strpath.find(slash, previous);
114 while (current != std::string::npos) {
115 if (current > previous)
116 arr.emplace_back(strpath.substr(previous, current - previous));
117 previous = current + 1;
118 current = strpath.find(slash, previous);
119 }
120
121 if (previous < strpath.length())
122 arr.emplace_back(strpath.substr(previous));
123
124 return arr;
125}
126
127/////////////////////////////////////////////////////////////////////
128/// Compare two paths,
129/// Returns number of elements matches in both paths
130
132{
133 int sz = path1.size();
134 if (sz > (int) path2.size()) sz = path2.size();
135
136 for (int n = 0; n < sz; ++n)
137 if (path1[n] != path2[n])
138 return n;
139
140 return sz;
141}
142
143/////////////////////////////////////////////////////////////////////
144/// Converts element path back to string
145
147{
148 std::string res;
149 for (auto &elem : path) {
150 res.append("/");
151 std::string subname = elem;
152 ExtractItemIndex(subname);
153 res.append(subname);
154 }
155
156 return res;
157}
158
159/////////////////////////////////////////////////////////////////////
160/// Extract index from name
161/// Index coded by client with ###<indx>$$$ suffix
162/// Such coding used by browser to identify element by index
163
165{
166 auto p1 = name.rfind("###"), p2 = name.rfind("$$$");
167 if ((p1 == std::string::npos) || (p2 == std::string::npos) || (p1 >= p2) || (p2 != name.length()-3)) return -1;
168
169 int indx = std::stoi(name.substr(p1+3,p2-p1-3));
170 name.resize(p1);
171 return indx;
172}
char name[80]
Definition: TGX11.cxx:110
@ kFileName
"filename" - file name if applicable
Definition: RElement.hxx:43
@ kJson
"json" representation of object, can be used in code editor
Definition: RElement.hxx:42
@ kImage
"image64" - base64 for supported image formats (png/gif/gpeg)
Definition: RElement.hxx:39
@ kJpeg
"jpg" or "jpeg" - plain jpg binary code, returned inside std::string
Definition: RElement.hxx:41
@ kPng
"png" - plain png binary code, returned inside std::string
Definition: RElement.hxx:40
@ kText
"text" - plain text for code editor
Definition: RElement.hxx:38
static EContentKind GetContentKind(const std::string &kind)
Find item with specified name Default implementation, should work for all.
Definition: RElement.cxx:49
virtual std::string GetContent(const std::string &="text")
Returns element content, depends from kind.
Definition: RElement.cxx:87
virtual std::unique_ptr< RLevelIter > GetChildsIter()
Create iterator for childs elements if any.
Definition: RElement.cxx:27
virtual int GetNumChilds()
Returns number of childs By default creates iterator and iterates over all items.
Definition: RElement.cxx:36
static int ExtractItemIndex(std::string &name)
Extract index from name Index coded by client with ###<indx>$$$ suffix Such coding used by browser to...
Definition: RElement.cxx:164
static int ComparePaths(const RElementPath_t &path1, const RElementPath_t &path2)
Compare two paths, Returns number of elements matches in both paths.
Definition: RElement.cxx:131
static std::string GetPathAsString(const RElementPath_t &path)
Converts element path back to string.
Definition: RElement.cxx:146
static std::shared_ptr< RElement > GetSubElement(std::shared_ptr< RElement > &elem, const RElementPath_t &path)
Returns sub element.
Definition: RElement.cxx:66
virtual std::unique_ptr< RHolder > GetObject()
Access object.
Definition: RElement.hxx:80
static RElementPath_t ParsePath(const std::string &str)
Parse string path to produce RElementPath_t One should avoid to use string pathes as much as possible...
Definition: RElement.cxx:102
A log configuration for a channel, e.g.
Definition: RLogger.hxx:101
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=nullptr)
Converts object, inherited from TObject class, to JSON string Lower digit of compact parameter define...
const char * Data() const
Definition: TString.h:369
const Int_t n
Definition: legend1.C:16
std::vector< std::string > RElementPath_t
Definition: RElement.hxx:21
RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition: RElement.cxx:18
static constexpr double s
const char * cnt
Definition: TXMLSetup.cxx:75
TCanvas * slash()
Definition: slash.C:1