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 buffer.XmlWriteBlock(fKeyNode);
194
195 if (cl)
196 fClassName = cl->GetName();
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// update key attributes in key node
201
203{
205 if (!xml || !fKeyNode)
206 return;
207
208 xml->FreeAllAttr(fKeyNode);
209
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// updates object, stored in the node
215/// Used for TDirectory data update
216
218{
219 TXMLFile *f = (TXMLFile *)GetFile();
221 if (!f || !xml || !obj || !fKeyNode)
222 return;
223
225 xml->SkipEmpty(objnode);
226
227 if (!objnode)
228 return;
229
230 xml->UnlinkNode(objnode);
231 xml->FreeNode(objnode);
232
233 xml->FreeAllAttr(fKeyNode);
234
235 StoreObject(obj, nullptr, kTRUE);
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// To read an object from the file.
240/// The object associated to this key is read from the file into memory.
241/// Before invoking this function, obj has been created via the
242/// default constructor.
243
245{
246 if (!tobj)
247 return 0;
248
249 void *res = XmlReadAny(tobj, nullptr);
250
251 return !res ? 0 : 1;
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// read object derived from TObject class, from key
256/// if it is not TObject or in case of error, return nullptr
257
259{
260 TObject *tobj = (TObject *)XmlReadAny(nullptr, TObject::Class());
261
262 if (tobj) {
263 if (gROOT->GetForceStyle())
264 tobj->UseCurrentStyle();
265 if (tobj->IsA() == TDirectoryFile::Class()) {
267 dir->SetName(GetName());
268 dir->SetTitle(GetTitle());
269 dir->SetSeekDir(GetKeyId());
270 // set mother before reading keys
271 dir->SetMother(fMotherDir);
272 dir->ReadKeys();
273 fMotherDir->Append(dir);
274 fSubdir = kTRUE;
275 }
276 }
277
278 return tobj;
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// read object derived from TObject class, from key
283/// if it is not TObject or in case of error, return nullptr
284
285TObject *TKeyXML::ReadObjWithBuffer(char * /*bufferRead*/)
286{
287 TObject *tobj = (TObject *)XmlReadAny(nullptr, TObject::Class());
288
289 if (tobj) {
290 if (gROOT->GetForceStyle())
291 tobj->UseCurrentStyle();
292 if (tobj->IsA() == TDirectoryFile::Class()) {
294 dir->SetName(GetName());
295 dir->SetTitle(GetTitle());
296 dir->SetSeekDir(GetKeyId());
297 // set mother before reading keys
298 dir->SetMother(fMotherDir);
299 dir->ReadKeys();
300 fMotherDir->Append(dir);
301 fSubdir = kTRUE;
302 }
303 }
304
305 return tobj;
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// read object of any type
310
312{
313 void *res = XmlReadAny(nullptr, expectedClass);
314
315 if (res && (expectedClass == TDirectoryFile::Class())) {
316 TDirectoryFile *dir = (TDirectoryFile *)res;
317 dir->SetName(GetName());
318 dir->SetTitle(GetTitle());
319 dir->SetSeekDir(GetKeyId());
320 // set mother before reading keys
321 dir->SetMother(fMotherDir);
322 dir->ReadKeys();
323 fMotherDir->Append(dir);
324 fSubdir = kTRUE;
325 }
326
327 return res;
328}
329
330////////////////////////////////////////////////////////////////////////////////
331/// read object from key and cast to expected class
332
334{
335 if (!fKeyNode)
336 return obj;
337
338 TXMLFile *f = (TXMLFile *)GetFile();
340 if (!f || !xml)
341 return obj;
342
343 TBufferXML buffer(TBuffer::kRead, f);
344 buffer.InitMap();
345 if (f->GetIOVersion() == 1)
347
349 xml->SkipEmpty(blocknode);
350 while (blocknode) {
351 if (strcmp(xml->GetNodeName(blocknode), xmlio::XmlBlock) == 0)
352 break;
353 xml->ShiftToNext(blocknode);
354 }
355 buffer.XmlReadBlock(blocknode);
356
358 xml->SkipEmpty(objnode);
359
360 TClass *cl = nullptr;
361 void *res = buffer.XmlReadAny(objnode, obj, &cl);
362
363 if (!cl || !res)
364 return obj;
365
366 Int_t delta = 0;
367
368 if (expectedClass) {
370 if (delta < 0) {
371 if (!obj)
372 cl->Destructor(res);
373 return nullptr;
374 }
375 if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
376 // we cannot mix a compiled class with an emulated class in the inheritance
377 Warning("XmlReadAny", "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
378 cl->GetName(), expectedClass->GetName());
379 }
380 }
381
382 return ((char *)res) + delta;
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// return pointer on TXMLEngine object, used for xml conversion
387
389{
390 TXMLFile *f = (TXMLFile *)GetFile();
391 return f ? f->XML() : nullptr;
392}
#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:411
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 XmlReadBlock(XMLNodePointer_t node)
Read binary block of data from xml.
void * XmlReadAny(XMLNodePointer_t node, void *obj, TClass **cl)
Recreate object from xml structure.
void XmlWriteBlock(XMLNodePointer_t node)
Write binary data block from buffer to xml.
@ 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:272
void UpdateObject(TObject *obj)
updates object, stored in the node Used for TDirectory data update
Definition TKeyXML.cxx:217
~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
Definition TKeyXML.h:76
Int_t Read(TObject *tobj) final
To read an object from the file.
Definition TKeyXML.cxx:244
TXMLEngine * XMLEngine()
return pointer on TXMLEngine object, used for xml conversion
Definition TKeyXML.cxx:388
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:285
void * XmlReadAny(void *obj, const TClass *expectedClass)
read object from key and cast to expected class
Definition TKeyXML.cxx:333
void * ReadObjectAny(const TClass *expectedClass) final
read object of any type
Definition TKeyXML.cxx:311
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:202
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:258
Bool_t fSubdir
unique identifier of key for search methods
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:1523
TFile * GetFile() const
Returns file to which key belong.
Definition TKey.cxx:587
TDatime fDatime
Date/Time of insertion in file.
Definition TKey.h:42
Short_t fCycle
Cycle number.
Definition TKey.h:44
TDirectory * GetMotherDir() const
Definition TKey.h:85
TDirectory * fMotherDir
!pointer to mother directory
Definition TKey.h:52
TString fClassName
Object Class name.
Definition TKey.h:47
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:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
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:1057
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:864
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