Logo AND Algorithmique Numérique Distribuée

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