4 * This file contains the declaration of the wrapper class of the native MSG task type.
\r
6 * Copyright 2006,2007 Martin Quinson, Malek Cherier
\r
7 * All right reserved.
\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
14 #ifndef MSG_OBJECT_H
\r
15 #define MSG_OBJECT_H
\r
17 // Compilation C++ recquise
\r
19 #error Object.hpp requires C++ compilation (use a .cxx suffix)
\r
24 #include <ClassNotFoundException.hpp>
\r
30 //////////////////////////////////////////////////////////////////////////////
\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
37 // Declare the class class_name as dynamic
\r
38 #define MSG_DECLARE_DYNAMIC(class_name) \
\r
40 static Class class##class_name; \
\r
41 virtual Class* getClass() const; \
\r
42 static Object* createObject() \
\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
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
60 //////////////////////////////////////////////////////////////////////////////
\r
61 // Classes declared in this file.
\r
63 class Object; // The msg object.
\r
65 //////////////////////////////////////////////////////////////////////////////
\r
66 // Structures declared in this files.
\r
68 struct Class; // used during the rtti operations
\r
69 struct DeclaringClass; // used during the instances registration.
\r
72 class DeclaringClasses;
\r
74 //////////////////////////////////////////////////////////////////////////////
\r
77 // Used during the registration.
\r
78 void DeclareClass(Class* c);
\r
80 //////////////////////////////////////////////////////////////////////////////
\r
83 struct SIMGRIDX_EXPORT DeclaringClass
\r
85 // Constructor : add the runtime classe in the list.
\r
86 DeclaringClass(Class* c);
\r
89 virtual ~DeclaringClass(void);
\r
92 // the list of runtime classes.
\r
93 static DeclaringClasses* declaringClasses;
\r
96 #define MSG_DELCARING_CLASSES (*(DeclaringClass::declaringClasses))
\r
99 struct SIMGRIDX_EXPORT Class
\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
113 // Create the runtime class from its name.
\r
114 static Class* fromName(const char* name)
\r
115 throw (ClassNotFoundException);
\r
117 // Create an object from the name of the its class.
\r
118 static Object* createObject(const char* name);
\r
120 // Create an instance of the class.
\r
121 Object* createObject(void);
\r
123 // Return true is the class is dervived from the base class baseClass.
\r
124 bool isDerivedFrom(const Class* baseClass) const;
\r
128 // Create an instance of the class.
\r
129 inline Object* Class::createObject(void)
\r
131 return (*createObjectFn)();
\r
135 class SIMGRIDX_EXPORT Object
\r
139 // Default constructor.
\r
143 virtual ~Object(){}
\r
147 // Get the runtime class.
\r
148 virtual Class* getClass(void) const;
\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
154 // Returns true if the object is valid. Otherwise the method returns false.
\r
155 virtual bool isValid(void) const;
\r
157 // Returns true is the object is an instance of the class specified as parameter.
\r
158 bool isInstanceOf(const char* className);
\r
164 // The runtime class.
\r
165 static const Class classObject;
\r
168 // inline member functions of the class Object.
\r
170 // Returns the runtime class of the object.
\r
171 inline Class* Object::getClass(void) const
\r
173 return MSG_GET_CLASS(Object);
\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
180 return (getClass()->isDerivedFrom(baseClass));
\r
183 // Returns true if the object is valid. Otherwise the method returns false.
\r
184 inline bool Object::isValid(void) const
\r
186 // returns always true.
\r
191 class DeclaringClasses
\r
196 DeclaringClasses();
\r
199 virtual ~DeclaringClasses(){}
\r
203 // Add the class at the head of the list.
\r
204 void addHead(Class* c);
\r
206 // Get the runtime class of the head of the list.
\r
207 Class* getHead(void) const ;
\r
209 // Remove the class from the list (don't destroy it).
\r
210 bool remove(Class* c);
\r
212 // Remove the head of the list.
\r
213 Class* removeHead(void);
\r
215 // Return true if the list is empty.
\r
217 bool isEmpty(void) const;
\r
219 // Remove of the elements of the list.
\r
220 void removeAll(void);
\r
222 // Get the number of classes in the list.
\r
223 unsigned int getCount(void);
\r
227 void unlock(void){}
\r
231 // The head of the list.
\r
238 // The number of elements of the list.
\r
239 unsigned int count;
\r
242 typedef Object* ObjectPtr;
\r
245 // Constructor (Add the class in the list).
\r
246 inline DeclaringClass::DeclaringClass(Class* c)
\r
248 if(!declaringClasses)
\r
249 declaringClasses = new DeclaringClasses();
\r
255 inline DeclaringClass::~DeclaringClass()
\r
257 /*if(NULL != declaringClasses)
\r
258 delete declaringClasses;
\r
260 declaringClasses=NULL;*/
\r
264 // Returns the number of elements of the list.
\r
265 inline unsigned int DeclaringClasses::getCount()
\r
270 // Returns the head of the list.
\r
271 inline Class* DeclaringClasses::getHead() const
\r
276 // Returns true if the list is empty. Otherwise this function
\r
278 inline bool DeclaringClasses::isEmpty() const
\r
283 // Removes all the elements of the list.
\r
284 inline void DeclaringClasses::removeAll()
\r
290 } // namespace Msg
\r
291 } // namespace SimGrid
\r
294 using namespace SimGrid::Msg;
\r
296 #define instanceOf(class_name) reinterpret_cast<class_name*>(Class::createObject(#class_name))
\r
298 #endif // !MSG_OBJECT_H
\r