GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KeyFactory.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 
24 #ifndef ADF_KeyFactory
25 #include "KeyFactory.h"
26 #endif
27 #ifndef ADF_DefaultKeyFactory
28 #include "DefaultKeyFactory.h"
29 #endif
30 #ifndef ADF_AgataKeyFactory
31 #include "AgataKeyFactory.h"
32 #endif
33 
34 #include <iostream>
35 
36 using namespace ADF;
37 
38 // pointer to the main key factory
39 MainKeyFactory MainKeyFactory::gTheMainFactory("MainFactory");
40 
42 {
43  FactoryItem l_item(item); Key *key = DoNew(item);
44 
45  if ( key ) {
46  // keeps in key a reference to the factory that built it
47  l_item.SetFactoryPtr(this);
48  key->fSignature = l_item;
49  //
50  key->SetDataLength(0u);
51  }
52  return key;
53 }
54 
55 /*
56 MainKeyFactory &KeyFactory::theMainFactory()
57 {
58  return MainKeyFactory::theMainFactory();
59 }
60 */
61 MainKeyFactory::MainKeyFactory(std::string factory_name, BaseBuffer::EEndian e ) :
62  KeyFactory(factory_name, e),
63  fFactories(),
64  fClonedFactories()
65 {
66  // Add Default as the first factory
68 }
69 
71 {
72  for (size_t i = 0; i < fFactories.size(); i++) {
73  delete fFactories[i];
74  }
75  fFactories.clear();
76 
77  for (size_t i = 0; i < fClonedFactories.size(); i++) {
78  delete fClonedFactories[i];
79  }
80  fClonedFactories.clear();
81 }
82 
83 KeyFactory *MainKeyFactory::GetFactory(std::string which_factory) const
84 {
85  KeyFactory *factory = 0x0;
86  //
87  for (size_t i = 0; i < fFactories.size(); i++) {
88  if ( fFactories[i]->GetName() == which_factory )
89  {
90  factory = fFactories[i];
91  break;
92  }
93  }
94  return factory;
95 }
96 
97 KeyFactory *MainKeyFactory::GetClonedFactory(std::string which_factory, void *ptr) const
98 {
99  KeyFactory *factory = 0x0;
100  //
101  for (size_t i = 0; i < fClonedFactories.size(); i++) {
102  if ( fClonedFactories[i]->GetName() == which_factory && fClonedFactories[i] == ptr )
103  {
104  factory = fClonedFactories[i];
105  break;
106  }
107  }
108  return factory;
109 }
110 
111 Bool_t MainKeyFactory::IsKnown(std::string which_factory) const
112 {
113  return GetFactory(which_factory) != 0x0;
114 }
115 
117 {
118  Bool_t ok = false;
119 
120  if ( !IsKnown(anewfactory->GetName()) ) {
121  // add it to the factories
122  fFactories.push_back(anewfactory);
123  // and to the list of DotConf so that configuration/reconfiguration by files/frames could be performed
124  AddConf(anewfactory);
125  //
126  ok = true;
127  }
128 
129  return ok;
130 }
131 
132 Bool_t MainKeyFactory::Register(std::string factory_name)
133 {
134  Bool_t ok = false;
135 
136  if ( IsKnown(factory_name) )
137  return true; // already registered
138 
139  if ( factory_name == "Default" ) {
141  ok = true;
142  }
143  if ( factory_name == "Agata" ) {
145  ok = true;
146  }
147  return ok;
148 }
149 
151 {
152  return gTheMainFactory;
153 }
154 
156 {
157  KeyFactory *factory = GetFactory(item.GetFactoryName());
158 
159  // if in the item there is no pointer to the factory given, look for the factory by the name : otherwise look in the clone list
160  // if item is given without precise factory (Ptr == 0) or if the referenced factory is the one found i.e. the global one, use it
161  // otherwise look in the clone factory
162  if ( item.GetFactoryPtr() == 0x0 || item.GetFactoryPtr() == factory )
163  ;
164  else
165  factory = GetClonedFactory(item.GetFactoryName(),item.GetFactoryPtr());
166  //
167  Key *key = 0x0;
168  if ( factory )
169  key = factory->New(item);
170  return key;
171 }
172 
173 Key *MainKeyFactory::Clone(const Key *key)
174 {
175  Key *ckey = 0x0;
176  //
177  ckey = New(key->GetSignature());
178  if ( ckey->GetEndian() != key->GetEndian() ) {
179  // a warning to add
180  }
181 
182  // Copy the content of the key
183  if ( ckey )
184  ckey->Copy(key);
185 
186  // return the clone
187  return ckey;
188 }
189 
190 KeyFactory *MainKeyFactory::Clone(std::string which_factory, const std::string &opt)
191 {
192  KeyFactory *factory = GetFactory(which_factory);
193  if ( factory == 0x0 )
194  return 0x0;
195 
196  KeyFactory *cloned = factory->Clone(opt);
197  if ( cloned ) {
198  fClonedFactories.push_back(cloned);
199  }
200  return cloned;
201 }
202 
203 Int_t MainKeyFactory::Clone(std::list<KeyFactory *> &cloned_list, const std::string &opt)
204 {
205  Int_t nb_cloned = 0; std::vector< KeyFactory * > tmp(fFactories); // first created a tmp vector since fFactories is modified by individual call of Clone
206 
207  for (size_t i = 0; i < tmp.size(); i++) {
208  // use Clone method for one factory
209  KeyFactory *newfact = Clone(tmp[i]->GetName(),opt);
210  // add it to the return cloned_list. to be kept by the user since a Cloned factory could be retrieved only by its pointer
211  if ( newfact )
212  {
213  cloned_list.push_back(newfact);
214  nb_cloned++;
215  }
216  }
217  return nb_cloned;
218 }
219 
220 void MainKeyFactory::ls(std::string o) const
221 {
222  std::cout << " list of global key factories : " << std::endl;
223  for (size_t i = 0; i < fFactories.size(); i++) {
224  std::cout << " [g] " << fFactories[i]->GetName() << " @ " << std::hex << (void*)fFactories[i] << std::dec << std::endl;
225  }
226  if ( o.find("clones") != std::string::npos ) {
227  std::cout << " list of cloned key factories : " << std::endl;
228  for (size_t i = 0; i < fClonedFactories.size(); i++) {
229  std::cout << " [c] " << fClonedFactories[i]->GetName() << " @ " << std::hex << (void*)fClonedFactories[i] << std::dec << std::endl;
230  }
231  }
232 }
233 
234 
235 
236 
237 
Base class for a Key.
Definition: Key.h:56
static Factory_T * theFactory()
to create the main global factory for that type
Definition: KeyFactory.h:290
header file for DefaultKeyFactory.cpp
KeyFactory * GetFactory(std::string which_factory) const
Ask for a particular factory from is name.
Definition: KeyFactory.cpp:83
void ls(std::string o) const
list all factories
Definition: KeyFactory.cpp:220
header file for KeyFactory.cpp
virtual Key * DoNew(const FactoryItem &)
Do really the job of allocating the key.
Definition: KeyFactory.h:60
const FactoryItem & GetSignature() const
Definition: Key.h:115
virtual Bool_t Copy(const Char_t *, UInt_t)
Copy an external buffer to this key.
Definition: Key.cpp:90
Base class for a MainFactory i.e. a factory composed of factories.
Definition: KeyFactory.h:145
virtual BaseBuffer::EEndian GetEndian() const
Definition: Key.h:126
Bool_t IsKnown(std::string which_factory) const
check a factory has been registered
Definition: KeyFactory.cpp:111
virtual KeyFactory * Clone(const std::string &)=0
realised clone of this factory
virtual const std::string & GetName() const
name of the factory
Definition: KeyFactory.h:94
header file for AgataKeyFactory.cpp
const std::string & GetFactoryName() const
Definition: FactoryItem.h:76
virtual ~MainKeyFactory()
Definition: KeyFactory.cpp:70
Bool_t Register(KeyFactory *)
Add one factory to this collection of keyfactories: name has to be unique so return false if a factor...
Definition: KeyFactory.cpp:116
virtual Key * New(const FactoryItem &)
build a key using item.
Definition: KeyFactory.cpp:41
ADF::LogMessage & endl(ADF::LogMessage &log)
void SetFactoryPtr(void *ptr)
change
Definition: FactoryItem.h:97
virtual void AddConf(DotConf *)
add a conf object to this. The added objects are not owned by this so not deleted.
Definition: DotConf.cpp:222
static MainKeyFactory & theMainFactory()
the main (global) keyfactory
Definition: KeyFactory.cpp:150
void * GetFactoryPtr() const
Definition: FactoryItem.h:94
virtual Key * New(const FactoryItem &)
build a key using item.
Definition: KeyFactory.cpp:155
Base Key factory.
Definition: KeyFactory.h:44
Base class that described an item in a Factory.
Definition: FactoryItem.h:52