States.hpp 3.4 KB

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