States.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file artis/kernel/States.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 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/Value.hpp>
  25. #include <vector>
  26. namespace artis { namespace kernel {
  27. template < typename T, typename U >
  28. class States
  29. {
  30. template < typename V >
  31. struct element
  32. {
  33. unsigned int index;
  34. const std::string name;
  35. V T::* var;
  36. element(unsigned int index, const std::string& name, V T::* var) :
  37. index(index), name(name), var(var)
  38. { }
  39. };
  40. public:
  41. States()
  42. { }
  43. virtual ~States()
  44. { }
  45. const Any& get(unsigned int index) const
  46. { return states.at(index); }
  47. template < typename V >
  48. void S_(std::initializer_list < element < V > > list)
  49. {
  50. for (typename std::initializer_list < element < V > >::iterator it =
  51. list.begin(); it != list.end(); ++it) {
  52. if (states.size() <= it->index) {
  53. states.resize(it->index + 1, 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(const context::State < U >& /* state */)
  61. {
  62. // unsigned int index = 0;
  63. // for (typename std::vector < bool T::* >::iterator it =
  64. // statesB.begin(); it != statesB.end(); ++it) {
  65. // state.get_state(index, *it);
  66. // ++index;
  67. // }
  68. // index = 0;
  69. // for (typename std::vector < int T::* >::iterator it =
  70. // statesI.begin(); it != statesI.end(); ++it) {
  71. // state.get_state(index, *it);
  72. // ++index;
  73. // }
  74. // index = 0;
  75. // for (typename std::vector < double T::* >::iterator it =
  76. // statesD.begin(); it != statesD.end(); ++it) {
  77. // state.get_state(index, *it);
  78. // ++index;
  79. // }
  80. }
  81. virtual void save(context::State < U >& /* state */) const
  82. {
  83. // unsigned int index = 0;
  84. // for (typename std::vector < bool T::* >::const_iterator it =
  85. // statesB.begin(); it != statesB.end(); ++it) {
  86. // state.add_state(index, *it);
  87. // ++index;
  88. // }
  89. // index = 0;
  90. // for (typename std::vector < int T::* >::const_iterator it =
  91. // statesI.begin(); it != statesI.end(); ++it) {
  92. // state.add_state(index, *it);
  93. // ++index;
  94. // }
  95. // index = 0;
  96. // for (typename std::vector < double T::* >::const_iterator it =
  97. // statesD.begin(); it != statesD.end(); ++it) {
  98. // state.add_state(index, *it);
  99. // ++index;
  100. // }
  101. }
  102. template < typename V >
  103. void state_(unsigned int index, const std::string& name, V T::* var)
  104. {
  105. if (states.size() <= index) {
  106. states.resize(index + 1, Any());
  107. state_names.resize(index + 1, std::string());
  108. }
  109. states[index] = Any(var);
  110. state_names[index] = name;
  111. }
  112. private:
  113. std::vector < Any > states;
  114. std::vector < std::string > state_names;
  115. };
  116. #define State(index, var) \
  117. state_(index, std::string(ESCAPEQUOTE(index)), var)
  118. #define States(V,L) S_< V >(UNWRAP2 L)
  119. } }
  120. #endif