Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an unused (for the moment) module file
[simgrid.git] / src / cxx / Object.hpp
1 /*
2  * Object.hpp
3  *
4  * This file contains the declaration of the wrapper class of the native MSG task type.
5  *
6  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
7  * All right reserved. 
8  *
9  * This program is free software; you can redistribute 
10  * it and/or modify it under the terms of the license 
11  *(GNU LGPL) which comes with this package. 
12  *
13  */  
14 #ifndef MSG_OBJECT_H
15 #define MSG_OBJECT_H
16
17 // Compilation C++ recquise
18 #ifndef __cplusplus
19         #error Object.hpp requires C++ compilation (use a .cxx suffix)
20 #endif
21
22 #include <stdlib.h>
23
24 #include <ClassNotFoundException.hpp>
25
26 namespace SimGrid
27 {
28         namespace Msg
29         {
30                 //////////////////////////////////////////////////////////////////////////////
31                 // Macros
32                 
33                 // Returns the runtime class of the class_name object.
34                 #define MSG_GET_CLASS(class_name) \
35                                         ((Class*)(&class_name::class##class_name))
36                         
37                 // Declare the class class_name as dynamic
38                 #define MSG_DECLARE_DYNAMIC(class_name) \
39                 public: \
40                         static Class class##class_name; \
41                         virtual Class* getClass() const; \
42                         static Object*  createObject() \
43
44                 // The runtime class implementation.    
45                 #define MSG_IMPLEMENT_CLASS(class_name, base_class_name, pfn,class_init) \
46                         Class class_name::class##class_name = { \
47                                 #class_name, sizeof(class class_name),pfn, \
48                                         MSG_GET_CLASS(base_class_name), NULL,class_init}; \
49                         Class* class_name::getClass() const \
50                                 { return MSG_GET_CLASS(class_name); } \
51                 
52                 // CreateObject implementation. 
53                 #define MSG_IMPLEMENT_DYNAMIC(class_name, base_class_name) \
54                         Object* class_name::createObject() \
55                                 { return (Object*)(new class_name); } \
56                         DeclaringClass _declaringClass_##class_name(MSG_GET_CLASS(class_name)); \
57                         MSG_IMPLEMENT_CLASS(class_name, base_class_name, \
58                                 class_name::createObject, &_declaringClass_##class_name) \
59
60                 //////////////////////////////////////////////////////////////////////////////
61                 // Classes declared in this file.
62
63                 class Object;   // The msg object.
64
65                 //////////////////////////////////////////////////////////////////////////////
66                 // Structures declared in this files.
67
68                 struct Class;           // used during the rtti operations
69                 struct DeclaringClass;  // used during the instances registration.
70
71
72         class DeclaringClasses;
73
74                 //////////////////////////////////////////////////////////////////////////////
75                 // Global functions
76
77                 // Used during the registration.
78                 void DeclareClass(Class* c);
79
80                 //////////////////////////////////////////////////////////////////////////////
81                 // DeclaringClass
82
83                 struct SIMGRIDX_EXPORT DeclaringClass
84                 {
85                         // Constructor : add the runtime classe in the list.
86                         DeclaringClass(Class* c);
87                         
88                         // Destructor
89                         virtual ~DeclaringClass(void);
90
91                 // Attributes :
92                     // the list of runtime classes.
93                     static DeclaringClasses* declaringClasses;
94                 };
95
96         #define MSG_DELCARING_CLASSES (*(DeclaringClass::declaringClasses))
97                 
98                 
99                 struct SIMGRIDX_EXPORT Class
100                 {
101                 
102                 // Attributes
103         
104                         const char* name;                                               // class name.
105                         size_t typeSize;                                                // type size.                   
106                         Object* (*createObjectFn)(void);                // pointer to the create object function. 
107                         Class* baseClass;                                               // the runtime class of the runtime class.
108                         Class* next;                                                    // the next runtime class in the list.
109                         const DeclaringClass* declaringClass;   // used during the registration of the class.
110                 
111                 // Operations
112                 
113                         // Create the runtime class from its name.
114                         static Class* fromName(const char* name)
115                         throw (ClassNotFoundException);
116                         
117                         // Create an object from the name of the its class.
118                         static Object* createObject(const char* name);
119                         
120                         // Create an instance of the class.
121                         Object* createObject(void);
122                         
123                         // Return true is the class is dervived from the base class baseClass.
124                         bool isDerivedFrom(const Class* baseClass) const;
125
126                 };
127
128         // Create an instance of the class.
129         inline Object* Class::createObject(void)
130         {
131             return (*createObjectFn)();
132         }
133         
134             
135                 class SIMGRIDX_EXPORT Object
136                 {
137                 public:
138                 
139                         // Default constructor.
140                         Object(){}
141                         
142                         // Destructor.
143                         virtual ~Object(){}
144                         
145                         // Operations.
146                 
147                         // Get the runtime class.
148                         virtual Class* getClass(void) const;
149                         
150                         // Returns true if the class is derived from the class baseClass. Otherwise
151                         // the method returns false.
152                         bool isDerivedFrom(const Class* baseClass) const;
153                         
154                         // Returns true if the object is valid. Otherwise the method returns false.
155                         virtual bool isValid(void) const;
156
157                         // Returns true is the object is an instance of the class specified as parameter.
158                         bool isInstanceOf(const char* className);
159                         
160                         // Operators.
161
162                         // Attributes.
163                         
164                         // The runtime class.
165                         static const Class classObject;
166                 };
167
168                 // inline member functions of the class Object.
169                 
170                 // Returns the runtime class of the object.
171                 inline Class* Object::getClass(void) const
172                 {
173                         return MSG_GET_CLASS(Object);
174                 }
175
176         // Returns true if the class is derived from the class pBaseClass. Otherwise
177                 // the method returns false.
178         inline bool Object::isDerivedFrom(const Class* baseClass) const
179         {
180             return (getClass()->isDerivedFrom(baseClass));
181         }
182                 
183                 // Returns true if the object is valid. Otherwise the method returns false.
184                 inline bool Object::isValid(void) const
185                 {
186                         // returns always true.
187                         return true;    
188                 }
189
190         
191                 class DeclaringClasses 
192                 {
193                 public:
194                 
195                         // Constructor.
196                         DeclaringClasses();
197
198                         // Destructor.
199                         virtual ~DeclaringClasses(){}
200                 
201                 // Operations.
202                 
203                         // Add the class at the head of the list.
204                         void addHead(Class* c);
205                         
206                         // Get the runtime class of the head of the list.
207                         Class* getHead(void) const ;
208                         
209                         // Remove the class from the list (don't destroy it).
210                         bool remove(Class* c);
211             
212             // Remove the head of the list.
213                         Class* removeHead(void);
214                         
215                         // Return true if the list is empty.
216                         
217                         bool isEmpty(void) const;
218                         
219                         // Remove of the elements of the list.
220                         void removeAll(void);
221                         
222                         // Get the number of classes in the list.
223                     unsigned int getCount(void);
224                     
225                     void lock(void){}
226                     
227                     void unlock(void){}
228                 
229                         //Attributes
230                 
231                         // The head of the list.
232                         Class* head;
233
234                 private:
235                 
236                 // Attributes
237                 
238                         // The number of elements of the list.
239                         unsigned int count;
240                 };
241
242                 typedef Object* ObjectPtr;
243
244
245         // Constructor (Add the class in the list).
246         inline DeclaringClass::DeclaringClass(Class* c)
247         {
248                 if(!declaringClasses)
249                         declaringClasses = new DeclaringClasses();
250
251                 DeclareClass(c);
252         }
253
254         // Destructor.
255         inline DeclaringClass::~DeclaringClass()
256         {
257                 /*if(NULL != declaringClasses)
258                         delete declaringClasses;
259
260                 declaringClasses=NULL;*/
261
262         }
263
264                 // Returns the number of elements of the list.
265                 inline unsigned int DeclaringClasses::getCount()
266                 {
267                         return count;
268                 }
269                 
270                 // Returns the head of the list.
271                 inline Class* DeclaringClasses::getHead() const
272                 {
273                         return head;
274                 }
275                 
276                 // Returns true if the list is empty. Otherwise this function
277                 // returns false.
278                 inline bool DeclaringClasses::isEmpty() const
279                 {
280                         return(!head);
281                 }
282                 
283                 // Removes all the elements of the list.
284                 inline void DeclaringClasses::removeAll()
285                 {
286                         head = 0;
287                     count=0;
288                 }
289                         
290         } // namespace Msg     
291 } // namespace SimGrid
292
293
294 using namespace SimGrid::Msg;
295
296 #define instanceOf(class_name) reinterpret_cast<class_name*>(Class::createObject(#class_name))
297
298 #endif // !MSG_OBJECT_H
299