State.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @file artis/context/State.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_KERNEL_STATE_HPP
  22. #define __ARTIS_KERNEL_STATE_HPP
  23. #include <artis/context/StateValues.hpp>
  24. #include <map>
  25. #include <boost/serialization/serialization.hpp>
  26. #include <boost/serialization/map.hpp>
  27. #include <boost/serialization/vector.hpp>
  28. namespace artis {
  29. namespace context {
  30. template<typename U>
  31. class State {
  32. typedef std::map<unsigned int, State<U> > Substates;
  33. public:
  34. State()
  35. :_last_time(-1) { }
  36. virtual ~State() { }
  37. void add_external(unsigned int variable_key, const Value& value)
  38. {
  39. _values.add_external(variable_key, value);
  40. }
  41. void add_internal(unsigned int variable_key, const Value& value)
  42. {
  43. _values.add_internal(variable_key, value);
  44. }
  45. void add_state(unsigned int variable_key, const Value& value) { _values.add_state(variable_key, value); }
  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_external(unsigned int variable_key) const { return _values.get_external(variable_key); }
  51. const Value& get_internal(unsigned int variable_key) const { return _values.get_internal(variable_key); }
  52. const Value& get_state(unsigned int variable_key) const { return _values.get_state(variable_key); }
  53. const State<U>& get_substate(unsigned int model_key) const { return _substates.find(model_key)->second; }
  54. typename U::type last_time() const { return _last_time; }
  55. void last_time(typename U::type t) { _last_time = t; }
  56. std::string to_string() const
  57. {
  58. std::string str = "last_time: " + std::to_string(_last_time) +
  59. "; values: " + _values.to_string() + "; sub_states: [ ";
  60. for (typename Substates::const_iterator it = _substates.begin();
  61. it != _substates.end(); ++it) {
  62. str += it->second.to_string() + " ";
  63. }
  64. str += "]";
  65. return str;
  66. }
  67. private:
  68. friend class boost::serialization::access;
  69. template<class Archive>
  70. void serialize(Archive& ar, const unsigned int version)
  71. {
  72. (void) version;
  73. ar & _values;
  74. ar & _substates;
  75. ar & _last_time;
  76. }
  77. StateValues _values;
  78. Substates _substates;
  79. typename U::type _last_time;
  80. };
  81. }
  82. }
  83. #endif