GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FrameFactory.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 #ifndef ADF_FrameFactory
24 #include "FrameFactory.h"
25 #endif
26 
27 #ifndef ADF_ConfigurationFrame
28 #include "ConfigurationFrame.h"
29 #endif
30 #include "DefaultFrameFactory.h"
31 
32 #ifndef ADF_AgataFrameFactory
33 #include "AgataFrameFactory.h"
34 #endif
35 
36 using namespace ADF;
37 
38 
39 
40 FrameFactory::FrameFactory():
41  DotConf("ADF::FrameFactory"),
42  fName("MainFactory")
43 {
44 }
45 
46 FrameFactory::FrameFactory(std::string name):
47  DotConf("ADF::FrameFactory"),
48  fName(name)
49 {
50 }
51 
52 SharedFP *FrameFactory::NewSharedFrame(const FactoryItem &key_item, const FactoryItem &frame_item)
53 {
54  FactoryItem l_item(frame_item); SharedFP *sfp = 0x0;
55 
56  // ask for a key, using the main key factory
57  Key *key = MainKeyFactory::theMainFactory().New(key_item);
58  if ( key == 0x0 )
59  return sfp;
60 
61  // try and allocate the frame and set the signature properly ... delete the key in case of failure
62  sfp = DoNew(key,frame_item);
63  if ( sfp ) {
64  // keeps in key a reference to the factory that built it
65  l_item.SetFactoryPtr(this);
66  sfp->GetFrame()->fSignature = l_item;
67  }
68  else delete key;
69 
70  return sfp;
71 }
72 
73 Frame *FrameFactory::New(const FactoryItem &key_item, const FactoryItem &frame_item)
74 {
75  SharedFP *sfp = NewSharedFrame(key_item, frame_item);
76  if( sfp == 0x0 )
77  return 0x0;
78 
79  Frame *frame = sfp->SetFrame(0x0); // extract frame from the shared pointer
80  delete sfp;
81 
82  return frame;
83 }
84 
85 // pointer to the main key factory
86 MainFrameFactory MainFrameFactory::gTheMainFactory("MainFactory");
87 
88 MainFrameFactory::MainFrameFactory(std::string factory_name) : FrameFactory(factory_name),
89  fFactories(),
90  fClonedFactories()
91 {
92  // Add Default as the first factory
93  // AFrameFactory<DefaultFrameFactory>::theFactory();
94 }
95 
97 {
98  for (size_t i = 0; i < fFactories.size(); i++) {
99  delete fFactories[i];
100  }
101  fFactories.clear();
102  for (size_t i = 0; i < fClonedFactories.size(); i++) {
103  delete fClonedFactories[i];
104  }
105  fClonedFactories.clear();
106 }
107 
108 FrameFactory *MainFrameFactory::GetFactory(std::string which_factory) const
109 {
110  FrameFactory *factory = 0x0;
111  //
112  for (size_t i = 0; i < fFactories.size(); i++) {
113  if ( fFactories[i]->GetName() == which_factory )
114  {
115  factory = fFactories[i];
116  break;
117  }
118  }
119  return factory;
120 }
121 
122 FrameFactory *MainFrameFactory::GetClonedFactory(std::string which_factory, void *ptr) const
123 {
124  FrameFactory *factory = 0x0;
125  //
126  for (size_t i = 0; i < fClonedFactories.size(); i++) {
127  if ( fClonedFactories[i]->GetName() == which_factory && fClonedFactories[i] == ptr )
128  {
129  factory = fClonedFactories[i];
130  break;
131  }
132  }
133  return factory;
134 }
135 
136 Bool_t MainFrameFactory::IsKnown(std::string which_factory) const
137 {
138  return GetFactory(which_factory) != 0x0;
139 }
140 
142 {
143  Bool_t ok = false;
144 
145  if ( !IsKnown(anewfactory->GetName()) ) {
146  // add it to the factories
147  fFactories.push_back(anewfactory);
148  // and to the list of DotConf so that configuration/reconfiguration by files/frames could be performed
149  AddConf(anewfactory);
150  //
151  ok = true;
152  }
153 
154  return ok;
155 }
156 
157 Bool_t MainFrameFactory::Register(std::string factory_name)
158 {
159  Bool_t ok = false;
160 
161  if ( IsKnown(factory_name) )
162  return true; // already registered
163 
164  if ( factory_name == "Default" ) { // default registered once Main is built
166  ok = true;
167  }
168  if ( factory_name == "Agata" ) {
170  ok = true;
171  }
172  return ok;
173 }
174 
176 {
177  return gTheMainFactory;
178 }
179 
180 FrameFactory *MainFrameFactory::Clone(std::string which_factory, const std::string &opt)
181 {
182  FrameFactory *factory = GetFactory(which_factory);
183  if ( factory == 0x0 )
184  return 0x0;
185 
186  FrameFactory *cloned = factory->Clone(opt);
187  if ( cloned ) {
188  fClonedFactories.push_back(cloned);
189  }
190 
191  return cloned;
192 }
193 
194 Int_t MainFrameFactory::Clone(std::list<FrameFactory *> &cloned_list, const std::string &opt)
195 {
196  Int_t nb_cloned = 0; std::vector< FrameFactory * > tmp(fFactories); // first created a tmp vector since fFactories is modified by individual call of Clone
197 
198  for (size_t i = 0; i < tmp.size(); i++) {
199  // use Clone method for one factory
200  FrameFactory *newfact = Clone(tmp[i]->GetName(),opt);
201  // add it to the return cloned_list. to be kept by the user since a Cloned factory could be retrieved only by its pointer
202  if ( newfact )
203  {
204  cloned_list.push_back(newfact);
205  nb_cloned++;
206  }
207  }
208  return nb_cloned;
209 }
210 
212 {
213  FrameFactory *factory = GetFactory(frame_item.GetFactoryName());
214 
215  // 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
216  // 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
217  // otherwise look in the clone factory
218  if ( frame_item.GetFactoryPtr() == 0x0 || frame_item.GetFactoryPtr() == factory )
219  ;
220  else
221  factory = GetClonedFactory(frame_item.GetFactoryName(),frame_item.GetFactoryPtr());
222  //
223  SharedFP *sfp = 0x0;
224  if ( factory )
225  sfp = factory->NewSharedFrame(key_item,frame_item);
226 
227  return sfp;
228 }
229 
230 Frame *MainFrameFactory::New(const FactoryItem &key_item, const FactoryItem &frame_item)
231 {
232  SharedFP *sfp = NewSharedFrame(key_item,frame_item); Frame *frame = 0x0;
233  if( sfp ) {
234  // remove frame from shared pointer and delete sfp
235  frame = sfp->SetFrame(0x0);
236  delete sfp;
237  }
238  return frame;
239 }
240 
241 SharedFP *MainFrameFactory::Clone(const SharedFP *sfp)
242 {
243  const Frame *frame;
244 
245  // be sure the input frame exists
246  if ( sfp == 0x0 )
247  return 0x0;
248  else
249  if ( sfp->GetFrame() == 0x0 )
250  return 0x0;
251  //
252  frame = sfp->GetFrame();
253 
254  // to get the same factory. In principle the frame has been already allocated so the factory is known ... except if the user has deleted it !
255  FactoryItem kitem = frame->GetKey()->GetSignature();
256  FactoryItem fitem = frame->GetSignature();
257  //
258  SharedFP *csfp = NewSharedFrame(kitem,fitem);
259  if ( csfp ) {
260  Bool_t ok =
261  csfp->GetFrame()->Copy(frame);
262 
263  if ( ok == false ) {
264  delete csfp; csfp = 0x0;
265  }
266  else {
268  }
269  }
270  // return the clone
271  return csfp;
272 }
273 
274 Frame *MainFrameFactory::Clone(const Frame *frame)
275 {
276  FactoryItem kitem = frame->GetKey()->GetSignature();
277  FactoryItem fitem = frame->GetSignature();
278 
279  Frame *cframe = New(kitem,fitem);
280  if ( cframe ) {
281  Bool_t ok =
282  cframe->Copy(frame);
283 
284  if ( ok == false ) {
285  delete cframe; cframe = 0x0;
286  }
287  }
288  return cframe;
289 }
290 
291 
292 
293 
Bool_t IsKnown(std::string which_factory) const
check a factory has been registered
virtual Key * GetKey()=0
To get the Key associated to this frame.
virtual Frame * GetFrame() const
Definition: Frame.h:625
Base class for a Key.
Definition: Key.h:56
static MainFrameFactory & theMainFactory()
the main (global) keyfactory
Base class for a FrameFactory.
Definition: FrameFactory.h:49
header file for AgataFrameFactory.cpp
Base class for a Frame.
Definition: Frame.h:73
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
const FactoryItem & GetSignature() const
Definition: Key.h:115
header file for DefaultFrameFactory.cpp
virtual void SetFrameChangeFunction(PF_FactoryItemChange pf)
Definition: Frame.h:646
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
const PF_FactoryItemChange & GetKeyChangeFunction() const
Definition: Frame.h:637
virtual SharedFP * NewSharedFrame(const FactoryItem &, const FactoryItem &)
Ask the main for a new Frame.
const std::string & GetFactoryName() const
Definition: FactoryItem.h:76
A Shared Frame Pointer.
Definition: Frame.h:597
virtual Frame * SetFrame(Frame *)
change the frame, returns the current one
Definition: Frame.cpp:275
virtual Bool_t Copy(const Char_t *, UInt_t, const char= 'b')=0
copy a buffer to this Frame
FrameFactory * GetFactory(std::string which_factory) const
Ask for a particular factory from is name.
virtual void SetKeyChangeFunction(PF_FactoryItemChange pf)
Definition: Frame.h:634
void SetFactoryPtr(void *ptr)
change
Definition: FactoryItem.h:97
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
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
header file for FrameFactory.cpp
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.
void * GetFactoryPtr() const
Definition: FactoryItem.h:94
virtual Key * New(const FactoryItem &)
build a key using item.
Definition: KeyFactory.cpp:155
const PF_FactoryItemChange & GetFrameChangeFunction() const
Definition: Frame.h:649
Base class that described an item in a Factory.
Definition: FactoryItem.h:52
const FactoryItem & GetSignature() const
Signature of that frame.
Definition: Frame.h:127
Base class for a MainFactory i.e. a factory composed of factories.
Definition: FrameFactory.h:106