Links.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @file common/Links.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_LINKS
  26. #define COMMON_LINKS
  27. #include <artis-star/common/Node.hpp>
  28. #include <artis-star/common/utils/String.hpp>
  29. #include <map>
  30. #include <sstream>
  31. namespace artis {
  32. namespace common {
  33. template<class Time>
  34. class Node;
  35. template<class Time>
  36. class Links : public std::multimap<Node<Time>, Node<Time> >
  37. {
  38. typedef std::multimap<Node<Time>, Node<Time> > type;
  39. public:
  40. typedef std::pair<
  41. typename Links<Time>::const_iterator,
  42. typename Links<Time>::const_iterator
  43. > Result;
  44. Links()
  45. {}
  46. virtual ~Links()
  47. {}
  48. void add(Model<Time> *out_model, unsigned int out_port_index,
  49. Model<Time> *in_model, unsigned int in_port_index)
  50. {
  51. type::insert(std::pair<Node<Time>, Node<Time> >(
  52. Node<Time>(out_model, out_port_index),
  53. Node<Time>(in_model, in_port_index)));
  54. }
  55. bool exist(Model<Time> *out_model,
  56. unsigned int out_port_index,
  57. Model<Time> *in_model,
  58. unsigned int in_port_index) const
  59. {
  60. std::pair<typename Links<Time>::const_iterator,
  61. typename Links<Time>::const_iterator> it =
  62. type::equal_range(Node<Time>(out_model, out_port_index));
  63. typename Links<Time>::const_iterator it2 = it.first;
  64. bool found = false;
  65. while (not found and it2 != it.second) {
  66. found = it2->second == Node<Time>(
  67. in_model, in_port_index);
  68. ++it2;
  69. }
  70. return found;
  71. }
  72. typename Links<Time>::const_iterator
  73. find(Model<Time> *out_model, unsigned int out_port_index,
  74. Model<Time> *in_model, unsigned int in_port_index) const
  75. {
  76. std::pair<typename Links<Time>::const_iterator,
  77. typename Links<Time>::const_iterator> it =
  78. type::equal_range(Node<Time>(out_model, out_port_index));
  79. typename Links<Time>::const_iterator it2 = it.first;
  80. bool found = false;
  81. while (not found and it2 != it.second) {
  82. found = it2->second == Node<Time>(
  83. in_model, in_port_index);
  84. if (not found) {
  85. ++it2;
  86. }
  87. }
  88. return it2;
  89. }
  90. Links::Result find(Model<Time> *out_model, unsigned int out_port_index) const
  91. {
  92. return type::equal_range(common::Node<Time>(out_model, out_port_index));
  93. }
  94. void remove(common::Model<Time> *src_model, unsigned int src_port_index,
  95. common::Model<Time> *dst_model, unsigned int dst_port_index)
  96. {
  97. typename Links<Time>::const_iterator result = find(src_model, src_port_index,
  98. dst_model, dst_port_index);
  99. this->erase(result);
  100. }
  101. void remove_links(common::Model<Time> *model)
  102. {
  103. typename Links<Time>::iterator it = this->begin();
  104. while (it != this->end()) {
  105. if (it->first.get_model() == model or it->second.get_model() == model) {
  106. this->erase(it);
  107. it = this->begin();
  108. } else {
  109. ++it;
  110. }
  111. }
  112. }
  113. std::string to_string(int level = 0) const
  114. {
  115. std::stringstream ss;
  116. ss << common::String::make_spaces(level * 2) << "Links:" << std::endl;
  117. for (typename Links<Time>::const_iterator it = Links<Time>::begin();
  118. it != Links<Time>::end(); ++it) {
  119. ss << common::String::make_spaces((level + 1) * 2)
  120. << it->first.get_model()->get_name() << "::"
  121. << it->first.get_model()->get_out_port_name(
  122. it->first.get_port_index())
  123. << " -> "
  124. << it->second.get_model()->get_name() << "::"
  125. << it->second.get_model()->get_out_port_name(
  126. it->second.get_port_index()) << std::endl;
  127. }
  128. return ss.str();
  129. }
  130. };
  131. }
  132. } // namespace artis common
  133. #endif