Observer.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @file common/observer/Observer.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-2022 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 ARTIS_OBSERVER_OBSERVER_HPP
  26. #define ARTIS_OBSERVER_OBSERVER_HPP
  27. #include <artis-star/common/Model.hpp>
  28. #include <artis-star/common/observer/View.hpp>
  29. #include <vector>
  30. namespace artis::common::observer {
  31. template<typename Time>
  32. class Observer {
  33. public:
  34. typedef std::map<std::string, View<Time> *> Views;
  35. Observer(const common::Model<Time> *model)
  36. : _step(0), _model(model), _last_time(0), _init(false) {}
  37. virtual ~Observer() {
  38. for (typename Views::iterator it = _views.begin(); it != _views.end(); ++it) {
  39. delete it->second;
  40. }
  41. }
  42. void attachView(const std::string &name, View<Time> *view) {
  43. _views[name] = view;
  44. view->attachModel(_model);
  45. }
  46. void clear() {
  47. for (typename Views::iterator it = _views.begin(); it != _views.end(); ++it) {
  48. it->second->clear();
  49. }
  50. }
  51. Views *cloneViews() const {
  52. auto view = new Views();
  53. for (typename Views::const_iterator it = _views.begin();
  54. it != _views.end(); ++it) {
  55. (*view)[it->first] = it->second->clone();
  56. }
  57. return view;
  58. }
  59. const View<Time> &view(const std::string &name) const {
  60. return *_views.find(name)->second;
  61. }
  62. const Views &views() const { return _views; }
  63. void init() { }
  64. void observe(double t, double next_t, bool finish) {
  65. if (_step == 0) {
  66. observe(t);
  67. _last_time = t;
  68. } else {
  69. if (not _init) {
  70. observe(t);
  71. _last_time = t;
  72. _init = true;
  73. }
  74. double next_last_time = std::floor((_last_time + _step + epsilon) / _step) * _step;
  75. if (t <= next_last_time and (next_t > next_last_time or finish)) {
  76. do {
  77. observe(next_last_time);
  78. _last_time = next_last_time;
  79. next_last_time = std::floor((_last_time + _step + epsilon) / _step) * _step;
  80. } while (t < next_last_time and next_t > next_last_time);
  81. }
  82. }
  83. }
  84. void switch_to_timed_observer(double step) {
  85. assert(step > 0);
  86. _step = step;
  87. }
  88. private:
  89. void observe(double t) {
  90. for (typename Views::iterator it = _views.begin(); it != _views.end();
  91. ++it) {
  92. it->second->observe(t, _step == 0);
  93. }
  94. }
  95. const double epsilon = 1e-6;
  96. double _step;
  97. const common::Model<Time> *_model;
  98. Views _views;
  99. double _last_time;
  100. bool _init;
  101. };
  102. }
  103. #endif