StateValues.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file artis/context/StateValues.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_VALUES_HPP
  22. #define __ARTIS_KERNEL_STATE_VALUES_HPP
  23. #include <artis/context/Value.hpp>
  24. #include <map>
  25. #include <boost/serialization/serialization.hpp>
  26. #include <boost/serialization/map.hpp>
  27. namespace artis {
  28. namespace context {
  29. class StateValues {
  30. public:
  31. StateValues() { }
  32. virtual ~StateValues() { }
  33. void add_external(unsigned int key, const Value& value) { _externals[key] = value; }
  34. void add_internal(unsigned int key, const Value& value) { _internals[key] = value; }
  35. void add_state(unsigned int key, const Value& value) { _states[key] = value; }
  36. const Value& get_external(unsigned int key) const
  37. {
  38. std::map<unsigned int, Value>::const_iterator it =
  39. _externals.find(key);
  40. if (it != _externals.end()) {
  41. return it->second;
  42. } else {
  43. assert(false);
  44. return it->second;
  45. }
  46. }
  47. const Value& get_internal(unsigned int key) const
  48. {
  49. std::map<unsigned int, Value>::const_iterator it =
  50. _internals.find(key);
  51. if (it != _internals.end()) {
  52. return it->second;
  53. } else {
  54. assert(false);
  55. return it->second;
  56. }
  57. }
  58. const Value& get_state(unsigned int key) const
  59. {
  60. std::map<unsigned int, Value>::const_iterator it =
  61. _states.find(key);
  62. if (it != _states.end()) {
  63. return it->second;
  64. } else {
  65. assert(false);
  66. return it->second;
  67. }
  68. }
  69. std::string to_string() const
  70. {
  71. std::string str = "externals: [ ";
  72. for (std::map<unsigned int, Value>::const_iterator it =
  73. _externals.begin(); it != _externals.end(); ++it) {
  74. str += it->second.to_string() + " ";
  75. }
  76. str += "]; internals: [ ";
  77. for (std::map<unsigned int, Value>::const_iterator it =
  78. _internals.begin(); it != _internals.end(); ++it) {
  79. str += it->second.to_string() + " ";
  80. }
  81. str += "]; states: [ ";
  82. for (std::map<unsigned int, Value>::const_iterator it =
  83. _states.begin(); it != _states.end(); ++it) {
  84. str += it->second.to_string() + " ";
  85. }
  86. str += "]";
  87. return str;
  88. }
  89. private:
  90. friend class boost::serialization::access;
  91. template<class Archive>
  92. void serialize(Archive& ar, const unsigned int version)
  93. {
  94. (void) version;
  95. ar & _externals;
  96. ar & _internals;
  97. ar & _states;
  98. }
  99. std::map<unsigned int, Value> _externals;
  100. std::map<unsigned int, Value> _internals;
  101. std::map<unsigned int, Value> _states;
  102. };
  103. }
  104. }
  105. #endif