Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TKeyXML.cxx
Go to the documentation of this file.
1// @(#)root/xml:$Id$
2// Author: Sergey Linev, Rene Brun 10.05.2004
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//________________________________________________________________________
13//
14// TKeyXML is represents one block of data in TXMLFile
15// Normally this block corresponds to data of single object like histogram,
16// TObjArray and so on.
17//________________________________________________________________________
18
19#include "TKeyXML.h"
20
21#include "TBufferXML.h"
22#include "TXMLFile.h"
23#include "TClass.h"
24#include "TROOT.h"
25
26
27////////////////////////////////////////////////////////////////////////////////
28/// Creates TKeyXML and convert object data to xml structures
29
30TKeyXML::TKeyXML(TDirectory *mother, Long64_t keyid, const TObject *obj, const char *name, const char *title)
31 : TKey(mother), fKeyNode(nullptr), fKeyId(keyid), fSubdir(kFALSE)
32{
33 if (name) {
35 } else if (obj) {
36 SetName(obj->GetName());
37 fClassName = obj->ClassName();
38 } else
39 SetName("Noname");
40
41 if (title)
42 SetTitle(title);
43
44 fCycle = GetMotherDir()->AppendKey(this);
45
47 if (xml)
48 fKeyNode = xml->NewChild(nullptr, nullptr, xmlio::Xmlkey);
49
50 fDatime.Set();
51
52 StoreObject(obj, nullptr, kTRUE);
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Creates TKeyXML and convert object data to xml structures
57
58TKeyXML::TKeyXML(TDirectory *mother, Long64_t keyid, const void *obj, const TClass *cl, const char *name,
59 const char *title)
60 : TKey(mother), fKeyNode(nullptr), fKeyId(keyid), fSubdir(kFALSE)
61{
62 if (name && *name)
64 else
65 SetName(cl ? cl->GetName() : "Noname");
66
67 if (title)
68 SetTitle(title);
69
70 fCycle = GetMotherDir()->AppendKey(this);
71
73 if (xml)
74 fKeyNode = xml->NewChild(nullptr, nullptr, xmlio::Xmlkey);
75
76 fDatime.Set();
77
78 StoreObject(obj, cl, kFALSE);
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Creates TKeyXML and takes ownership over xml node, from which object can be restored
83
85 : TKey(mother), fKeyNode(keynode), fKeyId(keyid), fSubdir(kFALSE)
86{
88
89 SetName(xml->GetAttr(keynode, xmlio::Name));
90
91 if (xml->HasAttr(keynode, xmlio::Title))
92 SetTitle(xml->GetAttr(keynode, xmlio::Title));
93
94 fCycle = xml->GetIntAttr(keynode, xmlio::Cycle);
95
96 if (xml->HasAttr(keynode, xmlio::CreateTm)) {
98 fDatime = tm;
99 }
100
101 XMLNodePointer_t objnode = xml->GetChild(keynode);
102 xml->SkipEmpty(objnode);
103
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// TKeyXML destructor
109
111{
112 if (fKeyNode) {
114 if (xml) {
115 xml->FreeNode(fKeyNode);
116 } else {
118 xml_.FreeNode(fKeyNode);
119 }
120 }
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Delete key from current directory
125/// Note: TKeyXML object is not deleted. You still have to call "delete key"
126
127void TKeyXML::Delete(Option_t * /*option*/)
128{
130 if (fKeyNode && xml) {
131 xml->FreeNode(fKeyNode);
132 fKeyNode = nullptr;
133 }
134
135 fMotherDir->GetListOfKeys()->Remove(this);
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Stores keys attributes in key node
140
142{
144 TXMLFile *f = (TXMLFile *)GetFile();
145 if (!f || !xml || !fKeyNode)
146 return;
147
148 xml->NewAttr(fKeyNode, nullptr, xmlio::Name, GetName());
149
150 xml->NewIntAttr(fKeyNode, xmlio::Cycle, fCycle);
151
152 if (f->GetIOVersion() > 1) {
153 if (strlen(GetTitle()) > 0)
154 xml->NewAttr(fKeyNode, nullptr, xmlio::Title, GetTitle());
155 if (f->TestBit(TFile::kReproducible))
156 xml->NewAttr(fKeyNode, nullptr, xmlio::CreateTm, TDatime((UInt_t) 1).AsSQLString());
157 else
158 xml->NewAttr(fKeyNode, nullptr, xmlio::CreateTm, fDatime.AsSQLString());
159 }
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// convert object to xml structure and keep this structure in key
164
165void TKeyXML::StoreObject(const void *obj, const TClass *cl, Bool_t check_tobj)
166{
167 TXMLFile *f = (TXMLFile *)GetFile();
169 if (!f || !xml || !fKeyNode)
170 return;
171
172 if (obj && check_tobj) {
173 TClass *actual = TObject::Class()->GetActualClass((TObject *)obj);
174 if (!actual) {
176 } else if (actual != TObject::Class())
177 obj = (void *)((Longptr_t)obj - actual->GetBaseClassOffset(TObject::Class()));
178 cl = actual;
179 }
180
182
184 buffer.InitMap();
185 if (f->GetIOVersion() == 1)
187
188 XMLNodePointer_t node = buffer.XmlWriteAny(obj, cl);
189
190 if (node)
191 xml->AddChildFirst(fKeyNode, node);
192
193 if (cl)
194 fClassName = cl->GetName();
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// update key attributes in key node
199
201{
203 if (!xml || !fKeyNode)
204 return;
205
206 xml->FreeAllAttr(fKeyNode);
207
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// updates object, stored in the node
213/// Used for TDirectory data update
214
216{
217 TXMLFile *f = (TXMLFile *)GetFile();
219 if (!f || !xml || !obj || !fKeyNode)
220 return;
221
223 xml->SkipEmpty(objnode);
224
225 if (!objnode)
226 return;
227
228 xml->UnlinkNode(objnode);
229 xml->FreeNode(objnode);
230
231 xml->FreeAllAttr(fKeyNode);
232
233 StoreObject(obj, nullptr, kTRUE);
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// To read an object from the file.
238/// The object associated to this key is read from the file into memory.
239/// Before invoking this function, obj has been created via the
240/// default constructor.
241
243{
244 if (!tobj)
245 return 0;
246
247 void *res = XmlReadAny(tobj, nullptr);
248
249 return !res ? 0 : 1;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// read object derived from TObject class, from key
254/// if it is not TObject or in case of error, return nullptr
255
257{
258 TObject *tobj = (TObject *)XmlReadAny(nullptr, TObject::Class());
259
260 if (tobj) {
261 if (gROOT->GetForceStyle())
262 tobj->UseCurrentStyle();
263 if (tobj->IsA() == TDirectoryFile::Class()) {
265 dir->SetName(GetName());
266 dir->SetTitle(GetTitle());
267 dir->SetSeekDir(GetKeyId());
268 // set mother before reading keys
269 dir->SetMother(fMotherDir);
270 dir->ReadKeys();
271 fMotherDir->Append(dir);
272 fSubdir = kTRUE;
273 }
274 }
275
276 return tobj;
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// read object derived from TObject class, from key
281/// if it is not TObject or in case of error, return nullptr
282
283TObject *TKeyXML::ReadObjWithBuffer(char * /*bufferRead*/)
284{
285 TObject *tobj = (TObject *)XmlReadAny(nullptr, TObject::Class());
286
287 if (tobj) {
288 if (gROOT->GetForceStyle())
289 tobj->UseCurrentStyle();
290 if (tobj->IsA() == TDirectoryFile::Class()) {
292 dir->SetName(GetName());
293 dir->SetTitle(GetTitle());
294 dir->SetSeekDir(GetKeyId());
295 // set mother before reading keys
296 dir->SetMother(fMotherDir);
297 dir->ReadKeys();
298 fMotherDir->Append(dir);
299 fSubdir = kTRUE;
300 }
301 }
302
303 return tobj;
304}
305
306////////////////////////////////////////////////////////////////////////////////
307/// read object of any type
308
310{
311 void *res = XmlReadAny(nullptr, expectedClass);
312
313 if (res && (expectedClass == TDirectoryFile::Class())) {
314 TDirectoryFile *dir = (TDirectoryFile *)res;
315 dir->SetName(GetName());
316 dir->SetTitle(GetTitle());
317 dir->SetSeekDir(GetKeyId());
318 // set mother before reading keys
319 dir->SetMother(fMotherDir);
320 dir->ReadKeys();
321 fMotherDir->Append(dir);
322 fSubdir = kTRUE;
323 }
324
325 return res;
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// read object from key and cast to expected class
330
332{
333 if (!fKeyNode)
334 return obj;
335
336 TXMLFile *f = (TXMLFile *)GetFile();
338 if (!f || !xml)
339 return obj;
340
341 TBufferXML buffer(TBuffer::kRead, f);
342 buffer.InitMap();
343 if (f->GetIOVersion() == 1)
345
347 xml->SkipEmpty(blocknode);
348 while (blocknode) {
349 if (strcmp(xml->GetNodeName(blocknode), xmlio::XmlBlock) == 0)
350 break;
351 xml->ShiftToNext(blocknode);
352 }
353
355 xml->SkipEmpty(objnode);
356
357 TClass *cl = nullptr;
358 void *res = buffer.XmlReadAny(objnode, obj, &cl);
359
360 if (!cl || !res)
361 return obj;
362
363 Int_t delta = 0;
364
365 if (expectedClass) {
367 if (delta < 0) {
368 if (!obj)
369 cl->Destructor(res);
370 return nullptr;
371 }
372 if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
373 // we cannot mix a compiled class with an emulated class in the inheritance
374 Warning("XmlReadAny", "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
375 cl->GetName(), expectedClass->GetName());
376 }
377 }
378
379 return ((char *)res) + delta;
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// return pointer on TXMLEngine object, used for xml conversion
384
386{
387 TXMLFile *f = (TXMLFile *)GetFile();
388 return f ? f->XML() : nullptr;
389}
#define f(i)
Definition RSha256.hxx:104
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:414
void * XMLNodePointer_t
Definition TXMLEngine.h:17
void InitMap() override
Create the fMap container and initialize them with the null object.
Class for serializing/deserializing object to/from xml.
Definition TBufferXML.h:33
XMLNodePointer_t XmlWriteAny(const void *obj, const TClass *cl)
Convert object of any class to xml structures Return pointer on top xml element.
void * XmlReadAny(XMLNodePointer_t node, void *obj, TClass **cl)
Recreate object from xml structure.
@ kCannotHandleMemberWiseStreaming
Definition TBuffer.h:76
@ kWrite
Definition TBuffer.h:73
@ kRead
Definition TBuffer.h:73
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
EState GetState() const
Definition TClass.h:501
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5439
Int_t GetBaseClassOffset(const TClass *toBase, void *address=nullptr, bool isDerivedObject=true)
Definition TClass.cxx:2796
@ kEmulated
Definition TClass.h:128
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition TDatime.h:37
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition TDatime.cxx:151
void Set()
Set Date/Time to current time as reported by the system.
Definition TDatime.cxx:288
A ROOT file is structured in Directories (like a file system).
static TClass * Class()
Int_t ReadKeys(Bool_t forceRead=kTRUE) override
Read the linked list of keys.
void SetSeekDir(Long64_t v) override
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Int_t AppendKey(TKey *)
Definition TDirectory.h:185
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
void SetName(const char *newname) override
Set the name for directory If the directory name is changed after the directory was written once,...
virtual void SetMother(TObject *mother)
Definition TDirectory.h:259
virtual TList * GetListOfKeys() const
Definition TDirectory.h:224
@ kReproducible
Definition TFile.h:271
void UpdateObject(TObject *obj)
updates object, stored in the node Used for TDirectory data update
Definition TKeyXML.cxx:215
~TKeyXML() override
TKeyXML destructor.
Definition TKeyXML.cxx:110
void Delete(Option_t *option="") final
Delete key from current directory Note: TKeyXML object is not deleted.
Definition TKeyXML.cxx:127
Long64_t GetKeyId() const
Definition TKeyXML.h:62
XMLNodePointer_t fKeyNode
! node with stored object
Definition TKeyXML.h:76
Int_t Read(TObject *tobj) final
To read an object from the file.
Definition TKeyXML.cxx:242
TXMLEngine * XMLEngine()
return pointer on TXMLEngine object, used for xml conversion
Definition TKeyXML.cxx:385
TObject * ReadObjWithBuffer(char *bufferRead) final
read object derived from TObject class, from key if it is not TObject or in case of error,...
Definition TKeyXML.cxx:283
void * XmlReadAny(void *obj, const TClass *expectedClass)
read object from key and cast to expected class
Definition TKeyXML.cxx:331
void * ReadObjectAny(const TClass *expectedClass) final
read object of any type
Definition TKeyXML.cxx:309
void StoreObject(const void *obj, const TClass *cl, Bool_t check_tobj=kFALSE)
convert object to xml structure and keep this structure in key
Definition TKeyXML.cxx:165
void UpdateAttributes()
update key attributes in key node
Definition TKeyXML.cxx:200
TKeyXML()
Definition TKeyXML.h:27
void StoreKeyAttributes()
Stores keys attributes in key node.
Definition TKeyXML.cxx:141
TObject * ReadObj() final
read object derived from TObject class, from key if it is not TObject or in case of error,...
Definition TKeyXML.cxx:256
Bool_t fSubdir
! indicates that key contains subdirectory
Definition TKeyXML.h:78
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
const char * GetTitle() const override
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition TKey.cxx:1503
TFile * GetFile() const
Returns file to which key belong.
Definition TKey.cxx:618
TDatime fDatime
Date/Time of insertion in file.
Definition TKey.h:44
Short_t fCycle
Cycle number.
Definition TKey.h:46
TDirectory * GetMotherDir() const
Definition TKey.h:87
TDirectory * fMotherDir
!pointer to mother directory
Definition TKey.h:54
TString fClassName
Object Class name.
Definition TKey.h:49
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:458
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1075
static TClass * Class()
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:882
const char * Name
Definition TXMLSetup.cxx:66
const char * Title
Definition TXMLSetup.cxx:67
const char * XmlBlock
Definition TXMLSetup.cxx:59
const char * Cycle
Definition TXMLSetup.cxx:58
const char * CreateTm
Definition TXMLSetup.cxx:68
const char * ObjClass
Definition TXMLSetup.cxx:62
const char * Xmlkey
Definition TXMLSetup.cxx:57