State.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file artis-star/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. {
  44. _values.add_state(variable_key, value);
  45. }
  46. void add_substate(unsigned int model_key, State& state)
  47. {
  48. _substates.insert(std::make_pair(model_key, state));
  49. }
  50. const Value& get_state(unsigned int variable_key) const
  51. {
  52. return _values.get_state(variable_key);
  53. }
  54. const State<Time>& get_substate(unsigned int model_key) const
  55. {
  56. return _substates.find(model_key)->second;
  57. }
  58. typename Time::type last_time() const { return _last_time; }
  59. void last_time(typename Time::type t) { _last_time = t; }
  60. typename Time::type next_time() const { return _next_time; }
  61. void next_time(typename Time::type t) { _next_time = t; }
  62. std::string to_string() const
  63. {
  64. std::string str = "last_time: " + std::to_string(_last_time) +
  65. "; next_time: " + std::to_string(_next_time) +
  66. "; values: " + _values.to_string() + "; sub_states: [ ";
  67. for (typename Substates::const_iterator it = _substates.begin();
  68. it != _substates.end(); ++it) {
  69. str += it->second.to_string() + " ";
  70. }
  71. str += "]";
  72. return str;
  73. }
  74. private:
  75. friend class boost::serialization::access;
  76. template<class Archive>
  77. void serialize(Archive& ar, const unsigned int version)
  78. {
  79. (void) version;
  80. ar & _values;
  81. ar & _substates;
  82. ar & _last_time;
  83. ar & _next_time;
  84. }
  85. StateValues _values;
  86. Substates _substates;
  87. typename Time::type _last_time;
  88. typename Time::type _next_time;
  89. };
  90. }
  91. }
  92. }
  93. #endif