Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some new classes of CPP version of Msg
[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 #include <ClassNotFoundException.hpp>\r
14 \r
15 namespace msg\r
16 {\r
17                 //////////////////////////////////////////////////////////////////////////////\r
18                 // Macros\r
19                 \r
20                 // Returns the runtime class of the class_name object.\r
21                 #define MSG_GET_CLASS(class_name) \\r
22                                         ((Class*)(&class_name::class##class_name))\r
23                         \r
24                 // Declare the class class_name as dynamic\r
25                 #define MSG_DECLARE_DYNAMIC(class_name) \\r
26                 public: \\r
27                         static const Class class##class_name; \\r
28                         virtual Class* getClass() const; \\r
29                         static Object*  createObject(); \\r
30 \r
31                 // The runtime class implementation.    \r
32                 #define MSG_IMPLEMENT_CLASS(class_name, base_class_name, pfn,class_init) \\r
33                         const Class class_name::class##class_name = { \\r
34                                 #class_name, sizeof(class class_name),pfn, \\r
35                                         MSG_GET_CLASS(base_class_name), NULL,class_init}; \\r
36                         Class* class_name::getClass() const \\r
37                                 { return MSG_GET_CLASS(class_name); } \\r
38                 \r
39                 // CreateObject implementation. \r
40                 #define MSG_IMPLEMENT_DYNAMIC(class_name, base_class_name) \\r
41                         Object* class_name::createObject() \\r
42                                 { return (Object*)(new class_name); } \\r
43                         DeclaringClass _declaringClass_##class_name(MSG_GET_CLASS(class_name)); \\r
44                         MSG_IMPLEMENT_CLASS(class_name, base_class_name, \\r
45                                 class_name::createObject, &_declaringClass_##class_name) \\r
46 \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 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 Class\r
88                 {\r
89                 \r
90                 // Attributes\r
91         \r
92                         const char* name;                                                       // class name.\r
93                         int 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 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                         // Operators.\r
146 \r
147                         // Attributes.\r
148                         \r
149                         // The runtime class.\r
150                         static const Class classObject;\r
151                 };\r
152 \r
153                 // inline member functions of the class Object.\r
154                 \r
155                 // Returns the runtime class of the object.\r
156                 inline Class* Object::getClass(void) const\r
157                 {\r
158                         return MSG_GET_CLASS(Object);\r
159                 }\r
160 \r
161         // Returns true if the class is derived from the class pBaseClass. Otherwise\r
162                 // the method returns false.\r
163         inline bool Object::isDerivedFrom(const Class* baseClass) const\r
164         {\r
165             return (getClass()->isDerivedFrom(baseClass));\r
166         }\r
167                 \r
168                 // Returns true if the object is valid. Otherwise the method returns false.\r
169                 inline bool Object::isValid(void) const\r
170                 {\r
171                         // returns always true.\r
172                         return true;    \r
173                 }\r
174         \r
175                 class DeclaringClasses \r
176                 {\r
177                 public:\r
178                 \r
179                         // Constructor.\r
180                         DeclaringClasses();\r
181 \r
182                         // Destructor.\r
183                         virtual ~DeclaringClasses(){}\r
184                 \r
185                 // Operations.\r
186                 \r
187                         // Add the class at the head of the list.\r
188                         void addHead(Class* c);\r
189                         \r
190                         // Get the runtime class of the head of the list.\r
191                         Class* getHead(void) const ;\r
192                         \r
193                         // Remove the class from the list (don't destroy it).\r
194                         bool remove(Class* c);\r
195             \r
196             // Remove the head of the list.\r
197                         Class* removeHead(void);\r
198                         \r
199                         // Return true if the list is empty.\r
200                         \r
201                         bool isEmpty(void) const;\r
202                         \r
203                         // Remove of the elements of the list.\r
204                         void removeAll(void);\r
205                         \r
206                         // Get the number of classes in the list.\r
207                     unsigned int getCount(void);\r
208                     \r
209                     void lock(void){}\r
210                     \r
211                     void unlock(void){}\r
212                 \r
213                         //Attributes\r
214                 \r
215                         // The head of the list.\r
216                         Class* head;\r
217 \r
218                 private:\r
219                 \r
220                 // Attributes\r
221                 \r
222                         // The number of elements of the list.\r
223                         unsigned int count;\r
224                 };\r
225 \r
226 \r
227         // Constructor (Add the class in the list).\r
228         inline DeclaringClass::DeclaringClass(Class* c)\r
229         {\r
230                 if(!declaringClasses)\r
231                         declaringClasses = new DeclaringClasses();\r
232 \r
233                 DeclareClass(c);\r
234         }\r
235 \r
236         // Destructor.\r
237         inline DeclaringClass::~DeclaringClass()\r
238         {\r
239                 /*if(NULL != declaringClasses)\r
240                         delete declaringClasses;\r
241 \r
242                 declaringClasses=NULL;*/\r
243 \r
244         }\r
245 \r
246                 // Returns the number of elements of the list.\r
247                 inline unsigned int DeclaringClasses::getCount()\r
248                 {\r
249                         return count;\r
250                 }\r
251                 \r
252                 // Returns the head of the list.\r
253                 inline Class* DeclaringClasses::getHead() const\r
254                 {\r
255                         return head;\r
256                 }\r
257                 \r
258                 // Returns true if the list is empty. Otherwise this function\r
259                 // returns false.\r
260                 inline bool DeclaringClasses::isEmpty() const\r
261                 {\r
262                         return(!head);\r
263                 }\r
264                 \r
265                 // Removes all the elements of the list.\r
266                 inline void DeclaringClasses::removeAll()\r
267                 {\r
268                         head = 0;\r
269                     count=0;\r
270                 }\r
271                         \r
272        \r
273\r
274 \r
275 using namespace msg;\r
276 \r
277 #endif // !MSG_OBJECT_H\r
278 \r