States.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file 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. {
  53. for (typename std::initializer_list<element<W> >::iterator it =
  54. list.begin(); it != list.end(); ++it) {
  55. if (states.size() <= it->index) {
  56. states.resize(it->index + 1, common::Any());
  57. state_names.resize(it->index + 1, std::string());
  58. }
  59. states[it->index] = it->var;
  60. state_names[it->index] = it->name;
  61. }
  62. }
  63. virtual void restore(Dyn* model, const common::context::State<Time>& state)
  64. {
  65. unsigned int index = 0;
  66. for (typename std::vector<common::Any>::iterator it = states.begin();
  67. it != states.end(); ++it) {
  68. if (not it->is_null()) {
  69. it->restore<Dyn>(model, state.get_state(index));
  70. }
  71. ++index;
  72. }
  73. }
  74. virtual void save(const Dyn* model, common::context::State<Time>& state) const
  75. {
  76. unsigned int index = 0;
  77. for (typename std::vector<common::Any>::const_iterator it =
  78. states.begin(); it != states.end(); ++it) {
  79. if (not it->is_null()) {
  80. state.add_state(index, it->save<Dyn>(model));
  81. }
  82. ++index;
  83. }
  84. }
  85. template<class W>
  86. void state_(unsigned int index, const std::string& name, W Dyn::* var)
  87. {
  88. if (states.size() <= index) {
  89. states.resize(index + 1, common::Any());
  90. state_names.resize(index + 1, std::string());
  91. }
  92. states[index] = common::Any(var);
  93. state_names[index] = name;
  94. }
  95. private:
  96. std::vector<common::Any> states;
  97. std::vector<std::string> state_names;
  98. };
  99. }
  100. }
  101. #define DECLARE_STATE(index, var) \
  102. this->state_(index, std::string(ESCAPEQUOTE(index)), var)
  103. #define DECLARE_STATES(W, L) this-> template S_< W >(UNWRAP2 L)
  104. #endif