Links.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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>,
  37. Node<Time> > {
  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,
  46. unsigned int out_port_index,
  47. Model<Time>* in_model,
  48. unsigned int in_port_index)
  49. {
  50. std::multimap<Node<Time>,
  51. Node<Time> >::insert(
  52. std::pair<Node<Time>,
  53. Node<Time> >(
  54. Node<Time>(
  55. out_model, out_port_index),
  56. Node<Time>(
  57. in_model, in_port_index)));
  58. }
  59. bool exist(Model<Time>* out_model,
  60. unsigned int out_port_index,
  61. Model<Time>* in_model,
  62. unsigned int in_port_index) const
  63. {
  64. std::pair<typename Links<Time>::const_iterator,
  65. typename Links<Time>::const_iterator> it =
  66. std::multimap<Node<Time>,
  67. Node<Time> >::equal_range(
  68. Node<Time>(out_model, out_port_index));
  69. typename Links<Time>::const_iterator it2 = it.first;
  70. bool found = false;
  71. while (not found and it2 != it.second) {
  72. found = it2->second == Node<Time>(
  73. in_model, in_port_index);
  74. ++it2;
  75. }
  76. return found;
  77. }
  78. Links::Result find(Model<Time>* out_model,
  79. unsigned int out_port_index) const
  80. {
  81. return std::multimap<Node<Time>,
  82. Node<Time> >::equal_range(
  83. common::Node<Time>(
  84. out_model, out_port_index));
  85. }
  86. std::string to_string(int level = 0) const
  87. {
  88. std::stringstream ss;
  89. ss << common::String::make_spaces(level * 2) << "Links:" << std::endl;
  90. for (typename Links<Time>::const_iterator it =
  91. Links<Time>::begin();
  92. it != Links<Time>::end(); ++it) {
  93. ss << common::String::make_spaces((level + 1) * 2)
  94. << it->first.get_model()->get_name() << "::"
  95. << it->first.get_model()->get_out_port_name(
  96. it->first.get_port_index())
  97. << " -> "
  98. << it->second.get_model()->get_name() << "::"
  99. << it->second.get_model()->get_out_port_name(
  100. it->second.get_port_index()) << std::endl;
  101. }
  102. return ss.str();
  103. }
  104. };
  105. }
  106. } // namespace artis common
  107. #endif