GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FrameFactory.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_FrameFactory
24 #define ADF_FrameFactory
25 #define ADF_FRAMEFACTORY_H ADF_FrameFactory
26 
27 #ifndef ADF_KeyFactory
28 #include "KeyFactory.h"
29 #endif
30 #ifndef ADF_PtrStack
31 #include "PtrStack.h"
32 #endif
33 #ifndef ADF_DotConf
34 #include "DotConf.h"
35 #endif
36 #include <string>
37 
38 
39 namespace ADF
40 {
41 
42 class Frame;
43 class SharedFP;
44 class MainFrameFactory;
45 
47 
49 class FrameFactory : public DotConf
50 {
51  friend class MainFrameFactory;
52 
53 private:
54  // used only by MainFactory
55  FrameFactory();
56 
57 private:
59  std::string fName;
60 
61 protected:
63  FrameFactory(std::string name);
64 
66  virtual SharedFP *DoNew(Key *, const FactoryItem &)
67  { return 0x0; }
68 
69 protected:
71  virtual FrameFactory *Clone(const std::string & = "full") = 0;
72 
73  // change the name of the factory in case default constructor has been used
74  void SetName(std::string name)
75  {
76  fName = name;
77  }
78 
79 public:
81  virtual ~FrameFactory()
82  {;}
83 
85 
88  virtual const std::string &GetName() const
89  {
90  return fName;
91  }
92 
94 
97  virtual SharedFP *NewSharedFrame(const FactoryItem &, const FactoryItem &);
98  //
99  virtual Frame *New(const FactoryItem &key_item, const FactoryItem &frame_item);
100 };
101 // FrameFactory inline members //////////////////////////////////////////////////////
102 
104 
107 {
108 private:
109  static MainFrameFactory gTheMainFactory;
110 
111 private:
113  std::vector <FrameFactory *> fFactories;
115  std::vector <FrameFactory *> fClonedFactories;
116 
117 private:
118  virtual FrameFactory *Clone(const std::string & = "full")
119  { return 0x0 ; } // the main factory cannot be cloned
120 
122 
124  FrameFactory *GetClonedFactory(std::string which_factory, void *) const;
125 
126 public:
128  MainFrameFactory(std::string factory_name);
129  virtual ~MainFrameFactory();
130 
132  const std::vector <FrameFactory *> &ListOfFactories() const
133  { return fFactories; }
134 
136 
139  virtual SharedFP *NewSharedFrame(const FactoryItem &, const FactoryItem &);
141  virtual Frame *New(const FactoryItem &key_item, const FactoryItem &frame_item);
142 
144 
146  virtual SharedFP *Clone(const SharedFP *);
148  virtual Frame *Clone(const Frame *frame);
149 
151 
155  FrameFactory *Clone(std::string which_factory, const std::string &opt = "full");
157  Int_t Clone(std::list<FrameFactory *> &, const std::string &opt = "full");
158 
160  Bool_t Register(FrameFactory *);
162  Bool_t Register(std::string factory_name);
163 
165 
167  FrameFactory *GetFactory(std::string which_factory) const;
168 
170  Bool_t IsKnown(std::string which_factory) const ;
171 
174 };
175 
176 
178 
180 template <typename Factory_T>
182 {
183 private:
184  static Factory_T *gTheFactory;
185 
186 protected:
187  AFrameFactory (const char *factory_name) : FrameFactory(factory_name)
188  {
189  if ( gTheFactory == 0x0 )
190  MainFrameFactory::theMainFactory().Register(this); // only the global one is registered. Others are cloned
191  }
192  virtual ~AFrameFactory ()
193  {
194  if ( fLog.IsDebug(5) ) {
195 
196  }
197  /* std::cout << "destroying singleton." << std::endl;*/
198  }
199 
201  virtual SharedFP *DoNew(Key *, const FactoryItem &) = 0;
202 
204  virtual FrameFactory *Clone(const std::string &opt = "full")
205  {
206  fLog.SetProcessMethod("ADF::AFrameFactory<>::Clone(const std::string &)");
207 
208  Factory_T * newfact = new Factory_T ();
209  if ( newfact == 0x0 ) {
210  fLog << error << "Cannot allocate a new cloned factory of " << GetName() << dolog;
211  return 0x0;
212  }
213  //
214  newfact->SetName(GetName());
215  //
216  if ( opt.find("full") == std::string::npos ) { // only constructor = empty instance, otherwise full clone
217 
218  fLog << info << "A new empty clone has been created for the factory " << GetName() << " " << nline;
219 
220  if ( fLog.IsDebug(2) ) {
222  DoConfigure( fLog() );
224  }
225  fLog << dolog;
226 
227  return newfact;
228  }
229  // use DotConf facilities to clone
230  Bool_t ok = true;
231  //
232  std::ostringstream o;
233  ok = ok &&
234  this->DoConfigure(o);
235  //
236  // std::string tmp;
237  // tmp = o.str();
238  std::istringstream i(o.str());
239  ok = ok &&
240  newfact->DoConfigure(i,true); //
241 
242  if ( !ok ) {
243  fLog << error << "the clone configuration has failed, delete it ! " << nline;
244 
245  delete newfact;
246  newfact = 0x0;
247  }
248  else {
249  fLog << info << "the clone configuration has succeded ! " << nline;
250 
251  if ( fLog.IsDebug(2) ) {
253  DoConfigure( fLog() );
255  }
256  }
257 
258  fLog << dolog;
259 
260  return newfact;
261  }
262 
263 public:
265  static Factory_T *theFactory ()
266  {
267  if (gTheFactory == 0x0) {
268 #ifdef DEBUGSTATIC
269  std::clog << "[[static]] Creation in Framefactory ]\n[...\n";
270 #endif
271  gTheFactory = new Factory_T;
272 #ifdef DEBUGSTATIC
273  if ( gTheFactory )
274  std::clog << gTheFactory->GetName() << " done\n...]\n";
275  else
276  { std::clog << " *** ERROR *** \n"; std::clog << "...]\n"; }
277 #endif
278  }
279  else { /* std::cout << "singleton already created!" << std::endl; */ }
280 
281  return (static_cast<Factory_T*> (gTheFactory));
282  }
284  virtual Bool_t DoConfigure(std::ostream &out)
285  { return DotConf::DoConfigure(out); }
287  virtual Bool_t DoConfigure(std::istream &in, Bool_t allow_init)
288  { return DotConf::DoConfigure(in, allow_init); }
289 };
290 
291 template <typename Factory_T>
292 Factory_T *AFrameFactory<Factory_T>::gTheFactory = 0x0;
293 
294 } // namespace ADF
295 #endif
296 
297 
298 
299 
300 
virtual void SetLevel(ELevel lev, unsigned short verbosity=0u)
To get the current level of the message.
Bool_t IsKnown(std::string which_factory) const
check a factory has been registered
Base class for a Key.
Definition: Key.h:56
AFrameFactory(const char *factory_name)
Definition: FrameFactory.h:187
static MainFrameFactory & theMainFactory()
the main (global) keyfactory
Base class for a FrameFactory.
Definition: FrameFactory.h:49
void SetName(std::string name)
Definition: FrameFactory.h:74
Base class for a Frame.
Definition: Frame.h:73
header file for KeyFactory.cpp
MainFrameFactory(std::string factory_name)
DotConf : Utility for class configuration from ascii file or Configuration frames.
Definition: DotConf.h:96
virtual Frame * New(const FactoryItem &key_item, const FactoryItem &frame_item)
just a frame
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
LogMessage & error(LogMessage &)
static Factory_T * theFactory()
to create the main (singleton) factory of that type
Definition: FrameFactory.h:265
virtual SharedFP * DoNew(Key *, const FactoryItem &)
Do really the job of allocating the frame.
Definition: FrameFactory.h:66
virtual FrameFactory * Clone(const std::string &opt="full")
Clone.
Definition: FrameFactory.h:204
template header file
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 Bool_t DoConfigure(std::istream &, Bool_t allow_init)
configure this from an input stream
virtual SharedFP * NewSharedFrame(const FactoryItem &, const FactoryItem &)
Ask the main for a new Frame.
virtual SharedFP * DoNew(Key *, const FactoryItem &)=0
Do really the job of allocating the frame.
virtual Bool_t DoConfigure(std::ostream &out)
Call Configure for all the registered factories.
Definition: FrameFactory.h:284
A Shared Frame Pointer.
Definition: Frame.h:597
virtual Bool_t DoConfigure(std::istream &in, Bool_t allow_init)
Call Configure for all the registered factories.
Definition: FrameFactory.h:287
header file for DotConf.cpp
virtual ~FrameFactory()
Definition: FrameFactory.h:81
FrameFactory * GetFactory(std::string which_factory) const
Ask for a particular factory from is name.
Bool_t Register(FrameFactory *)
Add one factory to this collection of keyfactories: name has to be unique so return false if a factor...
virtual Frame * New(const FactoryItem &key_item, const FactoryItem &frame_item)
virtual FrameFactory * Clone(const std::string &="full")=0
realised clone of this factory
LogMessage & dolog(LogMessage &)
Base class for any kind of Frame factory.
Definition: FrameFactory.h:181
virtual const std::string & GetName() const
name of the factory
Definition: FrameFactory.h:88
virtual SharedFP * NewSharedFrame(const FactoryItem &, const FactoryItem &)
Ask this factory for a new Frame.
Base class that described an item in a Factory.
Definition: FactoryItem.h:52
virtual ~AFrameFactory()
Definition: FrameFactory.h:192
const std::vector< FrameFactory * > & ListOfFactories() const
direct access to the list of factories
Definition: FrameFactory.h:132
Base class for a MainFactory i.e. a factory composed of factories.
Definition: FrameFactory.h:106