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