States.hpp 3.5 KB

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