States.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file artis/kernel/States.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_STATES_HPP
  22. #define __ARTIS_KERNEL_STATES_HPP
  23. #include <artis/context/State.hpp>
  24. #include <artis/kernel/Any.hpp>
  25. #include <vector>
  26. namespace artis {
  27. namespace kernel {
  28. template<typename T, typename U, typename V>
  29. class States {
  30. template<typename W>
  31. struct element {
  32. unsigned int index;
  33. const std::string name;
  34. W T::* var;
  35. element(unsigned int index, const std::string& name, W T::* var)
  36. :
  37. index(index), name(name), var(var) { }
  38. };
  39. public:
  40. States() { }
  41. virtual ~States() { }
  42. const Any& get(unsigned int index) const { return states.at(index); }
  43. template<typename W>
  44. void S_(std::initializer_list<element<W> > list)
  45. {
  46. for (typename std::initializer_list<element<W> >::iterator it =
  47. list.begin(); it != list.end(); ++it) {
  48. if (states.size() <= it->index) {
  49. states.resize(it->index + 1, Any());
  50. state_names.resize(it->index + 1, std::string());
  51. }
  52. states[it->index] = it->var;
  53. state_names[it->index] = it->name;
  54. }
  55. }
  56. virtual void restore(AbstractModel<U, V>* model,
  57. const context::State<U>& state)
  58. {
  59. unsigned int index = 0;
  60. for (typename std::vector<Any>::iterator it = states.begin();
  61. it != states.end(); ++it) {
  62. if (not it->is_null()) {
  63. it->restore<T>(static_cast < T* >(model),
  64. state.get_state(index));
  65. }
  66. ++index;
  67. }
  68. }
  69. virtual void save(const AbstractModel<U, V>* model,
  70. context::State<U>& state) const
  71. {
  72. unsigned int index = 0;
  73. for (typename std::vector<Any>::const_iterator it =
  74. states.begin(); it != states.end(); ++it) {
  75. if (not it->is_null()) {
  76. state.add_state(index,
  77. it->save<T>(
  78. static_cast < const T* >(model)));
  79. }
  80. ++index;
  81. }
  82. }
  83. template<typename W>
  84. void state_(unsigned int index, const std::string& name, W T::* var)
  85. {
  86. if (states.size() <= index) {
  87. states.resize(index + 1, Any());
  88. state_names.resize(index + 1, std::string());
  89. }
  90. states[index] = Any(var);
  91. state_names[index] = name;
  92. }
  93. private:
  94. std::vector<Any> states;
  95. std::vector<std::string> state_names;
  96. };
  97. #define State(index, var) \
  98. state_(index, std::string(ESCAPEQUOTE(index)), var)
  99. #define States(W, L) S_< W >(UNWRAP2 L)
  100. }
  101. }
  102. #endif