KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVClassFactory.h
Go to the documentation of this file.
1 /*
2 $Id: KVClassFactory.h,v 1.13 2009/01/21 08:04:20 franklan Exp $
3 $Revision: 1.13 $
4 $Date: 2009/01/21 08:04:20 $
5 */
6 
7 #ifndef __KVCLASSFACTORY_H
8 #define __KVCLASSFACTORY_H
9 
10 #include "Riostream.h"
11 #include "TDatime.h"
12 #include "TClass.h"
13 #include "KVString.h"
14 #include "KVNameValueList.h"
15 #include "KVList.h"
16 #include "KVBase.h"
17 
18 
85 class KVClassFactory : public TObject {
86 
87 private:
88 
110 
111 protected:
112 
113  void WritePreProc(std::ofstream&);
114  void WriteWhoWhen(std::ofstream&);
115  void SetWhoWhen();
116  void WriteClassDec(std::ofstream&);
117  void WriteClassHeader();
118  void WriteClassImp();
121  Bool_t CheckTemplateFiles(const Char_t* base_class,
122  const Char_t* templateFile);
123 
124 
126  void AddTObjectCopyMethod();
127  void AddCopyConstructor();
128 
130  void AddAssignmentOperator();
131 public:
132 
137  class KVClassMember : public KVBase {
138 
139  protected:
140  KVString fComment;//informative comment for member variable, like this one :)
141  KVString fRealName;//the name of the member without the leading 'f'
142 
143  public:
145  {
146  SetAccess();
147  }
148  KVClassMember(const Char_t*, const Char_t*, const Char_t*, const Char_t* = "protected");
150  ROOT_COPY_ASSIGN_OP(KVClassMember)
151  virtual ~KVClassMember() {}
152  void Copy(TObject& obj) const;
153 
154  void Print(Option_t* = "") const;
155 
156  virtual void WriteDeclaration(KVString&);
157 
159  void SetAccess(const Char_t* acc = "public")
160  {
161  SetLabel(acc);
162  }
163 
165  const Char_t* GetAccess() const
166  {
167  return GetLabel();
168  }
169  Bool_t IsPublic() const
170  {
172  return !strcmp(GetAccess(), "public");
173  }
175  {
177  return !strcmp(GetAccess(), "protected");
178  }
180  {
182  return !strcmp(GetAccess(), "private");
183  }
184 
186  void SetComment(const Char_t* c)
187  {
188  fComment = c;
189  }
190 
192  const Char_t* GetComment() const
193  {
194  return fComment;
195  }
196 
198  const Char_t* GetRealName() const
199  {
200  return fRealName;
201  }
202 
203  ClassDef(KVClassMember, 1) //KVClassFactory helper class - description of class member variable
204  };
205 
207 
213  class KVClassMethod : public KVClassMember {
214 
215  protected:
216  virtual void write_method_body(KVString& decl);
217  KVNameValueList fFields;//fields of method declaration
218  Bool_t fVirtual;//kTRUE if method is 'virtual'
219  Bool_t fConst;//kTRUE if method is 'const'
220  int fNargs;//counts arguments
221  Bool_t fInline;//kTRUE if method body is to be written in header file
222 
223  public:
224 
225  Int_t GetNargs() const
226  {
227  return fNargs;
228  }
229  void SetNargs(Int_t n)
230  {
231  fNargs = n;
232  }
233 
234  KVClassMethod(Bool_t Virtual = kFALSE, Bool_t Const = kFALSE, Bool_t Inline = kFALSE)
235  : KVClassMember(), fVirtual(Virtual), fConst(Const), fNargs(0), fInline(Inline)
236  {
237  }
239  virtual ~KVClassMethod() {}
240  void Copy(TObject& obj) const;
241 
242  void SetReturnType(const Char_t* type)
243  {
244  KVString s(type);
245  fFields.SetValue("ReturnType", s);
246  }
247  void SetClassName(const Char_t* name)
248  {
249  KVString s(name);
250  fFields.SetValue("ClassName", s);
251  }
252  void SetBaseClass(const Char_t* name)
253  {
254  KVString s(name);
255  fFields.SetValue("BaseClass", s);
256  }
257  void AddArgument(const Char_t* type, const Char_t* argname = "", const Char_t* defaultvalue = "")
258  {
259  KVString _type(type);
260  fFields.SetValue(Form("Arg_%d", ++fNargs), _type);
261  if (strcmp(defaultvalue, "")) {
262  KVString _s(defaultvalue);
263  fFields.SetValue(Form("Arg_%d_default", fNargs), _s);
264  }
265  if (strcmp(argname, "")) {
266  KVString _s(argname);
267  fFields.SetValue(Form("Arg_%d_name", fNargs), _s);
268  }
269  }
270  void AddArgument(Int_t i, const Char_t* type, const Char_t* argname = "", const Char_t* defaultvalue = "");
271  void SetMethodBody(const KVString& body)
272  {
273  fFields.SetValue("Body", body);
274  }
275  void SetMethodComment(const KVString& com)
276  {
277  fFields.SetValue("Comment", com);
278  }
280  {
281  if (fFields.HasParameter("ReturnType"))
282  return fFields.GetStringValue("ReturnType");
283  return "";
284  }
286  {
287  return fFields.GetStringValue("ClassName");
288  }
289 
290  void SetConst(Bool_t c = kTRUE)
291  {
292  fConst = c;
293  }
294  void SetVirtual(Bool_t c = kTRUE)
295  {
296  fVirtual = c;
297  }
298 
299  Bool_t IsConst() const
300  {
301  return fConst;
302  }
304  {
305  return fVirtual;
306  }
307  virtual Bool_t IsConstructor() const
308  {
309  return kFALSE;
310  }
311 
312  void Print(Option_t* = "") const;
313 
314  void WriteDeclaration(KVString&);
315  void WriteImplementation(KVString& decl);
316 
317  void SetInline(Bool_t yes = kTRUE)
318  {
319  fInline = yes;
320  }
321  Bool_t IsInline() const
322  {
323  return fInline;
324  }
325  virtual Bool_t IsDestructor() const
326  {
327  return kFALSE;
328  }
330  {
332  return (!IsConstructor() && !IsDestructor());
333  }
334 
335  ClassDef(KVClassMethod, 2) //KVClassFactory helper class - description of class method
336  };
337 
344  Bool_t fCopyCtor;//kTRUE if method is the copy constructor
345  virtual void write_method_body(KVString& decl);
347 
348  public:
350  : KVClassMethod(), fCopyCtor(kFALSE), fParentClass(ParentClass) {};
351  virtual ~KVClassConstructor() {};
352 
353  virtual Bool_t IsConstructor() const
354  {
355  return kTRUE;
356  }
358  {
360  return IsCalled("default_ctor");
361  }
363  {
364  return fCopyCtor;
365  }
366  void SetCopyCtor(Bool_t c = kTRUE)
367  {
368  fCopyCtor = c;
369  }
371  {
373  fFields.SetValue(Form("Arg_%d_baseclass", i), 1);
374  }
376  {
378  fFields.SetValue(Form("Arg_%d_memvar", i), memvar);
379  }
380 
381  ClassDef(KVClassConstructor, 1) //KVClassFactory helper class - description of constructor
382  };
383 
384 protected:
385  void AddMemberInitialiserConstructor(KVClassConstructor* = nullptr);
386 
387 public:
394 
395  public:
398  {
399  SetName("destructor");
400  SetMethodComment("Destructor");
401  }
402  virtual ~KVClassDestructor() {}
403  virtual Bool_t IsDestructor() const
404  {
405  return kTRUE;
406  }
407 
408  ClassDef(KVClassDestructor, 1) //KVClassFactory helper class - description of destructor
409  };
410 
411  KVClassFactory();
412  KVClassFactory(const Char_t* classname,
413  const Char_t* classdesc,
414  const Char_t* base_class =
415  "", Bool_t withTemplate =
416  kFALSE, const Char_t* templateFile = "");
418  virtual ~KVClassFactory() {};
419 #if ROOT_VERSION_CODE >= ROOT_VERSION(3,4,0)
420  void Copy(TObject& obj) const;
421 #else
422  void Copy(TObject& obj);
423 #endif
424 
425  static void MakeClass(const Char_t* classname,
426  const Char_t* classdesc,
427  const Char_t* base_class =
428  "", Bool_t withTemplate =
429  kFALSE, const Char_t* templateFile = "");
430  void GenerateCode();
431 
432  KVClassMember* AddMember(const Char_t* name, const Char_t* type, const Char_t* comment, const Char_t* access = "protected");
433  KVClassMethod* AddMethod(const Char_t* name, const Char_t* return_type, const Char_t* access = "public",
434  Bool_t isVirtual = kFALSE, Bool_t isConst = kFALSE);
435  KVClassConstructor* AddConstructor(const Char_t* argument_type = "",
436  const Char_t* argument_name = "", const Char_t* default_value = "", const Char_t* access = "public");
437  void AddAllBaseConstructors();
438  void AddDefaultConstructor();
439  void AddDestructor(const TString& access = "public");
440  void AddMethod(const KVClassMethod& kvcm);
441  void AddMethodArgument(const Char_t* method_name, const Char_t* argument_type,
442  const Char_t* argument_name = "", const Char_t* default_value = "");
443  void AddMethodBody(const Char_t* method_name, const KVString& body);
444  void AddMethodComment(const Char_t* method_name, const KVString& comment);
445 
446  void AddHeaderIncludeFile(const Char_t* filename);
447  void AddImplIncludeFile(const Char_t* filename);
448 
449  const KVList* GetListOfMethods() const
450  {
451  return &fMethods;
452  }
453  const KVList* GetListOfMembers() const
454  {
455  return &fMembers;
456  }
457  KVClassMethod* GetMethod(const Char_t* name) const
458  {
459  return fMethods.get_object<KVClassMethod>(name);
460  }
462  {
463  return (KVClassConstructor*)GetMethod("default_ctor");
464  }
466  {
467  return (KVClassDestructor*)GetMethod("destructor");
468  }
469 
470  const Char_t* GetClassName() const
471  {
472  return fClassName.Data();
473  }
474  void SetClassName(const Char_t* n)
475  {
476  fClassName = n;
477  }
478  const Char_t* GetHeaderFileName() const
479  {
483  return fClassPath != "" ? Form("%s%s.h", fClassPath.Data(), fClassName.Data()) : Form("%s.h", fClassName.Data());
484  }
485  const Char_t* GetImpFileName() const
486  {
490  return fClassPath != "" ? Form("%s%s.cpp", fClassPath.Data(), fClassName.Data()) : Form("%s.cpp", fClassName.Data());
491  }
492  void SetClassDesc(const Char_t* d)
493  {
494  fClassDesc = d;
495  }
496  const Char_t* GetClassDesc() const
497  {
498  return fClassDesc.Data();
499  }
500  void SetBaseClass(const Char_t* b)
501  {
502  fBaseClassName = b;
503  fHasBaseClass = (fBaseClassName != "");
505  if (fHasBaseClass) {
508  }
509  }
510  const Char_t* GetBaseClass() const
511  {
512  return fBaseClassName.Data();
513  }
515  {
516  return fBaseClassName.Contains(",");
517  }
519  {
520  return fWithTemplate;
521  }
523  {
524  return fBaseClassTObject;
525  }
526  void SetTemplate(Bool_t temp, const Char_t* temp_file);
527  const Char_t* GetTemplateBase() const
528  {
529  return fTemplateBase;
530  }
532  {
533  return fMembers.GetEntries();
534  }
535 
536  void Print(Option_t* opt = "") const;
537 
538  void AddGetSetMethods(const KVNameValueList&);
539 
540  void InlineAllMethods(bool yes = true);
541  void InlineAllConstructors(bool yes = true);
542 
543  void SetOutputPath(const KVString& p)
544  {
547  fClassPath = p;
548  if (!fClassPath.EndsWith("/")) fClassPath.Append("/");
549  }
550  const Char_t* GetOutputPath() const
551  {
554  return fClassPath;
555  }
556 
558  {
560  fInheritAllCtors = yes;
561  }
562 
563  ClassDef(KVClassFactory, 5) //Factory for generating KaliVeda skeleton classes
564 };
565 
566 #endif
int Int_t
#define d(i)
#define b(i)
#define c(i)
int Ssiz_t
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
const char Option_t
#define ClassDef(name, id)
int type
char * Form(const char *fmt,...)
Base class for KaliVeda framework.
Definition: KVBase.h:135
void SetLabel(const Char_t *lab)
Definition: KVBase.h:188
const Char_t * GetLabel() const
Definition: KVBase.h:192
virtual Bool_t IsCalled(const Char_t *name) const
Definition: KVBase.h:183
Constructor for a class generated with KVClassFactory.
virtual Bool_t IsConstructor() const
virtual void write_method_body(KVString &decl)
KVClassConstructor(KVClassFactory *ParentClass)
void SetMemberVariableNameForArgument(Int_t i, const Char_t *memvar)
Destructor for a class generated with KVClassFactory.
virtual Bool_t IsDestructor() const
Member variable in a class generated with KVClassFactory.
virtual void WriteDeclaration(KVString &)
Write declaration in the KVString object.
void SetComment(const Char_t *c)
set comment for variable
void Print(Option_t *="") const
print the KVClass member
void SetAccess(const Char_t *acc="public")
set access type : public, protected or private
const Char_t * GetRealName() const
get short name without leading 'f'
const Char_t * GetComment() const
get access type : public, protected or private
const Char_t * GetAccess() const
get access type : public, protected or private
void Copy(TObject &obj) const
copy this to obj
Method in a class generated with KVClassFactory.
virtual void write_method_body(KVString &decl)
void SetMethodComment(const KVString &com)
void Print(Option_t *="") const
print the KVClass method
KVClassMethod(Bool_t Virtual=kFALSE, Bool_t Const=kFALSE, Bool_t Inline=kFALSE)
virtual Bool_t IsDestructor() const
void SetConst(Bool_t c=kTRUE)
void WriteImplementation(KVString &decl)
void SetBaseClass(const Char_t *name)
void Copy(TObject &obj) const
copy this to obj
virtual Bool_t IsConstructor() const
void AddArgument(const Char_t *type, const Char_t *argname="", const Char_t *defaultvalue="")
void SetReturnType(const Char_t *type)
void SetClassName(const Char_t *name)
void SetVirtual(Bool_t c=kTRUE)
void SetInline(Bool_t yes=kTRUE)
void SetMethodBody(const KVString &body)
Factory class for generating skeleton files for new classes.
void Print(Option_t *opt="") const
Print infos on object.
void SetOutputPath(const KVString &p)
Bool_t fInlineAllCtors
kTRUE if all ctor implementations written in header
void WriteWhoWhen(std::ofstream &)
const Char_t * GetClassDesc() const
Bool_t WithMultipleBaseClasses() const
void SetTemplate(Bool_t temp, const Char_t *temp_file)
KVList fMembers
list of member variables for class
Bool_t fInlineAllMethods
kTRUE if all (non-ctor) method implementations written in header
void AddMethodComment(const Char_t *method_name, const KVString &comment)
Set the comments for method 'method_name' added to the class using AddMethod.
void WriteClassWithTemplateImp()
Writes the implementation file for the class.
KVString fTemplateClassName
name of template dummy class
Bool_t IsBaseClassTObject() const
void GenerateGettersAndSetters()
virtual ~KVClassFactory()
KVList fMethods
list of methods added to class
void GenerateCode()
Generate header and implementation file for currently-defined class.
TDatime fNow
for dating files
KVClassMethod * GetMethod(const Char_t *name) const
const Char_t * GetTemplateBase() const
Bool_t fHasBaseClass
kTRUE if class derived from another
void SetClassName(const Char_t *n)
void SetInheritAllConstructors(Bool_t yes=kTRUE)
const Char_t * GetImpFileName() const
const KVList * GetListOfMethods() const
KVString fTemplateCPP
full path to template .cpp
const KVList * GetListOfMembers() const
void AddMemberInitialiserConstructor(KVClassConstructor *=nullptr)
TClass * fBaseClass
description of base class
Bool_t WithTemplate() const
void WriteClassHeader()
Write the class header file.
KVString fClassDesc
class description
Bool_t fInheritAllCtors
kTRUE if all ctor from base class should be inherited
const Char_t * GetHeaderFileName() const
KVClassFactory()
Default ctor.
KVString fClassPath
directory in which to write source files, if not working directory
KVClassConstructor * GetDefaultCtor() const
static void MakeClass(const Char_t *classname, const Char_t *classdesc, const Char_t *base_class="", Bool_t withTemplate=kFALSE, const Char_t *templateFile="")
void AddHeaderIncludeFile(const Char_t *filename)
void Copy(TObject &obj) const
Copy the state of this KVClassFactory to the one referenced by 'obj'.
void AddDestructor(const TString &access="public")
Add default destructor to class.
void WriteClassDec(std::ofstream &)
void AddImplIncludeFile(const Char_t *filename)
KVString fTemplateH
full path to template .h
KVString fBaseClassName
name of base class
Bool_t CheckTemplateFiles(const Char_t *base_class, const Char_t *templateFile)
void AddDefaultConstructor()
const Char_t * GetClassName() const
void SetClassDesc(const Char_t *d)
void WritePreProc(std::ofstream &)
Int_t GetNumberOfMemberVariables() const
void AddMethodBody(const Char_t *method_name, const KVString &body)
Ssiz_t FindNextUncommentedLine(TString &, Ssiz_t beg=0)
const Char_t * GetBaseClass() const
void WriteClassWithTemplateHeader()
void AddAssignmentOperator()
void InlineAllMethods(bool yes=true)
const Char_t * GetOutputPath() const
Bool_t fBaseClassTObject
kTRUE if class derived from TObject
KVList fHeadInc
list of 'includes' to be added to header file
KVString fTemplateBase
template base name passed to SetTemplate method
void AddMethodArgument(const Char_t *method_name, const Char_t *argument_type, const Char_t *argument_name="", const Char_t *default_value="")
void InlineAllConstructors(bool yes=true)
KVString fClassName
name of class to generate
KVClassDestructor * GetDestructor() const
void SetBaseClass(const Char_t *b)
void AddGetSetMethods(const KVNameValueList &)
For each named parameter in the list, we add protected member variables with the name and type of the...
Bool_t fWithTemplate
true if class has a template
KVClassConstructor * AddConstructor(const Char_t *argument_type="", const Char_t *argument_name="", const Char_t *default_value="", const Char_t *access="public")
KVClassMember * AddMember(const Char_t *name, const Char_t *type, const Char_t *comment, const Char_t *access="protected")
KVString fAuthor
user who called ClassFactory to generate class
KVList fImpInc
list of 'includes' to be added to implementation file
void AddAllBaseConstructors()
KVClassMethod * AddMethod(const Char_t *name, const Char_t *return_type, const Char_t *access="public", Bool_t isVirtual=kFALSE, Bool_t isConst=kFALSE)
Extended TList class which owns its objects by default.
Definition: KVList.h:27
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
void SetValue(const Char_t *name, value_type value)
const Char_t * GetStringValue(const Char_t *name) const
Bool_t HasParameter(const Char_t *name) const
T * get_object(const char *name) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t InheritsFrom(const char *cl) const
virtual Int_t GetEntries() const
virtual void SetName(const char *name)
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
TString & Append(char c, Ssiz_t rep=1)
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
const Int_t n
const long double s
Definition: KVUnits.h:94