Externals.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file artis/kernel/Externals.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_EXTERNALS_HPP
  22. #define __ARTIS_KERNEL_EXTERNALS_HPP
  23. #include <artis/kernel/Any.hpp>
  24. #include <artis/kernel/Macro.hpp>
  25. #include <vector>
  26. #if WIN32
  27. #include <iso646.h>
  28. #endif
  29. namespace artis { namespace kernel {
  30. template < typename T, typename U, typename V >
  31. class Externals
  32. {
  33. template < typename W >
  34. struct element
  35. {
  36. unsigned int index;
  37. const std::string name;
  38. W T::* var;
  39. element(unsigned int index, const std::string& name, W T::* var) :
  40. index(index), name(name), var(var)
  41. { }
  42. };
  43. public:
  44. Externals() : updated(false)
  45. { }
  46. virtual ~Externals()
  47. { }
  48. bool check(typename U::type t) const
  49. {
  50. bool OK = true;
  51. typename std::vector <
  52. std::pair < double,
  53. Any > >::const_iterator it = externals.begin();
  54. while (it != externals.end() and OK) {
  55. OK = it->first == t;
  56. ++it;
  57. }
  58. return OK;
  59. }
  60. template < typename W >
  61. void E_(std::initializer_list < element < W > > list)
  62. {
  63. for (typename std::initializer_list < element < W > >::iterator it =
  64. list.begin(); it != list.end(); ++it) {
  65. if (externals.size() <= it->index) {
  66. externals.resize(it->index + 1, std::make_pair(-1, Any()));
  67. external_names.resize(it->index + 1, std::string());
  68. }
  69. externals[it->index] = std::make_pair(-1, it->var);
  70. external_names[it->index] = it->name;
  71. }
  72. }
  73. template < typename W >
  74. void external_(unsigned int index, const std::string& name, W T::* var)
  75. {
  76. if (externals.size() <= index) {
  77. externals.resize(index + 1, std::make_pair(-1, Any()));
  78. external_names.resize(index + 1, std::string());
  79. }
  80. externals[index] = std::make_pair(-1, var);
  81. external_names[index] = name;
  82. }
  83. bool is_ready(typename U::type t, unsigned int index) const
  84. { return externals.at(index).first == t; }
  85. template < typename W >
  86. void put(typename U::type t, unsigned int index, W value)
  87. {
  88. if (externals.at(index).first != t) {
  89. Any v = externals.at(index).second;
  90. v.put < T, W >(static_cast < T* >(this), value);
  91. externals.at(index).first = t;
  92. updated = true;
  93. } else {
  94. Any v = externals.at(index).second;
  95. if (static_cast < const T* >(this)->*(v.get < T, W >()) != value) {
  96. v.put < T, W >(static_cast < T* >(this), value);
  97. updated = true;
  98. }
  99. }
  100. }
  101. virtual void restore(AbstractModel < U, V >* model,
  102. const context::State < U >& state)
  103. {
  104. unsigned int index = 0;
  105. for (typename std::vector < std::pair < typename U::type,
  106. Any > >::iterator it = externals.begin();
  107. it != externals.end(); ++it) {
  108. Any& value = it->second;
  109. if (not value.is_null()) {
  110. value.restore < T >(static_cast < T* >(model),
  111. state.get_external(index));
  112. }
  113. ++index;
  114. }
  115. }
  116. virtual void save(const AbstractModel < U, V >* model,
  117. context::State < U >& state) const
  118. {
  119. unsigned int index = 0;
  120. for (typename std::vector < std::pair < typename U::type,
  121. Any > >::const_iterator it = externals.begin();
  122. it != externals.end(); ++it) {
  123. const Any& value = it->second;
  124. if (not value.is_null()) {
  125. state.add_external(index,
  126. value.save < T >(
  127. static_cast < const T* >(model)));
  128. }
  129. ++index;
  130. }
  131. }
  132. unsigned int size() const
  133. { return externals.size(); }
  134. protected:
  135. bool updated;
  136. std::vector < std::pair < typename U::type, Any > > externals;
  137. std::vector < std::string > external_names;
  138. };
  139. #define External(index, var) \
  140. external_(index, std::string(ESCAPEQUOTE(index)), var)
  141. #define Externals(W,L) E_< W >(UNWRAP2 L)
  142. } } // namespace artis kernel
  143. #endif