source: trunk/MagicSoft/Mars/mbase/MParEnv.cc@ 9492

Last change on this file since 9492 was 9276, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 1/2009 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2009
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MParEnv
28//
29// A wrapper container which allows to set a new container from the
30// resource file. Via SetClassName you can define which type of class
31// you want to allow (the given object must inherit from it).
32//
33// For further details see MParEnv::ReadEnv
34//
35/////////////////////////////////////////////////////////////////////////////
36#include "MParEnv.h"
37
38#include <TClass.h>
39#include <TInterpreter.h>
40
41#include "MLog.h"
42#include "MLogManip.h"
43
44ClassImp(MParEnv);
45
46using namespace std;
47
48// --------------------------------------------------------------------------
49//
50// Default constructor.
51//
52MParEnv::MParEnv(const char *name, const char *title) : fCont(0), fClassName("MParContainer"), fIsOwner(kFALSE)
53{
54 fName = name ? name : "MParEnv";
55 fTitle = title ? title : "A wrapper container which allows to set a new container from the resource file";
56}
57
58// --------------------------------------------------------------------------
59//
60// Delete fOwner if we own the instance.
61//
62MParEnv::~MParEnv()
63{
64 if (fIsOwner)
65 delete fCont;
66}
67
68// --------------------------------------------------------------------------
69//
70// Print the descriptor of this instance.
71// Calls Print() of fCont if fCont is set.
72//
73void MParEnv::Print(Option_t *o) const
74{
75 *fLog << all << GetDescriptor() << endl;
76 if (fCont)
77 fCont->Print();
78 *fLog << endl;
79}
80
81// --------------------------------------------------------------------------
82//
83// Set the class name fCont must inherit from. Check if the class name
84// inherits from MParContainer.
85// In case of success returns kTRUE, kFALSE otherwise.
86//
87Bool_t MParEnv::SetClassName(const char *name)
88{
89 TClass *cls = GetClass(name, fLog);
90 if (!cls)
91 return kFALSE;
92
93 if (!cls->InheritsFrom(MParContainer::Class()))
94 {
95 *fLog << err << "ERROR - " << name << " must inherit from MParContainer." << endl;
96 return kFALSE;
97 }
98
99 fClassName = name;
100
101 return kTRUE;
102}
103
104// --------------------------------------------------------------------------
105//
106// Deletes fCont if we are owner.
107// Sets fCont to 0.
108// Checks if c inherits from fClassName.
109// Sets fIsOwner to kFALSE.
110// Sets fCont to c.
111// Sets name of c to fName.
112//
113void MParEnv::SetCont(MParContainer *c)
114{
115 if (fIsOwner && fCont)
116 delete fCont;
117 fCont=0;
118
119 if (!c)
120 return;
121
122 TClass *cls = GetClass(fClassName, fLog);
123 if (!c->InheritsFrom(cls))
124 {
125 *fLog << err << "ERROR - MParEnv::SetCont: " << c->ClassName() << " doesn't inherit from " << fClassName << endl;
126 return;
127 }
128
129 fIsOwner = kFALSE;
130
131 fCont = c;
132 fCont->SetName(fName);
133}
134
135// --------------------------------------------------------------------------
136//
137// Checks for Class and Constructor.
138//
139// First we check for
140//
141// MParEnv.Class: MMyContainer
142//
143// which would create a new instance of MMyContainer which must inherit from
144// fClassName. The instance is created via TObject::New()
145//
146// If this was not found we check for
147//
148// MParEnv.Constructor: MMyContainer(14, "Hallo", "Test");
149//
150// which calls the given constructor via gInterpreter->ProcessLine. Also here
151// the result must inherit from fClassName.
152//
153// After successfull creation of the new class (or if a class was set already
154// before and not overwritten in the resource file) the container's ReadEnv
155// is called.
156//
157// WARNING: Especially in the second case oyu can do weird thing which
158// in principle can result in crashes. Please be extra-carefull.
159//
160Int_t MParEnv::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
161{
162 MParContainer *cont = 0;
163 Bool_t rc = kFALSE;
164 if (IsEnvDefined(env, prefix, "Class", print))
165 {
166 rc = kTRUE;
167 TString cls = GetEnvValue(env, prefix, "Class", "");
168 cont = GetNewObject(cls, fClassName);
169 }
170
171 if (!cont && IsEnvDefined(env, prefix, "Constructor", print))
172 {
173 rc = kTRUE;
174 if (!gInterpreter)
175 {
176 *fLog << err << "ERROR - gInterpreter==NULL." << endl;
177 return kERROR;
178 }
179
180 TString line = GetEnvValue(env, prefix, "Constructor", "");
181
182 cont = (MParContainer*)gInterpreter->ProcessLine(TString("new ")+line);
183 }
184
185 if (!rc && !fCont) // FIXME: fAllowEmpty
186 return kFALSE;
187
188 if (rc)
189 {
190 SetCont(cont);
191 if (!fCont)
192 {
193 if (cont)
194 delete cont;
195 return kERROR;
196 }
197 fIsOwner = kTRUE;
198 }
199
200 const Int_t res = fCont->ReadEnv(env, prefix, print);
201 if (res==kERROR)
202 return kERROR;
203
204 return rc ? rc : res;
205}
Note: See TracBrowser for help on using the repository browser.