GraphManager.hpp 3.3 KB

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