Internals.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file artis/kernel/Internals.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_INTERNALS_HPP
  22. #define __ARTIS_KERNEL_INTERNALS_HPP
  23. #include <artis/kernel/Macro.hpp>
  24. #include <artis/kernel/State.hpp>
  25. #include <artis/kernel/Value.hpp>
  26. #include <vector>
  27. namespace artis { namespace kernel {
  28. template < typename T, typename U >
  29. class Internals
  30. {
  31. template < typename V >
  32. struct element
  33. {
  34. unsigned int index;
  35. const std::string name;
  36. V T::* var;
  37. element(unsigned int index, const std::string& name, V T::* var) :
  38. index(index), name(name), var(var)
  39. { }
  40. };
  41. public:
  42. Internals()
  43. { }
  44. virtual ~Internals()
  45. { }
  46. const Any& get(unsigned int index) const
  47. { return internals.at(index); }
  48. template < typename V >
  49. void I_(std::initializer_list < element < V > > list)
  50. {
  51. for (typename std::initializer_list <element < V > >::iterator it =
  52. list.begin(); it != list.end(); ++it) {
  53. if (internals.size() <= it->index) {
  54. internals.resize(it->index + 1);
  55. internal_names.resize(it->index + 1, std::string());
  56. }
  57. internals[it->index] = it->var;
  58. internal_names[it->index] = it->name;
  59. }
  60. }
  61. template < typename V >
  62. void internal_(unsigned int index, const std::string& name, V T::* var)
  63. {
  64. if (internals.size() <= index) {
  65. internals.resize(index + 1, Any());
  66. internal_names.resize(index + 1, std::string());
  67. }
  68. internals[index] = Any(var);
  69. internal_names[index] = name;
  70. }
  71. const std::string& name(unsigned int index) const
  72. { return internal_names.at(index); }
  73. virtual void restore(const State < U >& /* state */)
  74. {
  75. // unsigned int index = 0;
  76. // for (typename std::vector < Any >::iterator it =
  77. // internals.begin(); it != internals.end(); ++it) {
  78. // state.get_internal(index, *it);
  79. // ++index;
  80. // }
  81. // index = 0;
  82. // for (typename std::vector < int T::* >::iterator it =
  83. // internalsI.begin(); it != internalsI.end(); ++it) {
  84. // state.get_internal(index, *it);
  85. // ++index;
  86. // }
  87. // index = 0;
  88. // for (typename std::vector < double T::* >::iterator it =
  89. // internalsD.begin(); it != internalsD.end(); ++it) {
  90. // state.get_internal(index, *it);
  91. // ++index;
  92. // }
  93. }
  94. virtual void save(State < U >& /* state */) const
  95. {
  96. // unsigned int index = 0;
  97. // for (typename std::vector < bool T::* >::const_iterator it =
  98. // internalsB.begin(); it != internalsB.end(); ++it) {
  99. // state.add_internal(index, *it);
  100. // ++index;
  101. // }
  102. // index = 0;
  103. // for (typename std::vector < int T::* >::const_iterator it =
  104. // internalsI.begin(); it != internalsI.end(); ++it) {
  105. // state.add_internal(index, *it);
  106. // ++index;
  107. // }
  108. // index = 0;
  109. // for (typename std::vector < double T::* >::const_iterator it =
  110. // internalsD.begin(); it != internalsD.end(); ++it) {
  111. // state.add_internal(index, *it);
  112. // ++index;
  113. // }
  114. }
  115. unsigned int size() const
  116. { return internals.size(); }
  117. private:
  118. std::vector < Any > internals;
  119. std::vector < std::string > internal_names;
  120. };
  121. #define Internal(index, var) \
  122. internal_(index, std::string(ESCAPEQUOTE(index)), var)
  123. #define Internals(V,L) I_< V >(UNWRAP2 L)
  124. } } // namespace artis kernel
  125. #endif