GCpp general purpose C++ library  version 1.0
GClassDefine.hh
Go to the documentation of this file.
1 //======================================================================
2 /*! \file GClassDefine.hh
3  *
4  * File defining the macro to include in class definitions.
5  */
6 //======================================================================
7 
8 #ifndef G_CLASS_DEFINE_HH
9 #define G_CLASS_DEFINE_HH
10 
11 #include <string>
12 #include <ctype.h>
13 
14 //======================================================================
15 // Some template functions for Objects description
16 
17 /*! Template function returning the class name of a template class T.*/
18 template < class T > const char * G_StaticClassName ( )
19  {
20  // g++ returns a string with name length + name string
21  // the function just strips the leading digits
22  size_t pos = 0, i = 0;
23  const char * impl_name = typeid(T).name();
24  while ( i < strlen(impl_name) )
25  { if ( isdigit(impl_name[i]) ) { pos++; } ++i; }
26  return ( & impl_name[pos] );
27  //return ( impl_name );
28  }
29 
30 //======================================================================
31 
32 /*! Macro defining the real class name of an object (final derivation class).
33  */
34 #define G_CLASS_NAME_FCT \
35  inline virtual const char * ClassName ( ) const { return StaticClassName(); }
36 
37 /*! Macro defining the static class name of an object (as it is used).
38  */
39 #define G_STATIC_CLASS_NAME_FCT(T) \
40  inline static const char * StaticClassName ( ) \
41  { return ( G_StaticClassName<T>( ) ); }
42 
43 /*! Define a virtual copy function. The function calls the copy constructor,
44  * that must be defined.
45  */
46 #define G_CLONE_FCT(T) \
47  virtual T * Clone ( ) const { return ( new T(*this) ); }
48 
49 /*! Define an pure virtual copy function for abstract classes.*/
50 #define G_CLONE_ABSTRACT_FCT(T) \
51  virtual T * Clone ( ) const = 0;
52 
53 /*! Define the template function to check if an object is instance of a given
54  * class.*/
55 #define G_TEMP_IS_INSTANCE_OF \
56  template <class Q> bool IsInstanceOf ( ) const \
57  { return (dynamic_cast<const Q*>(this) != NULL); } \
58  template <class Q> bool IsInstanceOf ( ) \
59  { return (dynamic_cast<Q*>(this) != NULL); }
60 
61 /*! Macro defining basic object functions.
62  * \note
63  * - the copy constructor of the class must be defined.
64  */
65 #define GObject(T) \
66  public: \
67  G_TEMP_IS_INSTANCE_OF \
68  G_CLONE_FCT(T) \
69  G_CLASS_NAME_FCT \
70  G_STATIC_CLASS_NAME_FCT(T)
71 
72 /*! Macro defining basic object functions, to be used with abstract classes:
73  * the virtual copy function (Clone) is pure virtual.
74  * \note
75  * - the copy constructor of the class must be defined.
76  */
77 #define GObjectV(T) \
78  public: \
79  G_TEMP_IS_INSTANCE_OF \
80  G_CLONE_ABSTRACT_FCT(T) \
81  G_CLASS_NAME_FCT \
82  G_STATIC_CLASS_NAME_FCT(T)
83 
84 
85 //======================================================================
86 #endif
const char * G_StaticClassName()
Definition: GClassDefine.hh:18