Logo AND Algorithmique Numérique Distribuée

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