KaliVeda
Toolkit for HIC analysis
KVClassFactory.h
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 
86 class KVClassFactory : public TObject {
87 
88 private:
89 
90  KVString fClassName;
91  KVString fClassDesc;
92  KVString fBaseClassName;
93  TClass* fBaseClass;
94  Bool_t fHasBaseClass;
95  Bool_t fBaseClassTObject;
96  KVString fAuthor;
97  TDatime fNow;
98  Bool_t fWithTemplate;
99  KVString fTemplateBase;
100  KVString fTemplateClassName;
101  KVString fTemplateH;
102  KVString fTemplateCPP;
103  KVList fMembers;
104  KVList fMethods;
105  KVList fHeadInc;
106  KVList fImpInc;
107  Bool_t fInlineAllMethods;
108  Bool_t fInlineAllCtors;
109  KVString fClassPath;
110  Bool_t fInheritAllCtors;
111 
112 protected:
113 
114  void WritePreProc(std::ofstream&);
115  void WriteWhoWhen(std::ofstream&);
116  void SetWhoWhen();
117  void WriteClassDec(std::ofstream&);
118  void WriteClassHeader();
119  void WriteClassImp();
120  void WriteClassWithTemplateHeader();
121  void WriteClassWithTemplateImp();
122  Bool_t CheckTemplateFiles(const Char_t* base_class,
123  const Char_t* templateFile);
124 
125 
126  Ssiz_t FindNextUncommentedLine(TString&, Ssiz_t beg = 0);
127  void AddTObjectCopyMethod();
128  void AddCopyConstructor();
129 
130  void GenerateGettersAndSetters();
131  void AddAssignmentOperator();
132 public:
133 
139  class KVClassMember : public KVBase {
140 
141  protected:
142  KVString fComment;//informative comment for member variable, like this one :)
143  KVString fRealName;//the name of the member without the leading 'f'
144  Bool_t fIsFundamental=false;//true for fundamental types i.e. basic variables
145  Bool_t fIsPointer=false;//true for raw pointer member variables
146 
147  public:
149  {
150  SetAccess();
151  }
152  KVClassMember(const Char_t*, const Char_t*, const Char_t*, const Char_t* = "protected");
154  ROOT_COPY_ASSIGN_OP(KVClassMember)
155  virtual ~KVClassMember() {}
156  void Copy(TObject& obj) const override;
157 
158  void Print(Option_t* = "") const override;
159 
160  virtual void WriteDeclaration(KVString&);
161 
163  void SetAccess(const Char_t* acc = "public")
164  {
165  SetLabel(acc);
166  }
167 
169  const Char_t* GetAccess() const
170  {
171  return GetLabel();
172  }
173  Bool_t IsPublic() const
174  {
176  return !strcmp(GetAccess(), "public");
177  }
179  {
181  return !strcmp(GetAccess(), "protected");
182  }
184  {
186  return !strcmp(GetAccess(), "private");
187  }
189  {
191  return fIsFundamental;
192  }
194  {
196  return fIsPointer;
197  }
198 
200  void SetComment(const Char_t* c)
201  {
202  fComment = c;
203  }
204 
206  const Char_t* GetComment() const
207  {
208  return fComment;
209  }
210 
212  const Char_t* GetRealName() const
213  {
214  return fRealName;
215  }
216 
217  ClassDefOverride(KVClassMember, 1) //KVClassFactory helper class - description of class member variable
218  };
219 
221 
228  class KVClassMethod : public KVClassMember {
229 
230  protected:
231  virtual void write_method_body(KVString& decl);
232  KVNameValueList fFields;//fields of method declaration
233  Bool_t fVirtual=false;//kTRUE if method is 'virtual'
234  Bool_t fConst=false;//kTRUE if method is 'const'
235  int fNargs=0;//counts arguments
236  Bool_t fInline=false;//kTRUE if method body is to be written in header file
237  Bool_t fNoexcept=false;//kTRUE if method is 'noexcept'
238 
239  void write_doxygen_format_method_comments(KVString& decl);
240 
241  public:
242 
243  Int_t GetNargs() const
244  {
245  return fNargs;
246  }
247  void SetNargs(Int_t n)
248  {
249  fNargs = n;
250  }
251 
252  bool IsNoExcept() const
253  {
254  return fNoexcept;
255  }
256  void SetNoExcept(bool yes = true)
257  {
258  fNoexcept = yes;
259  }
260 
261  KVClassMethod(Bool_t Virtual = kFALSE, Bool_t Const = kFALSE, Bool_t Inline = kFALSE)
262  : KVClassMember(), fVirtual(Virtual), fConst(Const), fNargs(0), fInline(Inline)
263  {
264  }
266 
267  void Copy(TObject& obj) const override;
268 
269  void SetReturnType(const Char_t* type)
270  {
271  KVString s(type);
272  fFields.SetValue("ReturnType", s);
273  }
274  void SetClassName(const Char_t* name)
275  {
276  KVString s(name);
277  fFields.SetValue("ClassName", s);
278  }
279  void SetBaseClass(const Char_t* name)
280  {
281  KVString s(name);
282  fFields.SetValue("BaseClass", s);
283  }
284  void AddArgument(const Char_t* type, const Char_t* argname = "", const Char_t* defaultvalue = "")
285  {
286  KVString _type(type);
287  fFields.SetValue(Form("Arg_%d", ++fNargs), _type);
288  if (strcmp(defaultvalue, "")) {
289  KVString _s(defaultvalue);
290  fFields.SetValue(Form("Arg_%d_default", fNargs), _s);
291  }
292  if (strcmp(argname, "")) {
293  KVString _s(argname);
294  fFields.SetValue(Form("Arg_%d_name", fNargs), _s);
295  }
296  }
297  void AddArgument(Int_t i, const Char_t* type, const Char_t* argname = "", const Char_t* defaultvalue = "");
298  void SetMethodBody(const KVString& body)
299  {
300  fFields.SetValue("Body", body);
301  }
302  void SetMethodComment(const KVString& com)
303  {
304  fFields.SetValue("Comment", com);
305  }
307  {
308  if (fFields.HasParameter("ReturnType"))
309  return fFields.GetStringValue("ReturnType");
310  return "";
311  }
313  {
314  return fFields.GetStringValue("ClassName");
315  }
316 
317  void SetConst(Bool_t c = kTRUE)
318  {
319  fConst = c;
320  }
321  void SetVirtual(Bool_t c = kTRUE)
322  {
323  fVirtual = c;
324  }
325 
326  Bool_t IsConst() const
327  {
328  return fConst;
329  }
331  {
332  return fVirtual;
333  }
334  virtual Bool_t IsConstructor() const
335  {
336  return kFALSE;
337  }
338 
339  void Print(Option_t* = "") const override;
340 
341  void WriteDeclaration(KVString&) override;
342  void WriteImplementation(KVString& decl);
343 
344  void SetInline(Bool_t yes = kTRUE)
345  {
346  fInline = yes;
347  }
348  Bool_t IsInline() const
349  {
350  return fInline;
351  }
352  virtual Bool_t IsDestructor() const
353  {
354  return kFALSE;
355  }
357  {
359  return (!IsConstructor() && !IsDestructor());
360  }
361 
362  ClassDefOverride(KVClassMethod, 2) //KVClassFactory helper class - description of class method
363  };
364 
372  Bool_t fCopyCtor=false;//kTRUE if method is the copy constructor
373  Bool_t fMoveCtor=false;//true if move ctor
374  void write_method_body(KVString& decl) override;
375  KVClassFactory* fParentClass=nullptr;
376 
377  public:
379  : KVClassMethod(), fCopyCtor(kFALSE), fParentClass(ParentClass) {};
380 
381  Bool_t IsConstructor() const override
382  {
383  return kTRUE;
384  }
386  {
388  return IsCalled("default_ctor");
389  }
391  {
392  return fCopyCtor;
393  }
394  void SetCopyCtor(Bool_t c = kTRUE)
395  {
396  fCopyCtor = c;
397  }
399  {
400  return fMoveCtor;
401  }
402  void SetMoveCtor(Bool_t c = kTRUE)
403  {
404  fMoveCtor = c;
405  }
407  {
409  fFields.SetValue(Form("Arg_%d_baseclass", i), 1);
410  }
412  {
414  fFields.SetValue(Form("Arg_%d_memvar", i), memvar);
415  }
416 
417  ClassDefOverride(KVClassConstructor, 1) //KVClassFactory helper class - description of constructor
418  };
419 
420 protected:
421  void AddMemberInitialiserConstructor(KVClassConstructor* = nullptr);
422 
423 public:
431 
432  public:
435  {
436  SetName("destructor");
437  SetMethodComment("Destructor");
438  }
439  Bool_t IsDestructor() const override
440  {
441  return kTRUE;
442  }
443 
444  ClassDefOverride(KVClassDestructor, 1) //KVClassFactory helper class - description of destructor
445  };
446 
447  KVClassFactory();
448  KVClassFactory(const Char_t* classname,
449  const Char_t* classdesc,
450  const Char_t* base_class =
451  "", Bool_t withTemplate =
452  kFALSE, const Char_t* templateFile = "");
454 
455  void Copy(TObject& obj) const override;
456 
457  static void MakeClass(const Char_t* classname,
458  const Char_t* classdesc,
459  const Char_t* base_class =
460  "", Bool_t withTemplate =
461  kFALSE, const Char_t* templateFile = "");
462  void GenerateCode();
463 
464  KVClassMember* AddMember(const Char_t* name, const Char_t* type, const Char_t* comment, const Char_t* access = "protected");
465  KVClassMethod* AddMethod(const Char_t* name, const Char_t* return_type, const Char_t* access = "public",
466  Bool_t isVirtual = kFALSE, Bool_t isConst = kFALSE);
467  KVClassConstructor* AddConstructor(const Char_t* argument_type = "",
468  const Char_t* argument_name = "", const Char_t* default_value = "", const Char_t* access = "public");
469  void AddAllBaseConstructors();
470  void AddDefaultConstructor();
471  void AddDestructor(const TString& access = "public");
472  void AddMethod(const KVClassMethod& kvcm);
473  void AddMethodArgument(const Char_t* method_name, const Char_t* argument_type,
474  const Char_t* argument_name = "", const Char_t* default_value = "");
475  void AddMethodBody(const Char_t* method_name, const KVString& body);
476  void AddMethodComment(const Char_t* method_name, const KVString& comment);
477 
478  void AddHeaderIncludeFile(const Char_t* filename);
479  void AddImplIncludeFile(const Char_t* filename);
480 
481  const KVList* GetListOfMethods() const
482  {
483  return &fMethods;
484  }
485  const KVList* GetListOfMembers() const
486  {
487  return &fMembers;
488  }
489  KVClassMethod* GetMethod(const Char_t* name) const
490  {
491  return fMethods.get_object<KVClassMethod>(name);
492  }
494  {
495  return (KVClassConstructor*)GetMethod("default_ctor");
496  }
498  {
499  return (KVClassDestructor*)GetMethod("destructor");
500  }
501 
502  const Char_t* GetClassName() const
503  {
504  return fClassName.Data();
505  }
506  void SetClassName(const Char_t* n)
507  {
508  fClassName = n;
509  }
510  const Char_t* GetHeaderFileName() const
511  {
515  return fClassPath != "" ? Form("%s%s.h", fClassPath.Data(), fClassName.Data()) : Form("%s.h", fClassName.Data());
516  }
517  const Char_t* GetImpFileName() const
518  {
522  return fClassPath != "" ? Form("%s%s.cpp", fClassPath.Data(), fClassName.Data()) : Form("%s.cpp", fClassName.Data());
523  }
524  void SetClassDesc(const Char_t* d)
525  {
526  fClassDesc = d;
527  }
528  const Char_t* GetClassDesc() const
529  {
530  return fClassDesc.Data();
531  }
532  void SetBaseClass(const Char_t* b)
533  {
534  fBaseClassName = b;
535  fHasBaseClass = (fBaseClassName != "");
536  fBaseClassTObject = kFALSE;
537  if (fHasBaseClass) {
538  fBaseClass = TClass::GetClass(fBaseClassName);
539  fBaseClassTObject = (fBaseClass && fBaseClass->InheritsFrom("TObject"));
540  }
541  }
542  const Char_t* GetBaseClass() const
543  {
544  return fBaseClassName.Data();
545  }
547  {
548  return fBaseClassName.Contains(",");
549  }
551  {
552  return fWithTemplate;
553  }
555  {
556  return fBaseClassTObject;
557  }
558  void SetTemplate(Bool_t temp, const Char_t* temp_file);
559  const Char_t* GetTemplateBase() const
560  {
561  return fTemplateBase;
562  }
564  {
565  return fMembers.GetEntries();
566  }
567 
568  void Print(Option_t* opt = "") const override;
569 
570  void AddGetSetMethods(const KVNameValueList&);
571 
572  void InlineAllMethods(bool yes = true);
573  void InlineAllConstructors(bool yes = true);
574 
575  void SetOutputPath(const KVString& p)
576  {
579  fClassPath = p;
580  if (!fClassPath.EndsWith("/")) fClassPath.Append("/");
581  }
582  const Char_t* GetOutputPath() const
583  {
586  return fClassPath;
587  }
588 
590  {
592  fInheritAllCtors = yes;
593  }
594 
596  {
598 
599  TIter it(GetListOfMethods());
600  KVClassMethod* cm;
601  while( ( cm = (KVClassMethod*)it() ))
602  {
603  if(cm->IsVirtual())
604  return kTRUE;
605  }
606  return kFALSE;
607  }
608 
609  ClassDefOverride(KVClassFactory, 5) //Factory for generating KaliVeda skeleton classes
610 };
611 
612 #endif
int Int_t
#define d(i)
#define c(i)
bool Bool_t
int Ssiz_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
#define ClassDefOverride(name, id)
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
char * Form(const char *fmt,...)
Base class for KaliVeda framework.
Definition: KVBase.h:140
void SetLabel(const Char_t *lab)
Definition: KVBase.h:195
const Char_t * GetLabel() const
Definition: KVBase.h:199
virtual Bool_t IsCalled(const Char_t *name) const
Definition: KVBase.h:190
Constructor for a class generated with KVClassFactory.
KVClassConstructor(KVClassFactory *ParentClass)
void SetMemberVariableNameForArgument(Int_t i, const Char_t *memvar)
Bool_t IsConstructor() const override
Destructor for a class generated with KVClassFactory.
Bool_t IsDestructor() const override
Member variable in a class generated with KVClassFactory.
virtual void WriteDeclaration(KVString &)
void SetComment(const Char_t *c)
set comment for variable
void Print(Option_t *="") const override
print the KVClass member
void SetAccess(const Char_t *acc="public")
set access type : public, protected or private
void Copy(TObject &obj) const override
copy this to obj
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
Method in a class generated with KVClassFactory.
void SetMethodComment(const KVString &com)
void Copy(TObject &obj) const override
copy this to obj
void WriteDeclaration(KVString &) override
void SetNoExcept(bool yes=true)
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 Print(Option_t *="") const override
print the KVClass method
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 override
Print infos on object.
void Copy(TObject &obj) const override
Copy the state of this KVClassFactory to the one referenced by 'obj'.
void SetOutputPath(const KVString &p)
const Char_t * GetClassDesc() const
Bool_t HasVirtualMethods() const
Bool_t WithMultipleBaseClasses() const
void SetTemplate(Bool_t temp, const Char_t *temp_file)
void AddMethodComment(const Char_t *method_name, const KVString &comment)
Set the comments for method 'method_name' added to the class using AddMethod.
Bool_t IsBaseClassTObject() const
void GenerateCode()
Generate header and implementation file for currently-defined class.
KVClassMethod * GetMethod(const Char_t *name) const
const Char_t * GetTemplateBase() const
void SetClassName(const Char_t *n)
void SetInheritAllConstructors(Bool_t yes=kTRUE)
const Char_t * GetImpFileName() const
const KVList * GetListOfMethods() const
const KVList * GetListOfMembers() const
Bool_t WithTemplate() const
const Char_t * GetHeaderFileName() const
KVClassFactory()
Default ctor.
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 AddDestructor(const TString &access="public")
void AddImplIncludeFile(const Char_t *filename)
void AddDefaultConstructor()
const Char_t * GetClassName() const
void SetClassDesc(const Char_t *d)
Int_t GetNumberOfMemberVariables() const
void AddMethodBody(const Char_t *method_name, const KVString &body)
const Char_t * GetBaseClass() const
void InlineAllMethods(bool yes=true)
const Char_t * GetOutputPath() const
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)
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...
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")
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:22
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 TString &name) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t InheritsFrom(const char *cl) const override
virtual Int_t GetEntries() const
virtual void SetName(const char *name)
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
TString & Append(char c, Ssiz_t rep=1)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
const Int_t n