Links.hpp 4.4 KB

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