Logo AND Algorithmique Numérique Distribuée

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