Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
normalize s_type class part 2
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/instr/instr_private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static type_t rootType = nullptr;        /* the root type */
12
13 void PJ_type_release ()
14 {
15   rootType = nullptr;
16 }
17
18 type_t PJ_type_get_root ()
19 {
20   return rootType;
21 }
22
23 Type::Type (const char *typeNameBuff, const char *key, const char *color, e_entity_types kind, type_t father)
24 {
25   if (typeNameBuff == nullptr || key == nullptr){
26     THROWF(tracing_error, 0, "can't create a new type with name or key equal nullptr");
27   }
28
29   this->name = xbt_strdup (typeNameBuff);
30   this->father = father;
31   this->kind = kind;
32   this->children = xbt_dict_new_homogeneous(nullptr);
33   this->values = xbt_dict_new_homogeneous(nullptr);
34   this->color = xbt_strdup (color);
35
36   char str_id[INSTR_DEFAULT_STR_SIZE];
37   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", instr_new_paje_id());
38   this->id = xbt_strdup (str_id);
39
40   if (father != nullptr){
41     xbt_dict_set (father->children, key, this, nullptr);
42     XBT_DEBUG("new type %s, child of %s", typeNameBuff, father->name);
43   }
44 }
45
46 void PJ_type_free (type_t type)
47 {
48   value* val;
49   char *value_name;
50   xbt_dict_cursor_t cursor = nullptr;
51   xbt_dict_foreach (type->values, cursor, value_name, val) {
52     XBT_DEBUG("free value %s, child of %s", val->name, val->father->name);
53     xbt_free(val);
54   }
55   xbt_dict_free (&type->values);
56   xbt_free (type->name);
57   xbt_free (type->id);
58   xbt_free (type->color);
59   xbt_dict_free (&type->children);
60   xbt_free (type);
61   type = nullptr;
62 }
63
64 void recursiveDestroyType (type_t type)
65 {
66   XBT_DEBUG("recursiveDestroyType %s", type->name);
67   xbt_dict_cursor_t cursor = nullptr;
68   type_t child;
69   char *child_name;
70   xbt_dict_foreach(type->children, cursor, child_name, child) {
71     recursiveDestroyType (child);
72   }
73   PJ_type_free(type);
74 }
75
76 type_t PJ_type_get (const char *name, type_t father)
77 {
78   type_t ret = Type::getOrNull (name, father);
79   if (ret == nullptr){
80     THROWF (tracing_error, 2, "type with name (%s) not found in father type (%s)", name, father->name);
81   }
82   return ret;
83 }
84
85 type_t Type::getOrNull (const char *name, type_t father)
86 {
87   if (name == nullptr || father == nullptr){
88     THROWF (tracing_error, 0, "can't get type with a nullptr name or from a nullptr father");
89   }
90
91   type_t ret = nullptr;
92   type_t child;
93   char *child_name;
94   xbt_dict_cursor_t cursor = nullptr;
95   xbt_dict_foreach(father->children, cursor, child_name, child) {
96     if (strcmp (child->name, name) == 0){
97       if (ret != nullptr){
98         THROWF (tracing_error, 0, "there are two children types with the same name?");
99       }else{
100         ret = child;
101       }
102     }
103   }
104   return ret;
105 }
106
107 type_t Type::containerNew (const char *name, type_t father)
108 {
109   if (name == nullptr){
110     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
111   }
112
113   type_t ret = new Type (name, name, nullptr, TYPE_CONTAINER, father);
114   if (father == nullptr) {
115     rootType = ret;
116   } else {
117     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
118     DefineContainerEvent(ret);
119   }
120   return ret;
121 }
122
123 type_t Type::eventNew (const char *name, type_t father)
124 {
125   if (name == nullptr){
126     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
127   }
128
129   type_t ret = new Type (name, name, nullptr, TYPE_EVENT, father);
130   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
131   LogDefineEventType(ret);
132   return ret;
133 }
134
135 type_t Type::variableNew (const char *name, const char *color, type_t father)
136 {
137   if (name == nullptr){
138     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
139   }
140
141   type_t ret = nullptr;
142
143   if (not color) {
144     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
145     ret = new Type (name, name, white, TYPE_VARIABLE, father);
146   }else{
147     ret = new Type (name, name, color, TYPE_VARIABLE, father);
148   }
149   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
150   LogVariableTypeDefinition (ret);
151   return ret;
152 }
153
154 type_t Type::linkNew (const char *name, type_t father, type_t source, type_t dest)
155 {
156   if (name == nullptr){
157     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
158   }
159
160   type_t ret = nullptr;
161
162   char key[INSTR_DEFAULT_STR_SIZE];
163   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id, dest->id);
164   ret = new Type (name, key, nullptr, TYPE_LINK, father);
165   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id,
166             source->name, source->id, dest->name, dest->id);
167   LogLinkTypeDefinition(ret, source, dest);
168   return ret;
169 }
170
171 type_t Type::stateNew (const char *name, type_t father)
172 {
173   if (name == nullptr){
174     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
175   }
176
177   type_t ret = nullptr;
178
179   ret = new Type (name, name, nullptr, TYPE_STATE, father);
180   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
181   LogStateTypeDefinition(ret);
182   return ret;
183 }