GraphManager.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @file common/GraphManager.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 COMMON_GRAPH_MANAGER
  26. #define COMMON_GRAPH_MANAGER
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Links.hpp>
  29. #include <artis-star/common/Model.hpp>
  30. #include <artis-star/common/Parameters.hpp>
  31. #include <artis-star/common/utils/String.hpp>
  32. #include <sstream>
  33. namespace artis::common {
  34. template<typename Time>
  35. class GraphManager {
  36. public:
  37. GraphManager(common::Coordinator<Time> *coordinator)
  38. :
  39. _coordinator(coordinator) {}
  40. virtual ~GraphManager() {}
  41. virtual void add_child(unsigned int index, common::Model<Time> *child) {
  42. _children.push_back(child);
  43. _child_map[index] = child;
  44. child->set_parent(_coordinator);
  45. }
  46. virtual void add_children(unsigned int index, common::Model<Time> *child) {
  47. _children.push_back(child);
  48. if (_children_map.find(index) == _children_map.end()) {
  49. _children_map[index] = Models<Time>();
  50. }
  51. _children_map[index].push_back(child);
  52. child->set_parent(_coordinator);
  53. }
  54. const common::Models<Time> &children() const { return _children; }
  55. const common::ModelMap<Time> &child_map() const { return _child_map; }
  56. common::Model<Time> *child_map(size_t index) const { return _child_map.at(index); }
  57. const common::ModelsMap<Time> &children_map() const { return _children_map; }
  58. common::Coordinator<Time> *coordinator() const { return _coordinator; }
  59. virtual void remove_child(unsigned int index) {
  60. common::Model<Time> *child = _child_map[index];
  61. _children.erase(std::find(_children.begin(), _children.end(), child));
  62. _child_map[index] = nullptr;
  63. _child_map.erase(index);
  64. }
  65. void restore(const common::context::State<Time> &state) {
  66. for (typename common::ModelMap<Time>::iterator it = _child_map.begin(); it != _child_map.end();
  67. ++it) {
  68. it->second->restore(state.get_sub_state(it->first));
  69. }
  70. }
  71. void save(common::context::State<Time> &state) const {
  72. for (typename common::ModelMap<Time>::const_iterator it = _child_map.begin();
  73. it != _child_map.end(); ++it) {
  74. context::State <Time> sub_state;
  75. it->second->save(sub_state);
  76. state.add_sub_state(it->first, sub_state);
  77. }
  78. }
  79. protected:
  80. common::Models<Time> _children;
  81. common::ModelMap<Time> _child_map;
  82. common::ModelsMap<Time> _children_map;
  83. common::Coordinator<Time> *_coordinator;
  84. };
  85. } // namespace artis common
  86. #endif