Links.hpp 3.8 KB

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