Links.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @file Links.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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 <common/Node.hpp>
  28. #include <common/utils/String.hpp>
  29. #include <map>
  30. #include <sstream>
  31. namespace paradevs { namespace common {
  32. template < class Time, class SchedulerHandle >
  33. class Node;
  34. template < class Time, class SchedulerHandle >
  35. class Links : public std::multimap < Node < Time, SchedulerHandle >,
  36. Node < Time, SchedulerHandle > >
  37. {
  38. public:
  39. typedef std::pair <
  40. typename Links < Time, SchedulerHandle >::const_iterator,
  41. typename Links < Time, SchedulerHandle >::const_iterator
  42. > Result;
  43. Links()
  44. { }
  45. virtual ~Links()
  46. { }
  47. void add(Model < Time, SchedulerHandle >* out_model,
  48. const std::string& out_port_name,
  49. Model < Time, SchedulerHandle >* in_model,
  50. const std::string& in_port_name)
  51. {
  52. std::multimap < Node < Time, SchedulerHandle >,
  53. Node < Time, SchedulerHandle > >::insert(
  54. std::pair < Node < Time, SchedulerHandle >,
  55. Node < Time, SchedulerHandle > >(
  56. Node < Time, SchedulerHandle >(
  57. out_model, out_port_name),
  58. Node < Time, SchedulerHandle >(
  59. in_model, in_port_name)));
  60. }
  61. bool exist(Model < Time, SchedulerHandle >* out_model,
  62. const std::string& out_port_name,
  63. Model < Time, SchedulerHandle >* in_model,
  64. const std::string& in_port_name) const
  65. {
  66. typename Links < Time, SchedulerHandle >::const_iterator it =
  67. std::multimap < Node < Time, SchedulerHandle >,
  68. Node < Time, SchedulerHandle > >::find(
  69. Node < Time, SchedulerHandle >(out_model,
  70. out_port_name));
  71. bool found = false;
  72. while (not found and it != Links < Time, SchedulerHandle >::end()) {
  73. found = it->second == Node < Time, SchedulerHandle >(
  74. in_model, in_port_name);
  75. ++it;
  76. }
  77. return found;
  78. }
  79. Links::Result find(Model < Time, SchedulerHandle >* out_model,
  80. const std::string& out_port_name) const
  81. {
  82. return std::multimap < Node < Time, SchedulerHandle >,
  83. Node < Time, SchedulerHandle > >::equal_range(
  84. common::Node < Time, SchedulerHandle >(
  85. out_model, out_port_name));
  86. }
  87. std::string to_string(int level = 0) const
  88. {
  89. std::stringstream ss;
  90. ss << common::spaces(level * 2) << "Links:" << std::endl;
  91. for (typename Links < Time, SchedulerHandle >::const_iterator it =
  92. Links < Time, SchedulerHandle >::begin();
  93. it != Links < Time, SchedulerHandle >::end(); ++it) {
  94. ss << common::spaces((level + 1) * 2)
  95. << it->first.get_model()->get_name() << "::"
  96. << it->first.get_port_name()
  97. << " -> "
  98. << it->second.get_model()->get_name() << "::"
  99. << it->second.get_port_name() << std::endl;
  100. }
  101. return ss.str();
  102. }
  103. };
  104. } } // namespace paradevs common
  105. #endif