VectorScheduler.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file VectorScheduler.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_SCHEDULER_VECTOR_SCHEDULER_HPP
  26. #define COMMON_SCHEDULER_VECTOR_SCHEDULER_HPP 1
  27. #include <common/InternalEvent.hpp>
  28. #include <algorithm>
  29. #include <sstream>
  30. namespace paradevs { namespace common { namespace scheduler {
  31. template < class Time >
  32. class VectorScheduler : protected std::vector < InternalEvent < Time > >
  33. {
  34. public:
  35. VectorScheduler()
  36. { }
  37. virtual ~VectorScheduler()
  38. { }
  39. Model < Time >* get_current_model()
  40. {
  41. return VectorScheduler < Time >::front().get_model();
  42. }
  43. Models < Time > get_current_models(typename Time::type time) const
  44. {
  45. Models < Time > models;
  46. for (typename VectorScheduler < Time >::const_iterator it =
  47. VectorScheduler < Time >::begin();
  48. it != VectorScheduler < Time >::end() and it->get_time() == time;
  49. ++it) {
  50. models.push_back(it->get_model());
  51. }
  52. return models;
  53. }
  54. typename Time::type get_current_time() const
  55. { return VectorScheduler < Time >::front().get_time(); }
  56. void init(typename Time::type time, Model < Time >* model)
  57. {
  58. VectorScheduler < Time >::push_back(
  59. InternalEvent < Time >(time, model));
  60. std::sort(VectorScheduler < Time >::begin(),
  61. VectorScheduler < Time >::end());
  62. }
  63. void put(typename Time::type time, Model < Time >* model)
  64. {
  65. remove(model);
  66. VectorScheduler < Time >::push_back(
  67. InternalEvent < Time >(time, model));
  68. std::sort(VectorScheduler < Time >::begin(),
  69. VectorScheduler < Time >::end());
  70. }
  71. std::string to_string() const
  72. {
  73. std::stringstream ss;
  74. ss << "Scheduler = { ";
  75. for (typename VectorScheduler < Time >::const_iterator it =
  76. VectorScheduler < Time >::begin();
  77. it != VectorScheduler < Time >::end(); ++it) {
  78. ss << "(" << it->get_time() << " -> " << it->get_model()->get_name()
  79. << ") ";
  80. }
  81. ss << "}";
  82. return ss.str();
  83. }
  84. private:
  85. void remove(Model < Time >* model)
  86. {
  87. typename VectorScheduler < Time >::iterator jt =
  88. VectorScheduler < Time >::begin();
  89. while (jt != VectorScheduler < Time >::end()) {
  90. if (jt->get_model() == model) {
  91. jt = VectorScheduler < Time >::erase(jt);
  92. } else {
  93. ++jt;
  94. }
  95. }
  96. }
  97. };
  98. } } } // namespace paradevs common scheduler
  99. #endif