Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
proper use of the HAVE_TRACING variable defined by Cmake through -Dtracing=on
[simgrid.git] / src / cxx / Object.cxx
1 /*
2  * Object.cxx
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  *
11  */
12  
13  /* SimGrid::Msg RTTI implementation.
14   */  
15
16 #include <Object.hpp>
17 #include <string.h>
18
19 #include <xbt/dynar.h>
20
21
22
23
24 DeclaringClasses* DeclaringClass::declaringClasses = NULL;
25
26 namespace SimGrid
27 {
28         namespace Msg
29         {
30
31                 // Generate static object constructor for class registration
32                 void DeclareClass(Class* c)
33                 {
34                         MSG_DELCARING_CLASSES.lock();
35                         MSG_DELCARING_CLASSES.addHead(c);
36                         MSG_DELCARING_CLASSES.unlock();
37                 }
38         } // namespace Msg
39 } // namespace SimGrid
40
41 //////////////////////////////////////////////////////////////////////////////
42 // Implémentation des fonctions membre de la classe Class
43
44 // true if the class is derived from base classe referenced 
45 // by pBaseClass
46 bool Class::isDerivedFrom(const Class* baseClass) const
47 {
48         const Class* cur = this;
49
50         while(cur)
51         {
52                 if(cur == baseClass)
53                         return true;
54
55                 cur = cur->baseClass;
56         }
57
58         return false;
59 }
60
61 // Dynamic name lookup and creation
62 Class* Class::fromName(const char* name)
63 throw (ClassNotFoundException)
64 {
65         Class* cur;
66
67         MSG_DELCARING_CLASSES.lock();
68         
69         for(cur = MSG_DELCARING_CLASSES.getHead(); cur; cur = cur->next)
70         {
71                 if(!strcmp(name,cur->name))
72                 {
73                         MSG_DELCARING_CLASSES.unlock();
74                         return cur;
75                 }
76         }
77
78         MSG_DELCARING_CLASSES.unlock();
79         throw ClassNotFoundException(name);
80 }
81
82
83 Object* Class::createObject(const char* name)
84 {
85         Class* c = fromName(name);
86         return c->createObject();
87 }
88
89
90 //////////////////////////////////////////////////////////////////////////////
91 // Object members implementation
92
93 // Special runtime-class structure for Object (no base class)
94 const struct Class Object::classObject =
95
96         "Object",               // name
97         sizeof(Object), // typeSize
98         NULL,                   // baseClass
99         NULL,                   // next
100         NULL                    // declaringClass
101 };
102
103
104 //////////////////////////////////////////////////////////////////////////////
105 // DeclaringClasses members implementation
106 //
107
108 DeclaringClasses::DeclaringClasses()
109 {
110         head = NULL;
111         count = 0;
112 }
113
114
115 // Ajoute une nouvelle classe en tête de liste
116 void DeclaringClasses::addHead(Class* c)
117 {
118         if(NULL != head)
119                 c->next = head;
120
121         head = c;
122     count++;
123 }
124
125 // Retourne la tête de liste et la retire de la liste
126 Class* DeclaringClasses::removeHead(void)
127 {
128         Class* c = NULL;
129
130         if(NULL != head)
131         {
132                 c = head;
133                 head = head->next;
134                 count--;
135         }
136
137         return c;
138 }
139
140 // Retire la classe pClasse de la liste, mais ne la détruit pas 
141 bool DeclaringClasses::remove(Class* c)
142 {
143         if(NULL == head)
144                 return false;
145
146         bool success = false;
147
148         if(head == c)
149         {
150                 head = c->next;
151         count--;
152                 success = true;
153         }
154         else
155         {
156                 Class* cur = head;
157
158                 while((NULL != cur) && (cur->next!= c))
159                         cur = cur->next;
160
161                 if(NULL != cur)
162                 {
163                         cur->next = c->next;
164             count--;
165                         success = true;
166                 }
167         }
168         
169         return success;
170 }
171
172 bool Object::isInstanceOf(const char* className)
173 {
174         Class* c = Class::fromName(className);
175
176         return this->getClass()->isDerivedFrom(c);
177 }
178                 
179