Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static simgrid::instr::Type* rootType = nullptr; /* the root type */
12
13 simgrid::instr::Type* PJ_type_get_root()
14 {
15   return rootType;
16 }
17
18 namespace simgrid {
19 namespace instr {
20
21 Type::Type(std::string name, const char* key, std::string color, e_entity_types kind, Type* father)
22     : name_(name), color_(color), kind_(kind), father_(father)
23 {
24   if (name.empty() || key == nullptr) {
25     THROWF(tracing_error, 0, "can't create a new type with name or key equal nullptr");
26   }
27
28   id_ = std::to_string(instr_new_paje_id());
29
30   if (father != nullptr){
31     father->children_.insert({key, this});
32     XBT_DEBUG("new type %s, child of %s", name_.c_str(), father->getCname());
33   }
34 }
35
36 Type::~Type()
37 {
38   for (auto elm : values_) {
39     XBT_DEBUG("free value %s, child of %s", elm.second->getCname(), elm.second->father_->getCname());
40     delete elm.second;
41   }
42   for (auto elm : children_) {
43     delete elm.second;
44   }
45 }
46
47 Type* Type::byName(std::string name)
48 {
49   Type* ret = this->getChildOrNull(name);
50   if (ret == nullptr)
51     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
52   return ret;
53 }
54
55 Type* Type::getChildOrNull(std::string name)
56 {
57   xbt_assert(not name.empty(), "can't get type with a nullptr name");
58
59   Type* ret = nullptr;
60   for (auto elm : children_) {
61     if (elm.second->name_ == name) {
62       if (ret != nullptr) {
63         THROWF (tracing_error, 0, "there are two children types with the same name?");
64       } else {
65         ret = elm.second;
66       }
67     }
68   }
69   return ret;
70 }
71
72 Type* simgrid::instr::Type::containerNew(const char* name, Type* father)
73 {
74   if (name == nullptr){
75     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
76   }
77
78   simgrid::instr::Type* ret = new simgrid::instr::Type(name, name, "", TYPE_CONTAINER, father);
79   if (father == nullptr) {
80     rootType = ret;
81   } else {
82     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), father->getCname(),
83               father->getId());
84     LogContainerTypeDefinition(ret);
85   }
86   return ret;
87 }
88
89 Type* Type::eventNew(const char* name, Type* father)
90 {
91   if (name == nullptr){
92     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
93   }
94
95   Type* ret = new Type(name, name, "", TYPE_EVENT, father);
96   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), father->getCname(), father->getId());
97   LogDefineEventType(ret);
98   return ret;
99 }
100
101 Type* Type::variableNew(const char* name, std::string color, Type* father)
102 {
103   if (name == nullptr){
104     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
105   }
106
107   Type* ret = nullptr;
108
109   if (color.empty()) {
110     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
111     ret = new Type (name, name, white, TYPE_VARIABLE, father);
112   }else{
113     ret = new Type (name, name, color, TYPE_VARIABLE, father);
114   }
115   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), father->getCname(), father->getId());
116   LogVariableTypeDefinition (ret);
117   return ret;
118 }
119
120 Type* Type::linkNew(const char* name, Type* father, Type* source, Type* dest)
121 {
122   if (name == nullptr){
123     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
124   }
125
126   char key[INSTR_DEFAULT_STR_SIZE];
127   snprintf(key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->getId(), dest->getId());
128   Type* ret = new Type(name, key, "", TYPE_LINK, father);
129   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->getCname(), ret->getId(), father->getCname(),
130             father->getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
131   LogLinkTypeDefinition(ret, source, dest);
132   return ret;
133 }
134
135 Type* Type::stateNew(const char* name, Type* father)
136 {
137   if (name == nullptr){
138     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
139   }
140
141   Type* ret = new Type(name, name, "", TYPE_STATE, father);
142   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), father->getCname(), father->getId());
143   LogStateTypeDefinition(ret);
144   return ret;
145 }
146 }
147 }