GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KeyFactory.h
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 #ifndef ADF_KeyFactory
24 #define ADF_KeyFactory
25 #define ADF_KEYFACTORY_H ADF_KeyFactory
26 
27 #include "BaseBuffer.h"
28 #include "FactoryItem.h"
29 #include "DotConf.h"
30 
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 
35 namespace ADF
36 {
37 
38 class Key;
39 class MainKeyFactory;
40 
42 
44 class KeyFactory : public DotConf
45 {
46  friend class MainKeyFactory;
47 
48 private:
50  std::string fName;
52  BaseBuffer::EEndian fEndian;
53 
54 private:
55  KeyFactory()
56  {;}
57 
58 protected:
60  virtual Key *DoNew(const FactoryItem &)
61  { return 0x0; }
62 
63 protected:
65  virtual KeyFactory *Clone(const std::string &) = 0;
66 
67  // change the name of the factory in case default constructor has been used
68  void SetName(std::string name)
69  {
70  fName = name;
71  }
72 
73 public:
75  KeyFactory(std::string factory_name, BaseBuffer::EEndian e = BaseBuffer::kLittle) : DotConf("ADF::KeyFactory"), fName(factory_name), fEndian(e)
76  {;}
78  virtual ~KeyFactory()
79  {;}
80 
82 
88  virtual Key *New(const FactoryItem &);
89 
91 
94  virtual const std::string &GetName() const
95  {
96  return fName;
97  }
100  {
101  return fEndian;
102  }
105  {
106  fEndian = e;
107  }
108 
109 // some static to access the global main key factory
110 public:
112 
115  //static Bool_t Register(const Char_t *);
116 
118 
122  //static MainKeyFactory &theMainFactory();
123 
125 
129  // static Key *New(const FactoryItem &item);
130  //static Key *New(const Char_t *which_factory, const Char_t *which_key, Version version);
131 
133 
135  //static KeyFactory *GetFactory(std::string which_factory);
136 
138  //static Bool_t IsKnown(std::string which_factory)
139  // { return KeyFactory::GetFactory(which_factory) != 0x0 ; }
140 };
141 
143 
145 class MainKeyFactory : public KeyFactory
146 {
147 private:
148  static MainKeyFactory gTheMainFactory;
149 
150 private:
152  std::vector <KeyFactory *> fFactories;
154  std::vector <KeyFactory *> fClonedFactories;
155 
156 private:
157  MainKeyFactory(std::string factory_name, BaseBuffer::EEndian e = BaseBuffer::kLittle);
158 
159 private:
160  virtual KeyFactory *Clone(const std::string &)
161  { return 0x0 ; } // the main factory cannot be cloned
162 
164 
166  KeyFactory *GetClonedFactory(std::string which_factory, void *) const;
167 
168 public:
170  virtual ~MainKeyFactory();
171 
173 
176  virtual Key *New(const FactoryItem &);
177 
179  Key *Clone(const Key *key);
180 
182  KeyFactory *Clone(std::string, const std::string &opt = "full");
184  Int_t Clone(std::list<KeyFactory *> &, const std::string &opt = "full");
185 
187  const std::vector <KeyFactory *> &ListOfFactories() const
188  { return fFactories; }
189 
191  Bool_t Register(KeyFactory *);
193  Bool_t Register(std::string factory_name);
194 
196 
198  KeyFactory *GetFactory(std::string which_factory) const;
199 
201  Bool_t IsKnown(std::string which_factory) const ;
202 
204  static MainKeyFactory &theMainFactory();
205 
207  void ls(std::string o) const;
208 };
209 
211 
214 template <typename Factory_T>
215 class AKeyFactory : public KeyFactory
216 {
217 private:
218  static Factory_T *gTheFactory;
219 
220 protected:
221  AKeyFactory (const char *factory_name, BaseBuffer::EEndian e = BaseBuffer::kLittle) : KeyFactory(factory_name,e)
222  {
223  if ( gTheFactory == 0x0 )
224  MainKeyFactory::theMainFactory().Register(this); // only the global one is registered. Others are cloned
225  }
226  virtual ~AKeyFactory () { /* std::cout << "destroying singleton." << std::endl; */ }
227 
229  virtual Key *DoNew(const FactoryItem &) = 0;
230 
232  virtual KeyFactory *Clone(const std::string &opt = "full")
233  {
234  fLog.SetProcessMethod("ADF::AKeyFactory<>::Clone(const std::string &)");
235 
236  Factory_T * newfact = new Factory_T ();
237  if ( newfact == 0x0 ) {
238  fLog << error << "Cannot allocate a new cloned factory of " << GetName() << dolog;
239  return 0x0;
240  }
241  //
242  newfact->SetName(GetName()); newfact->SetEndian(GetEndian());
243  //
244  if ( opt.find("full") == std::string::npos ) { // only constructor = empty instance, otherwise full clone
245 
246  fLog << info << "A new empty clone has been created for the factory " << GetName() << " " << nline;
247 
248  if ( fLog.IsDebug(2) ) {
250  DoConfigure( fLog() );
252  }
253  fLog << dolog;
254 
255  return newfact;
256  }
257  // use DotConf facilities to clone
258  Bool_t ok = true;
259  //
260  std::ostringstream o;
261  ok = ok &&
262  this->DoConfigure(o);
263  //
264  std::istringstream i(o.str());
265  ok = ok &&
266  newfact->DoConfigure(i,true); //
267 
268  if ( !ok ) {
269  fLog << error << "the clone configuration has failed, delete it ! " << nline;
270 
271  delete newfact;
272  newfact = 0x0;
273  }
274  else {
275  fLog << info << "the clone configuration has succeded ! " << nline;
276 
277  if ( fLog.IsDebug(2) ) {
279  DoConfigure( fLog() );
281  }
282  }
283 
284  fLog << dolog;
285  return newfact;
286  }
287 
288 public:
290  static Factory_T *theFactory ()
291  {
292  if (0x0 == gTheFactory) {
293 #ifdef DEBUGSTATIC
294  std::clog << "[[static]] Creation in Keyfactory ]\n[...\n";
295 #endif
296  gTheFactory = new Factory_T;
297 #ifdef DEBUGSTATIC
298  if ( gTheFactory )
299  std::clog << gTheFactory->GetName() << " done\n...]\n";
300  else
301  { std::clog << " *** ERROR *** \n"; std::clog << "...]\n" ;}
302 #endif
303  }
304  else { /* std::cout << "singleton already created!" << std::endl; */ }
305 
306  return (static_cast<Factory_T*> (gTheFactory));
307  }
308 
310  virtual Bool_t DoConfigure(std::ostream &out)
311  { return DotConf::DoConfigure(out); }
313  virtual Bool_t DoConfigure(std::istream &in, Bool_t allow_init)
314  { return DotConf::DoConfigure(in, allow_init); }
315 };
316 
317 template <typename Factory_T>
318 Factory_T *AKeyFactory<Factory_T>::gTheFactory = 0x0;
319 
320 } // namespace ADF
321 #endif
322 
323 
324 
325 
326 
virtual void SetLevel(ELevel lev, unsigned short verbosity=0u)
To get the current level of the message.
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
KeyFactory * GetFactory(std::string which_factory) const
Ask for a particular factory from is name.
Definition: KeyFactory.cpp:83
header file for FactoryItem.cpp
void ls(std::string o) const
list all factories
Definition: KeyFactory.cpp:220
DotConf : Utility for class configuration from ascii file or Configuration frames.
Definition: DotConf.h:96
virtual Key * DoNew(const FactoryItem &)
Do really the job of allocating the key.
Definition: KeyFactory.h:60
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
virtual Bool_t DoConfigure(std::ostream &out)
just call DotConf method, may be be overwitten
Definition: KeyFactory.h:310
virtual BaseBuffer::EEndian GetEndian() const
Endian type for the keys delivered by this factory.
Definition: KeyFactory.h:99
LogMessage & error(LogMessage &)
virtual Key * DoNew(const FactoryItem &)=0
Have to be overwritten.
Base class for a MainFactory i.e. a factory composed of factories.
Definition: KeyFactory.h:145
Bool_t IsKnown(std::string which_factory) const
check a factory has been registered
Definition: KeyFactory.cpp:111
virtual KeyFactory * Clone(const std::string &opt="full")
Clone.
Definition: KeyFactory.h:232
virtual Bool_t DoConfigure(std::istream &in, Bool_t allow_init)
just call DotConf method
Definition: KeyFactory.h:313
virtual KeyFactory * Clone(const std::string &)=0
realised clone of this factory
void SetName(std::string name)
Definition: KeyFactory.h:68
virtual void SetProcessMethod(const char *)
To set the current method.
bool IsDebug(short debug_to_test) const
to test if the required debug level is lower that the global one
LogMessage & nline(LogMessage &)
LogMessage fLog
the log messenger ... to know the object hae been properly intitialised/modified
Definition: DotConf.h:100
virtual const std::string & GetName() const
name of the factory
Definition: KeyFactory.h:94
virtual Bool_t DoConfigure(std::istream &, Bool_t allow_init)
configure this from an input stream
AKeyFactory(const char *factory_name, BaseBuffer::EEndian e=BaseBuffer::kLittle)
Definition: KeyFactory.h:221
virtual ~MainKeyFactory()
Definition: KeyFactory.cpp:70
virtual ~AKeyFactory()
Definition: KeyFactory.h:226
const std::vector< KeyFactory * > & ListOfFactories() const
direct access to the list of factories
Definition: KeyFactory.h:187
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
header file for DotConf.cpp
LogMessage & dolog(LogMessage &)
virtual ~KeyFactory()
destructor
Definition: KeyFactory.h:78
virtual void SetEndian(BaseBuffer::EEndian e)
Set Endian type for the keys delivered by this factory.
Definition: KeyFactory.h:104
static MainKeyFactory & theMainFactory()
the main (global) keyfactory
Definition: KeyFactory.cpp:150
KeyFactory(std::string factory_name, BaseBuffer::EEndian e=BaseBuffer::kLittle)
Constructor.
Definition: KeyFactory.h:75
virtual Key * New(const FactoryItem &)
build a key using item.
Definition: KeyFactory.cpp:155
Base Key factory.
Definition: KeyFactory.h:44
Base class for any KeyFactory implementation.
Definition: KeyFactory.h:215
Base class that described an item in a Factory.
Definition: FactoryItem.h:52