State.hpp 2.9 KB

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