Links.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file 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 1
  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. typedef std::multimap<Node<Time>, Node<Time> > type;
  38. public:
  39. typedef std::pair<
  40. typename Links<Time>::const_iterator,
  41. typename Links<Time>::const_iterator
  42. > Result;
  43. Links() { }
  44. virtual ~Links() { }
  45. void add(Model<Time>* out_model, unsigned int out_port_index,
  46. Model<Time>* in_model, unsigned int in_port_index)
  47. {
  48. type::insert(std::pair<Node<Time>, Node<Time> >(
  49. Node<Time>(out_model, out_port_index),
  50. Node<Time>(in_model, in_port_index)));
  51. }
  52. bool exist(Model<Time>* out_model,
  53. unsigned int out_port_index,
  54. Model<Time>* in_model,
  55. unsigned int in_port_index) const
  56. {
  57. std::pair<typename Links<Time>::const_iterator,
  58. typename Links<Time>::const_iterator> it =
  59. type::equal_range(Node<Time>(out_model, out_port_index));
  60. typename Links<Time>::const_iterator it2 = it.first;
  61. bool found = false;
  62. while (not found and it2 != it.second) {
  63. found = it2->second == Node<Time>(
  64. in_model, in_port_index);
  65. ++it2;
  66. }
  67. return found;
  68. }
  69. typename Links<Time>::const_iterator
  70. find(Model<Time>* out_model, unsigned int out_port_index,
  71. Model<Time>* in_model, unsigned int in_port_index) const
  72. {
  73. std::pair<typename Links<Time>::const_iterator,
  74. typename Links<Time>::const_iterator> it =
  75. type::equal_range(Node<Time>(out_model, out_port_index));
  76. typename Links<Time>::const_iterator it2 = it.first;
  77. bool found = false;
  78. while (not found and it2 != it.second) {
  79. found = it2->second == Node<Time>(
  80. in_model, in_port_index);
  81. if (not found) {
  82. ++it2;
  83. }
  84. }
  85. return it2;
  86. }
  87. Links::Result find(Model<Time>* out_model, unsigned int out_port_index) const
  88. {
  89. return type::equal_range(common::Node<Time>(out_model, out_port_index));
  90. }
  91. void remove(common::Model<Time>* src_model, unsigned int src_port_index,
  92. common::Model<Time>* dst_model, unsigned int dst_port_index)
  93. {
  94. typename Links<Time>::const_iterator result = find(src_model, src_port_index,
  95. dst_model, dst_port_index);
  96. this->erase(result);
  97. }
  98. void remove_links(common::Model<Time>* model)
  99. {
  100. typename Links<Time>::iterator it = this->begin();
  101. while (it != this->end()) {
  102. if (it->first.get_model() == model or it->second.get_model() == model) {
  103. this->erase(it);
  104. it = this->begin();
  105. } else {
  106. ++it;
  107. }
  108. }
  109. }
  110. std::string to_string(int level = 0) const
  111. {
  112. std::stringstream ss;
  113. ss << common::String::make_spaces(level * 2) << "Links:" << std::endl;
  114. for (typename Links<Time>::const_iterator it = Links<Time>::begin();
  115. it != Links<Time>::end(); ++it) {
  116. ss << common::String::make_spaces((level + 1) * 2)
  117. << it->first.get_model()->get_name() << "::"
  118. << it->first.get_model()->get_out_port_name(
  119. it->first.get_port_index())
  120. << " -> "
  121. << it->second.get_model()->get_name() << "::"
  122. << it->second.get_model()->get_out_port_name(
  123. it->second.get_port_index()) << std::endl;
  124. }
  125. return ss.str();
  126. }
  127. };
  128. }
  129. } // namespace artis common
  130. #endif