GraphManager.hpp 3.3 KB

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