StateValues.hpp 3.5 KB

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