KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVDBKey.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 $Id: KVDBKey.cpp,v 1.23 2007/04/19 12:41:54 franklan Exp $
3  KVDBKey.cpp - description
4  -------------------
5  begin : jeu f� 6 2003
6  copyright : (C) 2003 by Alexis Mignon
7  email : mignon@ganil.fr
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 #include "KVDBKey.h"
19 #include "KVDBRecord.h"
20 #include "Riostream.h"
21 #include "KVDBTable.h"
22 #include "TObjString.h"
23 #include "TROOT.h"
24 
26 
27 
28 //____________________________________________________________________________
29 
30 
33 {
34  fIsUnique = fSingle = kFALSE;
35  fLinks = new KVRList;
36  fRecord = 0;
37 }
38 
39 
40 
42 
43 KVDBKey::KVDBKey(const Char_t* name, const Char_t* title,
44  KVDBRecord* parent)
45  : KVBase(name, title)
46 {
48  fRecord = 0;
49  SetParent(parent);
50  fLinks = new KVRList;
51  TString knom(name);
52  knom.Prepend("Key:");
53  SetName(knom.Data());
54  SetTitle(title);
55 }
56 
57 
58 
60 
61 KVDBKey::~KVDBKey()
62 {
63  if (fLinks) {
64  fLinks->Clear();
65  delete fLinks;
66  fLinks = 0;
67  }
68  fRecord = 0;
69 }
70 
71 
72 
80 
82 {
83 // This function adds a record to the list of cross-references
84 // if fIsUnique is kTRUE we check if there is already an object with the same name:
85 // if so we return kFALSE otherwise the record is added and kTRUE is returned.
86 // if fSingle is kTRUE the list can handle only one object:
87 // if the list is empty the record is added and kTRUE is returned, otherwise kFALSE.
88 
89  // check if more than one object is allowed
90  if (fSingle && (GetLinks()->GetSize() > 0) && (GetLinks()->First())) {
91  Warning("LinkTo(KVDBRecord*)",
92  "%s : Only one cross-reference allowed.\nCurrent cross-reference is:%s\nCan't add new record \"%s\" in list",
93  GetName(), GetLinks()->First()->GetName(), rec->GetName());
94  return kFALSE;
95  }
96  // check if identical names are allowed
97  if (fIsUnique) {
98  if (fLinks->FindObject(rec->GetName())) {
99  Warning("LinkTo(KVDBRecord*)",
100  "%s : Cross-references must have different names.\nA record named %s already exists.\nCan't add new record \"%s\" in list",
101  GetName(), rec->GetName(), rec->GetName());
102  return kFALSE;
103  }
104  }
105  AddLink(rec);
106 
107  if (linkback) {
108  //if linkback is kTRUE then a mirror link to this record will be made in record "rec"
109  //if a key with the same name as the table containing the current record is found
110  //in the record to link , then this key is linked back to the current record.
111  //if no key is found, one will be created, and then the linkback performed.
112 
113  //check parent is set
114  if (!GetParent()) {
115  Error("LinkTo", "Parent table not set for key %s", GetName());
116  return kFALSE;
117  }
118  const Char_t* table_name = GetParent()->GetTable()->GetName();
119 
120  KVDBKey* key = rec->GetKey(table_name);
121  if (!key)
122  key = rec->AddKey(table_name, GetParent()->GetName());
123  key->LinkTo(GetParent(), kFALSE); //linkback=kFALSE otherwise infinite circular linkage results !!!
124  }
125  return kTRUE;
126 }
127 
128 
129 
130 
134 
135 void KVDBKey::Unlink(KVDBRecord* rec, Bool_t linkback)
136 {
137  //This function removes a record from the list of cross-references
138  //if linkback is kTRUE then the reference in record "rec" is also removed.
139 
140  RemoveLink(rec);
141 
142  if (linkback) {
143 
144  //check parent is set
145  if (!GetParent()) {
146  Error("Unlink", "Parent not set for key %s", GetName());
147  return;
148  }
149  const Char_t* table_name = GetParent()->GetTable()->GetName();
150 
151  KVDBKey* key = rec->GetKey(table_name);
152  if (key) key->Unlink(GetParent(), kFALSE); //linkback=kFALSE otherwise infinite circular linkage results !!!
153  }
154 }
155 
156 
157 
158 
159 
164 
166 {
167  //This function recursively removes all cross-references
168  //The corresponding references to the owner of this key in all of the referecnced
169  //objects will also be removed.
170 
171  KVDBRecord* rec = (KVDBRecord*)fLinks->Last();
172  if (rec) {
173  Unlink(rec);
174  UnlinkAll();
175  }
176  fLinks->Clear();
177 }
178 
179 
180 
181 
184 
186 {
187  //Add a link to the list of links
188 
189  fLinks->Add(rec);
190 }
191 
192 
193 
196 
198 {
199  //Remove a link from the list of links
200 
201  fLinks->Remove(rec);
202 }
203 
204 
205 
207 
208 KVDBRecord* KVDBKey::GetLink(const Char_t* link) const
209 {
210  return (KVDBRecord*) fLinks->FindObjectByName(link);
211 }
212 
213 
214 
216 
218 {
219  return (KVDBRecord*) fRecord.GetObject();
220 }
221 
222 
223 
225 
227 {
228  fRecord = (TObject*) parent;
229 }
230 
231 
232 
234 
236 {
237  SetParent(parent);
238 }
239 
240 
241 
242 
244 
246 {
247  return GetParent();
248 }
249 
250 
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
Base class for KaliVeda framework.
Definition: KVBase.h:135
Cross-reference in a KVDataBase.
Definition: KVDBKey.h:37
virtual void SetParent(KVDBRecord *parent)
Definition: KVDBKey.cpp:226
virtual KVDBRecord * GetParent()
Definition: KVDBKey.cpp:217
virtual void RemoveLink(KVDBRecord *rec)
Remove a link from the list of links.
Definition: KVDBKey.cpp:197
virtual void SetRecord(KVDBRecord *parent)
Definition: KVDBKey.cpp:235
virtual KVRList * GetLinks() const
return the list of cross-referenced objects
Definition: KVDBKey.h:64
KVDBKey()
Definition: KVDBKey.cpp:32
KVRList * fLinks
list of cross-referenced records
Definition: KVDBKey.h:44
virtual KVDBRecord * GetLink(const Char_t *link) const
Definition: KVDBKey.cpp:208
virtual void UnlinkAll()
Definition: KVDBKey.cpp:165
virtual void Unlink(KVDBRecord *rec, Bool_t linkback=kTRUE)
Definition: KVDBKey.cpp:135
virtual KVDBRecord * GetRecord()
Definition: KVDBKey.cpp:245
Bool_t fIsUnique
Can the list contains more than 1 object the same name.
Definition: KVDBKey.h:41
TRef fRecord
direct pointer to parent record
Definition: KVDBKey.h:43
Bool_t fSingle
Can the list contain more than 1 object in the list.
Definition: KVDBKey.h:42
virtual void AddLink(KVDBRecord *rec)
Add a link to the list of links.
Definition: KVDBKey.cpp:185
virtual Bool_t LinkTo(KVDBRecord *rec, Bool_t linkback=kTRUE)
Definition: KVDBKey.cpp:81
Record folder for the database.
Definition: KVDBRecord.h:42
virtual KVDBKey * GetKey(const Char_t *key) const
Definition: KVDBRecord.cpp:290
virtual Bool_t AddKey(KVDBKey *key, Bool_t check=kTRUE)
Definition: KVDBRecord.cpp:65
virtual KVDBTable * GetTable() const
Definition: KVDBRecord.cpp:271
Wrapper for TRefArray adding some functionality.
Definition: KVRList.h:36
virtual KVBase * FindObjectByName(const Char_t *) const
Look for object with name "name" in the list.
Definition: KVRList.cpp:31
virtual KVBase * FindObject(const Char_t *name, const Char_t *type) const
Definition: KVRList.cpp:106
virtual const char * GetName() const
virtual void SetTitle(const char *title="")
virtual void SetName(const char *name)
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual void Error(const char *method, const char *msgfmt,...) const
void Add(TObject *obj)
virtual void Clear(Option_t *option="")
virtual TObject * Remove(TObject *obj)
TObject * Last() const
TObject * GetObject() const
const char * Data() const
TString & Prepend(char c, Ssiz_t rep=1)