Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use s4u API in example.
[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 namespace simgrid {
14 namespace instr {
15
16 Type::Type(std::string name, std::string alias, std::string color, e_entity_types kind, Type* father)
17     : name_(name), color_(color), kind_(kind), father_(father)
18 {
19   if (name.empty() || alias.empty()) {
20     THROWF(tracing_error, 0, "can't create a new type with no name or alias");
21   }
22
23   id_ = std::to_string(instr_new_paje_id());
24
25   if (father != nullptr){
26     father->children_.insert({alias, this});
27     XBT_DEBUG("new type %s, child of %s", name_.c_str(), father->getCname());
28   }
29 }
30
31 Type::~Type()
32 {
33   for (auto elm : values_)
34     delete elm.second;
35   for (auto elm : children_)
36     delete elm.second;
37 }
38
39 Type* Type::byName(std::string name)
40 {
41   Type* ret = nullptr;
42   for (auto elm : children_) {
43     if (elm.second->name_ == name) {
44       if (ret != nullptr) {
45         THROWF (tracing_error, 0, "there are two children types with the same name?");
46       } else {
47         ret = elm.second;
48       }
49     }
50   }
51   if (ret == nullptr)
52     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
53   return ret;
54 }
55
56 Type* Type::createRootType()
57 {
58   simgrid::instr::Type* ret = new simgrid::instr::Type("0", "0", "", TYPE_CONTAINER, nullptr);
59   rootType                  = ret;
60   return ret;
61 }
62
63 Type* Type::getRootType()
64 {
65   return rootType;
66 }
67
68 Type* Type::getOrCreateContainerType(std::string name)
69 {
70   auto it = children_.find(name);
71   if (it == children_.end()) {
72     Type* ret = new simgrid::instr::Type(name, name, "", TYPE_CONTAINER, this);
73     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
74     ret->logContainerTypeDefinition();
75     return ret;
76   } else
77     return it->second;
78 }
79
80 Type* Type::getOrCreateEventType(std::string name)
81 {
82   auto it = children_.find(name);
83   if (it == children_.end()) {
84     Type* ret = new Type(name, name, "", TYPE_EVENT, this);
85     XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
86     ret->logDefineEventType();
87     return ret;
88   } else {
89     return it->second;
90   }
91 }
92
93 Type* Type::getOrCreateVariableType(std::string name, std::string color)
94 {
95   auto it = children_.find(name);
96   if (it == children_.end()) {
97     Type* ret = new Type(name, name, color.empty() ? "1 1 1" : color, TYPE_VARIABLE, this);
98
99     XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
100     ret->logVariableTypeDefinition();
101
102     return ret;
103   } else
104     return it->second;
105 }
106
107 Type* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
108 {
109   std::string alias = name + "-" + source->id_ + "-" + dest->id_;
110   auto it           = children_.find(alias);
111   if (it == children_.end()) {
112     Type* ret = new Type(name, alias, "", TYPE_LINK, this);
113     XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->getCname(), ret->getId(), getCname(), getId(),
114               source->getCname(), source->getId(), dest->getCname(), dest->getId());
115     ret->logLinkTypeDefinition(source, dest);
116     return ret;
117   } else
118     return it->second;
119 }
120
121 Type* Type::getOrCreateStateType(std::string name)
122 {
123   auto it = children_.find(name);
124   if (it == children_.end()) {
125     Type* ret = new Type(name, name, "", TYPE_STATE, this);
126     XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
127     ret->logStateTypeDefinition();
128     return ret;
129   } else
130     return it->second;
131 }
132 }
133 }