State.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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-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_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::common::context {
  33. template<typename Time>
  34. class State {
  35. typedef std::map<unsigned int, State<Time> > Substates;
  36. public:
  37. State()
  38. : _last_time(-1), _next_time(-1) {}
  39. State(const State &) = default;
  40. State(State &&) = default;
  41. virtual ~State() = default;
  42. void add_state(unsigned int variable_key, const event::Value &value) {
  43. _values.add_state(variable_key, value);
  44. }
  45. void add_sub_state(unsigned int model_key, State &state) {
  46. _sub_states.insert(std::make_pair(model_key, state));
  47. }
  48. const event::Value &get_state(unsigned int variable_key) const {
  49. return _values.get_state(variable_key);
  50. }
  51. const State<Time> &get_sub_state(unsigned int model_key) const {
  52. return _sub_states.find(model_key)->second;
  53. }
  54. typename Time::type last_time() const { return _last_time; }
  55. void last_time(typename Time::type time) { _last_time = time; }
  56. typename Time::type next_time() const { return _next_time; }
  57. void next_time(typename Time::type time) { _next_time = time; }
  58. State& operator=(const State& state) {
  59. _values = state._values;
  60. _sub_states = state._sub_states;
  61. _last_time = state._last_time;
  62. _next_time = state._next_time;
  63. return *this;
  64. }
  65. State& operator=(State&& state) {
  66. _values = state._values;
  67. _sub_states = state._sub_states;
  68. _last_time = state._last_time;
  69. _next_time = state._next_time;
  70. return *this;
  71. }
  72. std::string to_string() const {
  73. std::string str = "last_time: " + std::to_string(_last_time) +
  74. "; next_time: " + std::to_string(_next_time) +
  75. "; values: { " + _values.to_string() + " } ; sub_states: [ ";
  76. for (typename Substates::const_iterator it = _sub_states.begin();
  77. it != _sub_states.end(); ++it) {
  78. str += "{ " + it->second.to_string() + "} ";
  79. }
  80. str += "]";
  81. return str;
  82. }
  83. private:
  84. friend class boost::serialization::access;
  85. template<typename Archive>
  86. void serialize(Archive &ar, const unsigned int version) {
  87. (void) version;
  88. ar & _values;
  89. ar & _sub_states;
  90. ar & _last_time;
  91. ar & _next_time;
  92. }
  93. StateValues _values;
  94. Substates _sub_states;
  95. typename Time::type _last_time;
  96. typename Time::type _next_time;
  97. };
  98. }
  99. #endif