State.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @file common/context/State.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_CONTEXT_STATE_HPP
  26. #define COMMON_CONTEXT_STATE_HPP
  27. #include <artis-star/common/context/StateValues.hpp>
  28. #include <map>
  29. #include <boost/serialization/serialization.hpp>
  30. #include <boost/serialization/map.hpp>
  31. #include <boost/serialization/vector.hpp>
  32. namespace artis {
  33. namespace common {
  34. namespace context {
  35. template<class Time>
  36. class State {
  37. typedef std::map<unsigned int, State<Time> > Substates;
  38. public:
  39. State()
  40. : _last_time(-1), _next_time(-1) {}
  41. virtual ~State() {}
  42. void add_state(unsigned int variable_key, const Value &value) {
  43. _values.add_state(variable_key, value);
  44. }
  45. void add_substate(unsigned int model_key, State &state) {
  46. _substates.insert(std::make_pair(model_key, state));
  47. }
  48. const Value &get_state(unsigned int variable_key) const {
  49. return _values.get_state(variable_key);
  50. }
  51. const State<Time> &get_substate(unsigned int model_key) const {
  52. return _substates.find(model_key)->second;
  53. }
  54. typename Time::type last_time() const { return _last_time; }
  55. void last_time(typename Time::type t) { _last_time = t; }
  56. typename Time::type next_time() const { return _next_time; }
  57. void next_time(typename Time::type t) { _next_time = t; }
  58. std::string to_string() const {
  59. std::string str = "last_time: " + std::to_string(_last_time) +
  60. "; next_time: " + std::to_string(_next_time) +
  61. "; values: " + _values.to_string() + "; sub_states: [ ";
  62. for (typename Substates::const_iterator it = _substates.begin();
  63. it != _substates.end(); ++it) {
  64. str += it->second.to_string() + " ";
  65. }
  66. str += "]";
  67. return str;
  68. }
  69. private:
  70. friend class boost::serialization::access;
  71. template<class Archive>
  72. void serialize(Archive &ar, const unsigned int version) {
  73. (void) version;
  74. ar & _values;
  75. ar & _substates;
  76. ar & _last_time;
  77. ar & _next_time;
  78. }
  79. StateValues _values;
  80. Substates _substates;
  81. typename Time::type _last_time;
  82. typename Time::type _next_time;
  83. };
  84. }
  85. }
  86. }
  87. #endif