Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13#include <ROOT/RLogger.hxx>
14
15#include "TBufferJSON.h"
16
17using namespace ROOT::Experimental::Browsable;
18using namespace std::string_literals;
19
21 static RLogChannel sLog("ROOT.Browsable");
22 return sLog;
23}
24
25
26/////////////////////////////////////////////////////////////////////
27/// Returns child iterator (if any)
28
29std::unique_ptr<RLevelIter> RElement::GetChildsIter()
30{
31 return nullptr;
32}
33
34/////////////////////////////////////////////////////////////////////
35/// Returns number of childs
36/// By default creates iterator and iterates over all items
37
39{
40 auto iter = GetChildsIter();
41 if (!iter) return 0;
42 int cnt = 0;
43 while (iter->Next()) cnt++;
44 return cnt;
45}
46
47/////////////////////////////////////////////////////////////////////
48/// Find item with specified name
49/// Default implementation, should work for all
50
52{
53 std::string lkind = kind;
54 std::transform(lkind.begin(), lkind.end(), lkind.begin(), ::tolower);
55
56 if (lkind == "text") return kText;
57 if ((lkind == "image") || (lkind == "image64")) return kImage;
58 if (lkind == "png") return kPng;
59 if ((lkind == "jpg") || (lkind == "jpeg")) return kJpeg;
60 if (lkind == "json") return kJson;
61 if (lkind == "filename") return kFileName;
62 return kNone;
63}
64
65/////////////////////////////////////////////////////////////////////
66/// Returns sub element
67
68std::shared_ptr<RElement> RElement::GetSubElement(std::shared_ptr<RElement> &elem, const RElementPath_t &path)
69{
70 auto curr = elem;
71
72 for (auto &itemname : path) {
73 if (!curr)
74 return nullptr;
75
76 auto iter = curr->GetChildsIter();
77 if (!iter || !iter->Find(itemname))
78 return nullptr;
79
80 curr = iter->GetElement();
81 }
82
83 return curr;
84}
85
86/////////////////////////////////////////////////////////////////////
87/// Returns string content like text file content or json representation
88
89std::string RElement::GetContent(const std::string &kind)
90{
91 if (GetContentKind(kind) == kJson) {
92 auto obj = GetObject();
93 if (obj)
94 return TBufferJSON::ConvertToJSON(obj->GetObject(), obj->GetClass()).Data();
95 }
96
97 return ""s;
98}
99
100
101/////////////////////////////////////////////////////////////////////
102/// Returns item with element description
103
104std::unique_ptr<RItem> RElement::CreateItem() const
105{
106 auto item = std::make_unique<RItem>(GetName());
107 item->SetTitle(GetTitle());
108 return item;
109}
110
111/////////////////////////////////////////////////////////////////////
112/// Parse string path to produce RElementPath_t
113/// One should avoid to use string pathes as much as possible
114
115RElementPath_t RElement::ParsePath(const std::string &strpath)
116{
117 RElementPath_t arr;
118 if (strpath.empty())
119 return arr;
120
121 std::string slash = "/";
122
123 std::string::size_type previous = 0;
124 if (strpath[0] == slash[0]) previous++;
125
126 auto current = strpath.find(slash, previous);
127 while (current != std::string::npos) {
128 if (current > previous)
129 arr.emplace_back(strpath.substr(previous, current - previous));
130 previous = current + 1;
131 current = strpath.find(slash, previous);
132 }
133
134 if (previous < strpath.length())
135 arr.emplace_back(strpath.substr(previous));
136
137 return arr;
138}
139
140/////////////////////////////////////////////////////////////////////
141/// Compare two paths,
142/// Returns number of elements matches in both paths
143
145{
146 int sz = path1.size();
147 if (sz > (int) path2.size()) sz = path2.size();
148
149 for (int n = 0; n < sz; ++n)
150 if (path1[n] != path2[n])
151 return n;
152
153 return sz;
154}
155
156/////////////////////////////////////////////////////////////////////
157/// Converts element path back to string
158
160{
161 std::string res;
162 for (auto &elem : path) {
163 res.append("/");
164 std::string subname = elem;
165 ExtractItemIndex(subname);
166 res.append(subname);
167 }
168
169 return res;
170}
171
172/////////////////////////////////////////////////////////////////////
173/// Extract index from name
174/// Index coded by client with `###<indx>$$$` suffix
175/// Such coding used by browser to identify element by index
176
178{
179 auto p1 = name.rfind("###"), p2 = name.rfind("$$$");
180 if ((p1 == std::string::npos) || (p2 == std::string::npos) || (p1 >= p2) || (p2 != name.length()-3)) return -1;
181
182 int indx = std::stoi(name.substr(p1+3,p2-p1-3));
183 name.resize(p1);
184 return indx;
185}
char name[80]
Definition TGX11.cxx:110
@ kFileName
"filename" - file name if applicable
Definition RElement.hxx:45
@ kJson
"json" representation of object, can be used in code editor
Definition RElement.hxx:44
@ kImage
"image64" - base64 for supported image formats (png/gif/gpeg)
Definition RElement.hxx:41
@ kJpeg
"jpg" or "jpeg" - plain jpg binary code, returned inside std::string
Definition RElement.hxx:43
@ kPng
"png" - plain png binary code, returned inside std::string
Definition RElement.hxx:42
@ kText
"text" - plain text for code editor
Definition RElement.hxx:40
static EContentKind GetContentKind(const std::string &kind)
Find item with specified name Default implementation, should work for all.
Definition RElement.cxx:51
virtual std::string GetName() const =0
Name of browsable, must be provided in derived classes.
virtual std::string GetContent(const std::string &="text")
Returns element content, depends from kind.
Definition RElement.cxx:89
virtual std::unique_ptr< RLevelIter > GetChildsIter()
Create iterator for childs elements if any.
Definition RElement.cxx:29
virtual int GetNumChilds()
Returns number of childs By default creates iterator and iterates over all items.
Definition RElement.cxx:38
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:177
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:144
static std::string GetPathAsString(const RElementPath_t &path)
Converts element path back to string.
Definition RElement.cxx:159
static std::shared_ptr< RElement > GetSubElement(std::shared_ptr< RElement > &elem, const RElementPath_t &path)
Returns sub element.
Definition RElement.cxx:68
virtual std::unique_ptr< RHolder > GetObject()
Access object.
Definition RElement.hxx:81
virtual std::unique_ptr< RItem > CreateItem() const
Returns item with element description.
Definition RElement.cxx:104
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:115
virtual std::string GetTitle() const
Title of browsable (optional)
Definition RElement.hxx:72
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:380
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:20
TCanvas * slash()
Definition slash.C:1