States.hpp 3.4 KB

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