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