Model.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file kernel/sss/Model.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 SSS_MODEL
  26. #define SSS_MODEL
  27. #include <artis-star/common/Model.hpp>
  28. namespace artis {
  29. namespace sss {
  30. template<class Time>
  31. class Model : public virtual common::Model<Time>
  32. {
  33. public:
  34. Model(const std::string &name)
  35. :
  36. common::Model<Time>(name)
  37. {
  38. assigned = 0;
  39. _mark = false;
  40. _send = false;
  41. }
  42. virtual ~Model()
  43. {}
  44. virtual void update_buffer(typename Time::type /* time */) = 0;
  45. void add_event(const common::ExternalEvent<Time> &message)
  46. {
  47. common::Model<Time>::add_event(message);
  48. std::map<std::string, bool>::iterator it =
  49. port_assigned.find(message.get_port_name());
  50. if (it != port_assigned.end() and not it->second) {
  51. port_assigned[message.get_port_name()] = true;
  52. ++assigned;
  53. }
  54. }
  55. void add_in_port(const std::string &port_name, bool sync)
  56. {
  57. common::Model<Time>::add_in_port(port_name);
  58. if (sync) {
  59. port_assigned[port_name] = false;
  60. }
  61. }
  62. bool all_ports_are_assigned() const
  63. { return assigned == port_assigned.size(); }
  64. void clear_bag()
  65. {
  66. common::Model<Time>::clear_bag();
  67. for (auto &p: port_assigned) {
  68. p.second = false;
  69. }
  70. assigned = 0;
  71. }
  72. bool is_marked() const
  73. { return _mark; }
  74. bool is_send() const
  75. { return _send; }
  76. void mark()
  77. { _mark = true; }
  78. void send()
  79. { _send = true; }
  80. void unmark()
  81. { _mark = false; }
  82. void unsend()
  83. { _send = false; }
  84. private:
  85. std::map<std::string, bool> port_assigned;
  86. unsigned int assigned;
  87. bool _mark;
  88. bool _send;
  89. };
  90. template<class Time>
  91. class Models : public std::vector<sss::Model<Time> *>
  92. {
  93. public:
  94. Models()
  95. {}
  96. virtual ~Models()
  97. {}
  98. std::string to_string() const
  99. {
  100. std::ostringstream ss;
  101. ss << "{ ";
  102. for (typename Models<Time>::const_iterator it =
  103. Models<Time>::begin();
  104. it != Models<Time>::end(); ++it) {
  105. ss << (*it)->get_name() << " ";
  106. }
  107. ss << "}";
  108. return ss.str();
  109. }
  110. };
  111. }
  112. } // namespace artis sss
  113. #endif