GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GObject.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #include "GObject.h"
24 
25 using namespace ADF;
26 
28 fDataItems(),
29 fDataLinks(),
30 fArrayOfItems(),
31 fArrayOfIdleItems()
32 {
33 }
35 {
36  // delete proxy and links
37  size_t i, size = fArrayOfItems.size();
38  for (i = 0; i < size; i++) {
39  if ( fArrayOfItems[i].second.first ) { // Is a proxy
40  delete fArrayOfItems[i].second.first;
41  fArrayOfItems[i].second.first = 0x0;
42  }
43  if ( fArrayOfItems[i].second.second ) { // Is a link
44  UnLink ( fArrayOfItems[i].second.second );
45  }
46 
47  }
48  // delete proxy and links for idle items
49  size = fArrayOfIdleItems.size();
50  for (i = 0; i < size; i++) {
51  if ( fArrayOfIdleItems[i].second.first ) { // Is a proxy
52  delete fArrayOfIdleItems[i].second.first;
53  fArrayOfIdleItems[i].second.first = 0x0;
54  }
55  if ( fArrayOfIdleItems[i].second.second ) { // Is a link
56  UnLink ( fArrayOfIdleItems[i].second.second );
57  }
58  }
59  // now item and items car be safely deleted (items needed in UnLink method)
60  size = fArrayOfItems.size();
61  for (i = 0; i < size; i++) {
62  delete fArrayOfItems[i].first; fArrayOfItems[i].first = 0x0;
63  }
64  size = fArrayOfIdleItems.size();
65  for (i = 0; i < size; i++) {
66  delete fArrayOfIdleItems[i].first; fArrayOfIdleItems[i].first = 0x0;
67  }
68 }
70 {
71  // check if an item with the same name and type already exist
72  size_t i, size = fArrayOfItems.size();
73  for (i = 0; i < size; i++) {
74  if (fArrayOfItems[i].first->Name() == item->Name() &&
75  fArrayOfItems[i].first->Type() == item->Type()) {
76  return false;
77  }
78  }
79  // if not add it to the list of items
81  p.first = item;
82  p.second.first = 0x0;
83  p.second.second = 0x0;
84 
85  fArrayOfItems.push_back( p ); fDataItems.push_back( item );
86 
87  return true;
88 }
89 GObject::FullItem *GObject::IsAnItem(const Char_t *name, const Char_t *type)
90 {
91  size_t i, size = fArrayOfItems.size();
92  FullItem *item = 0x0;
93 
94  for (i = 0u; i < size; i++) {
95  if ( fArrayOfItems[i].first->Name() == name && fArrayOfItems[i].first->IsType(type) ) {
96  item = &fArrayOfItems[i];
97  break;
98  }
99  }
100  return item;
101 }
102 GObject::FullItem *GObject::IsAnItem(const ANamedItem *item)
103 {
104  return IsAnItem(item->GetName().data(),item->GetType().data());
105 }
106 GObject::FullItem *GObject::IsAIdleItem(const Char_t *name, const Char_t *type)
107 {
108  size_t i, size = fArrayOfIdleItems.size();
109  FullItem *item = 0x0;
110 
111  for (i = 0u; i < size; i++) {
112  if ( fArrayOfIdleItems[i].first->Name() == name &&
113  fArrayOfIdleItems[i].first->IsType(type) ) {
114  item = &fArrayOfIdleItems[i];
115  break;
116  }
117  }
118  return item;
119 }
120 Bool_t GObject::LinkItem(const Char_t *name, const Char_t *type, void *extern_address)
121 {
122  std::string t = type;
123 
124  if ( t == kUShort_t )
125  return Link<UShort_t>(name,(UShort_t *)extern_address);
126  if ( t == kShort_t )
127  return Link<Short_t>(name,(Short_t *)extern_address);
128  if ( t == kUInt_t )
129  return Link<UInt_t>(name,(UInt_t *)extern_address);
130  if ( t == kInt_t )
131  return Link<Int_t>(name,(Int_t *)extern_address);
132  if ( t == kULong64_t )
133  return Link<ULong64_t>(name,(ULong64_t *)extern_address);
134  if ( t == kLong64_t )
135  return Link<Long64_t>(name,(Long64_t *)extern_address);
136  if ( t == kDouble_t )
137  return Link<Double_t>(name,(Double_t *)extern_address);
138  if ( t == kFloat_t )
139  return Link<Float_t>(name,(Float_t *)extern_address);
140 
141  return false;
142 }
143 // Bool_t GObject::LinkItem(const Char_t *name, Bool_t *address)
144 // { return Link<Bool_t>(name,address); }
145 Bool_t GObject::LinkItem(const Char_t *name, Short_t *address)
146 { return Link<Short_t>(name,address); }
147 Bool_t GObject::LinkItem(const Char_t *name, UShort_t *address)
148 { return Link<UShort_t>(name,address); }
149 Bool_t GObject::LinkItem(const Char_t *name, Int_t *address)
150 { return Link<Int_t>(name,address); }
151 Bool_t GObject::LinkItem(const Char_t *name, UInt_t *address)
152 { return Link<UInt_t>(name,address); }
153 Bool_t GObject::LinkItem(const Char_t *name, Float_t *address)
154 { return Link<Float_t>(name,address); }
155 Bool_t GObject::LinkItem(const Char_t *name, Double_t *address)
156 { return Link<Double_t>(name,address); }
157 Bool_t GObject::LinkItem(const Char_t *name, Long64_t *address)
158 { return Link<Long64_t>(name,address); }
159 Bool_t GObject::LinkItem(const Char_t *name, ULong64_t *address)
160 { return Link<ULong64_t>(name,address); }
161 
162 void GObject::UnLink(DataLink *link)
163 {
164  // remove the link from the list of links to synchronize
165  size_t i, i_to_remove = 0u, size = fDataLinks.size();
166  Bool_t do_erase;
167 
168  do_erase = false; size = fDataLinks.size();
169  for (i = 0; i < size; i++)
170  if ( fDataLinks[i] == link )
171  { i_to_remove = i; do_erase = true; break; }
172 
173  if ( do_erase )
174  fDataLinks.erase(fDataLinks.begin()+i_to_remove);
175 
176  // now delete it !
177  size = fArrayOfItems.size();
178  for (i = 0; i < size; i++)
179  if ( fArrayOfItems[i].second.second == link ){
180  delete fArrayOfItems[i].second.second;
181  fArrayOfItems[i].second.second = 0x0;
182  break;
183  }
184  // now delete it !
185  size = fArrayOfIdleItems.size();
186  for (i = 0; i < size; i++)
187  if ( fArrayOfIdleItems[i].second.second == link ){
188  delete fArrayOfIdleItems[i].second.second;
189  fArrayOfIdleItems[i].second.second = 0x0;
190  break;
191  }
192 }
194 {
195  UInt_t i; GObject::FullItem *item_from, *item_to;
196 
197  // loop on items from the from object
198  for (i = 0u; i < fArrayOfItems.size(); i++) {
199 
200  // get the next full item in the from object list
201  item_from = &fArrayOfItems[i];
202 
203  // this item is also is the destination object, so move proxy and link if any
204  if ( (item_to = to.IsAnItem(item_from->first)) ) {
205  if ( item_from->second.first ) { // this item has a proxy
206  // copy the proxy in the destination object and change the true value
207  item_to->second.first = item_from->second.first;
208  AProxyNamedItem *prox =
209  dynamic_cast<AProxyNamedItem *>(item_to->second.first);
210  prox->SetProxy(item_to->first);
211 
212  // remove the proxy from the source
213  item_from->second.first = 0x0;
214  }
215  if ( item_from->second.second ) { // this item is linked
216  // create a link in the destination object and add it to the list of links
217  item_to->second.second =
218  item_to->first->MakeADataLink(item_from->second.second->A2()) ;
219  to.fDataLinks.push_back(item_to->second.second);
220 
221  // unlink properly this one from the source
222  UnLink( item_from->second.second );
223  }
224  }
225  // this item is not in the destination object
226  // add a clone, with proxy and link if any, in the fArrayOfIdleItems
227  else {
228  FullItem p;
229  p.first = item_from->first->MakeAClone();
230 
231  // if a proxy, set it
232  if ( item_from->second.first ) {
233  // copy the proxy in the destination object and change the true value
234  p.second.first = item_from->second.first;
235  AProxyNamedItem *prox =
236  dynamic_cast<AProxyNamedItem *>(p.second.first);
237  prox->SetProxy(p.first);
238 
239  // remove the proxy from the source
240  item_from->second.first = 0x0;
241  }
242  else p.second.first = 0x0;
243 
244  if ( item_from->second.second ) { // this item is linked
245  // create a link in the destination object and add it to the list of links
246  p.second.second =
247  p.first->MakeADataLink(item_from->second.second->A2()) ;
248 
249  // unlink this one
250  UnLink( item_from->second.second );
251  }
252  else p.second.second = 0x0;
253 
254  to.fArrayOfIdleItems.push_back ( p );
255  }
256  }
257  // loop on passed items from the from object
258  for (i = 0u; i < fArrayOfIdleItems.size(); i++) {
259 
260  // get the next full item in the from object list
261  GObject::FullItem *item_from = &fArrayOfIdleItems[i];
262 
263  // this item is also is the destination object, so move proxy and link if any
264  if ( (item_to = to.IsAnItem(item_from->first)) ) {
265  if ( item_from->second.first ) { // this item has a proxy
266  // copy the proxy in the destination object and change the true value
267  item_to->second.first = item_from->second.first;
268  AProxyNamedItem *prox =
269  dynamic_cast<AProxyNamedItem *>(item_to->second.first);
270  prox->SetProxy(item_to->first);
271 
272  // remove the proxy from the source
273  item_from->second.first = 0x0;
274  }
275  if ( item_from->second.second ) { // this item is linked
276  // create a link in the destination object and add it to the list of links
277  item_to->second.second =
278  item_to->first->MakeADataLink(item_from->second.second->A2()) ;
279  to.fDataLinks.push_back(item_to->second.second);
280 
281  // unlink properly this one from the source
282  UnLink( item_from->second.second );
283  }
284  }
285  // this item is not in the destination object
286  // add a clone, with proxy and link if any, in the fArrayOfIdleItems
287  else {
288  FullItem p;
289  p.first = item_from->first->MakeAClone();
290 
291  // if a proxy, set it
292  if ( item_from->second.first ) {
293  // copy the proxy in the destination object and change the true value
294  p.second.first = item_from->second.first;
295  AProxyNamedItem *prox =
296  dynamic_cast<AProxyNamedItem *>(p.second.first);
297  prox->SetProxy(p.first);
298 
299  // remove the proxy from the source
300  item_from->second.first = 0x0;
301  }
302  else p.second.first = 0x0;
303 
304  if ( item_from->second.second ) { // this item is linked
305  // create a link in the destination object and add it to the list of links
306  p.second.second =
307  p.first->MakeADataLink(item_from->second.second->A2()) ;
308 
309  // unlink this one
310  UnLink( item_from->second.second );
311  }
312  else p.second.second = 0x0;
313 
314  to.fArrayOfIdleItems.push_back ( p );
315  }
316  }
317 }
318 void GObject::Print(std::ostream &out) const
319 {
320  for (UInt_t i = 0u; i < fArrayOfItems.size(); i++) {
321 
322  out << "Item #" << i << " ";
323 
324  fArrayOfItems[i].first->Print(out);
325  out << " ";
326 
327  if ( fArrayOfItems[i].second.first )
328  out << " [proxy]";
329  if ( fArrayOfItems[i].second.second )
330  out << " [link]";
331 
332  out << std::endl;
333  }
334  for (UInt_t i = 0u; i < fArrayOfIdleItems.size(); i++) {
335 
336  out << "IdleItem #" << i << " ";
337 
338  fArrayOfIdleItems[i].first->Print(out);
339  out << " ";
340 
341  if ( fArrayOfIdleItems[i].second.first )
342  out << " [proxy]";
343  if ( fArrayOfIdleItems[i].second.second )
344  out << " [link]";
345 
346  out << std::endl;
347  }
348 }
350 {
351  size_t i, size = fDataLinks.size();
352  for (i = 0; i < size; i++)
353  fDataLinks[i]->CopyA2_to_A1();
354 }
355 void GObject::GetItems() const
356 {
357  size_t i, size = fDataLinks.size();
358  for (i = 0; i < size; i++)
359  fDataLinks[i]->CopyA1_to_A2();
360 }
362 {
363  size_t i, size = fDataItems.size();
364  for (i = 0; i < size; i++)
365  fDataItems[i]->Reset();
366 }
367 void GObject::SetItems(BufferIO &buf) const
368 {
369  // first read items
370  size_t i, size = fDataItems.size();
371  for (i = 0; i < size; i++)
372  fDataItems[i]->SetItem(buf);
373 }
374 void GObject::GetItems(const BufferIO &buf)
375 {
376  size_t i, size = fDataItems.size();
377  for (i = 0; i < size; i++)
378  fDataItems[i]->GetItem(buf);
379 }
380 
381 
382 const char *ADF::NamedItemTypeOf(Anonymous /*s*/)
383 { return "Anonymous"; }
384 
385 std::ostream & ADF::operator << (std::ostream &buf, const ADF::Anonymous &a)
386 {
387  buf << "[" << a.fRealSize << "]";
388 
389  return buf;
390 }
392 {
393  buf << a.fRealSize;
394  if ( a.fRealSize > 0u )
395  buf.Import(a.fData,a.fRealSize) ;
396 
397  return buf;
398 }
400 {
401  buf >> a.fRealSize;
402  if ( a.fRealSize > 0u )
403  buf.Export(a.fData,a.fRealSize) ;
404 
405  return buf;
406 }
407 
409 {
410  a.fRealSize > kMaxSize ? fRealSize = kMaxSize : fRealSize = a.fRealSize ;
411 
412  if ( fRealSize )
413  ::memcpy(fData,a.fData,fRealSize);
414  return (*this);
415 }
416 
417 
418 
419 
420 
421 
422 
std::ostream & operator<<(std::ostream &, const Anonymous &)
Definition: GObject.cpp:385
const char kShort_t[]
Definition: ADFConfig.h:106
const char kLong64_t[]
Definition: ADFConfig.h:110
virtual Bool_t AddItem(ANamedItem *item)
Add a named item to this. The item belongs to GObject and will be thus deleted.
Definition: GObject.cpp:69
const char kDouble_t[]
Definition: ADFConfig.h:112
GObject()
Constructor.
Definition: GObject.cpp:27
const char kULong64_t[]
Definition: ADFConfig.h:109
Base class for a Global Object.
Definition: GObject.h:115
std::string GetName() const
change value of this nmaed item using the passed one. it checks if asked, it check first if of the sa...
Definition: DataHandling.h:219
Char_t fData[kMaxSize]
Definition: GObject.h:299
std::string GetType() const
Type of the Item.
Definition: DataHandling.h:222
virtual void Reset()
Set local to default.
Definition: GObject.cpp:361
void TransferLinkAndProxy(GObject &to)
transfert proxy and link for one GObject to another one
Definition: GObject.cpp:193
virtual UInt_t Export(Char_t *, UInt_t) const
Export this buffer to an array.
Definition: BufferIO.h:694
const char kFloat_t[]
Definition: ADFConfig.h:111
virtual void SetProxy(ANamedItem *)
the passed item becomes the real item
Definition: DataHandling.h:333
Bool_t LinkItem(const Char_t *, const Char_t *, void *)
Only for basic type ... otherwise better to use the most general Link method.
Definition: GObject.cpp:120
const char kInt_t[]
Definition: ADFConfig.h:108
std::pair< ANamedItem *, std::pair< ANamedItem *, DataLink * > > FullItem
a full item is composed of a pointer to an item, an pointer to a proxy and a pointer to a datalink ...
Definition: GObject.h:119
ANamedItem.
Definition: DataHandling.h:188
const char kUShort_t[]
Definition: ADFConfig.h:105
const char kUInt_t[]
Definition: ADFConfig.h:107
const BufferIO & operator>>(const BufferIO &, Anonymous &)
Definition: GObject.cpp:399
ADF::LogMessage & endl(ADF::LogMessage &log)
UInt_t Import(const Char_t *from, UInt_t size_ext_buf)
Import the given array in this buffer.
Definition: BufferIO.cpp:298
std::string & Name()
Name of the Item.
Definition: DataHandling.h:226
virtual ~GObject()
Destructor.
Definition: GObject.cpp:34
const char * NamedItemTypeOf(Bool_t)
UShort_t fRealSize
Definition: GObject.h:298
Anonymous operator=(const Anonymous &a)
Definition: GObject.cpp:408
virtual void Print(std::ostream &out=std::cout) const
print some informations about this object
Definition: GObject.cpp:318
const Int_t size
Definition: BenchIO.C:24
std::string & Type()
Type of the Item.
Definition: DataHandling.h:229
virtual void SetItems()
Copy external variables into locals.
Definition: GObject.cpp:349
virtual void GetItems() const
Copy local variables into externals.
Definition: GObject.cpp:355