4 * This file contains the declaration of the wrapper class of the native MSG task type.
6 * Copyright 2006,2007 Martin Quinson, Malek Cherier
9 * This program is free software; you can redistribute
10 * it and/or modify it under the terms of the license
11 *(GNU LGPL) which comes with this package.
17 // Compilation C++ recquise
19 #error Object.hpp requires C++ compilation (use a .cxx suffix)
24 #include <ClassNotFoundException.hpp>
30 //////////////////////////////////////////////////////////////////////////////
33 // Returns the runtime class of the class_name object.
34 #define MSG_GET_CLASS(class_name) \
35 ((Class*)(&class_name::class##class_name))
37 // Declare the class class_name as dynamic
38 #define MSG_DECLARE_DYNAMIC(class_name) \
40 static Class class##class_name; \
41 virtual Class* getClass() const; \
42 static Object* createObject() \
44 // The runtime class implementation.
45 #define MSG_IMPLEMENT_CLASS(class_name, base_class_name, pfn,class_init) \
46 Class class_name::class##class_name = { \
47 #class_name, sizeof(class class_name),pfn, \
48 MSG_GET_CLASS(base_class_name), NULL,class_init}; \
49 Class* class_name::getClass() const \
50 { return MSG_GET_CLASS(class_name); } \
52 // CreateObject implementation.
53 #define MSG_IMPLEMENT_DYNAMIC(class_name, base_class_name) \
54 Object* class_name::createObject() \
55 { return (Object*)(new class_name); } \
56 DeclaringClass _declaringClass_##class_name(MSG_GET_CLASS(class_name)); \
57 MSG_IMPLEMENT_CLASS(class_name, base_class_name, \
58 class_name::createObject, &_declaringClass_##class_name) \
60 //////////////////////////////////////////////////////////////////////////////
61 // Classes declared in this file.
63 class Object; // The msg object.
65 //////////////////////////////////////////////////////////////////////////////
66 // Structures declared in this files.
68 struct Class; // used during the rtti operations
69 struct DeclaringClass; // used during the instances registration.
72 class DeclaringClasses;
74 //////////////////////////////////////////////////////////////////////////////
77 // Used during the registration.
78 void DeclareClass(Class* c);
80 //////////////////////////////////////////////////////////////////////////////
83 struct SIMGRIDX_EXPORT DeclaringClass
85 // Constructor : add the runtime classe in the list.
86 DeclaringClass(Class* c);
89 virtual ~DeclaringClass(void);
92 // the list of runtime classes.
93 static DeclaringClasses* declaringClasses;
96 #define MSG_DELCARING_CLASSES (*(DeclaringClass::declaringClasses))
99 struct SIMGRIDX_EXPORT Class
104 const char* name; // class name.
105 size_t typeSize; // type size.
106 Object* (*createObjectFn)(void); // pointer to the create object function.
107 Class* baseClass; // the runtime class of the runtime class.
108 Class* next; // the next runtime class in the list.
109 const DeclaringClass* declaringClass; // used during the registration of the class.
113 // Create the runtime class from its name.
114 static Class* fromName(const char* name)
115 throw (ClassNotFoundException);
117 // Create an object from the name of the its class.
118 static Object* createObject(const char* name);
120 // Create an instance of the class.
121 Object* createObject(void);
123 // Return true is the class is dervived from the base class baseClass.
124 bool isDerivedFrom(const Class* baseClass) const;
128 // Create an instance of the class.
129 inline Object* Class::createObject(void)
131 return (*createObjectFn)();
135 class SIMGRIDX_EXPORT Object
139 // Default constructor.
147 // Get the runtime class.
148 virtual Class* getClass(void) const;
150 // Returns true if the class is derived from the class baseClass. Otherwise
151 // the method returns false.
152 bool isDerivedFrom(const Class* baseClass) const;
154 // Returns true if the object is valid. Otherwise the method returns false.
155 virtual bool isValid(void) const;
157 // Returns true is the object is an instance of the class specified as parameter.
158 bool isInstanceOf(const char* className);
164 // The runtime class.
165 static const Class classObject;
168 // inline member functions of the class Object.
170 // Returns the runtime class of the object.
171 inline Class* Object::getClass(void) const
173 return MSG_GET_CLASS(Object);
176 // Returns true if the class is derived from the class pBaseClass. Otherwise
177 // the method returns false.
178 inline bool Object::isDerivedFrom(const Class* baseClass) const
180 return (getClass()->isDerivedFrom(baseClass));
183 // Returns true if the object is valid. Otherwise the method returns false.
184 inline bool Object::isValid(void) const
186 // returns always true.
191 class DeclaringClasses
199 virtual ~DeclaringClasses(){}
203 // Add the class at the head of the list.
204 void addHead(Class* c);
206 // Get the runtime class of the head of the list.
207 Class* getHead(void) const ;
209 // Remove the class from the list (don't destroy it).
210 bool remove(Class* c);
212 // Remove the head of the list.
213 Class* removeHead(void);
215 // Return true if the list is empty.
217 bool isEmpty(void) const;
219 // Remove of the elements of the list.
220 void removeAll(void);
222 // Get the number of classes in the list.
223 unsigned int getCount(void);
231 // The head of the list.
238 // The number of elements of the list.
242 typedef Object* ObjectPtr;
245 // Constructor (Add the class in the list).
246 inline DeclaringClass::DeclaringClass(Class* c)
248 if(!declaringClasses)
249 declaringClasses = new DeclaringClasses();
255 inline DeclaringClass::~DeclaringClass()
257 /*if(NULL != declaringClasses)
258 delete declaringClasses;
260 declaringClasses=NULL;*/
264 // Returns the number of elements of the list.
265 inline unsigned int DeclaringClasses::getCount()
270 // Returns the head of the list.
271 inline Class* DeclaringClasses::getHead() const
276 // Returns true if the list is empty. Otherwise this function
278 inline bool DeclaringClasses::isEmpty() const
283 // Removes all the elements of the list.
284 inline void DeclaringClasses::removeAll()
291 } // namespace SimGrid
294 using namespace SimGrid::Msg;
296 #define instanceOf(class_name) reinterpret_cast<class_name*>(Class::createObject(#class_name))
298 #endif // !MSG_OBJECT_H