Externals.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @file artis/kernel/Externals.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 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 {
  30. namespace kernel {
  31. template<typename T, typename U, typename V>
  32. class Externals {
  33. template<typename W>
  34. struct element {
  35. unsigned int index;
  36. const std::string name;
  37. W T::* var;
  38. element(unsigned int index, const std::string& name, W T::* var)
  39. :
  40. index(index), name(name), var(var) { }
  41. };
  42. public:
  43. Externals()
  44. :updated(false) { }
  45. virtual ~Externals() { }
  46. const Any& get(unsigned int index) const { return externals.at(index).second; }
  47. const std::string& name(unsigned int index) const { return external_names.at(index); }
  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. #ifdef WITH_TRACE
  72. utils::Trace<utils::DoubleTime>::trace()
  73. << utils::TraceElement<utils::DoubleTime>(
  74. true,
  75. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  76. utils::DoubleTime::null,
  77. utils::EXTERNAL_DECL)
  78. << utils::KernelInfo(it->name, false);
  79. utils::Trace<utils::DoubleTime>::trace().flush();
  80. #endif
  81. }
  82. }
  83. template<typename W>
  84. void external_(unsigned int index, const std::string& name, W T::* var)
  85. {
  86. if (externals.size() <= index) {
  87. externals.resize(index + 1, std::make_pair(-1, Any()));
  88. external_names.resize(index + 1, std::string());
  89. }
  90. externals[index] = std::make_pair(-1, var);
  91. external_names[index] = name;
  92. #ifdef WITH_TRACE
  93. utils::Trace<utils::DoubleTime>::trace()
  94. << utils::TraceElement<utils::DoubleTime>(
  95. true,
  96. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  97. utils::DoubleTime::null,
  98. utils::EXTERNAL_DECL)
  99. << utils::KernelInfo(name, false);
  100. utils::Trace<utils::DoubleTime>::trace().flush();
  101. #endif
  102. }
  103. bool is_ready(typename U::type t, unsigned int index) const { return externals.at(index).first == t; }
  104. template<typename W>
  105. void put(typename U::type t, unsigned int index, W value)
  106. {
  107. if (externals.at(index).first != t) {
  108. Any v = externals.at(index).second;
  109. v.put<T, W>(static_cast < T* >(this), value);
  110. externals.at(index).first = t;
  111. updated = true;
  112. } else {
  113. Any v = externals.at(index).second;
  114. if (static_cast < const T* >(this)->*(v.get<T, W>()) != value) {
  115. v.put<T, W>(static_cast < T* >(this), value);
  116. updated = true;
  117. }
  118. }
  119. #ifdef WITH_TRACE
  120. utils::Trace<utils::DoubleTime>::trace()
  121. << utils::TraceElement<utils::DoubleTime>(
  122. true,
  123. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  124. t, utils::PUT)
  125. << utils::KernelInfo(external_names[index], true,
  126. get(index).to_string(dynamic_cast < const T* >(this)));
  127. utils::Trace<utils::DoubleTime>::trace().flush();
  128. #endif
  129. }
  130. virtual void restore(AbstractModel<U, V>* model,
  131. const context::State<U>& state)
  132. {
  133. unsigned int index = 0;
  134. for (typename std::vector<std::pair<typename U::type,
  135. Any> >::iterator it = externals.begin();
  136. it != externals.end(); ++it) {
  137. Any& value = it->second;
  138. if (not value.is_null()) {
  139. value.restore<T>(static_cast < T* >(model),
  140. state.get_external(index));
  141. }
  142. ++index;
  143. }
  144. }
  145. virtual void save(const AbstractModel<U, V>* model,
  146. context::State<U>& state) const
  147. {
  148. unsigned int index = 0;
  149. for (typename std::vector<std::pair<typename U::type,
  150. Any> >::const_iterator it = externals.begin();
  151. it != externals.end(); ++it) {
  152. const Any& value = it->second;
  153. if (not value.is_null()) {
  154. state.add_external(index,
  155. value.save<T>(
  156. static_cast < const T* >(model)));
  157. }
  158. ++index;
  159. }
  160. }
  161. unsigned int size() const { return externals.size(); }
  162. protected:
  163. bool updated;
  164. std::vector<std::pair<typename U::type, Any> > externals;
  165. std::vector<std::string> external_names;
  166. };
  167. #define External(index, var) \
  168. external_(index, std::string(ESCAPEQUOTE(index)), var)
  169. #define Externals(W, L) E_< W >(UNWRAP2 L)
  170. }
  171. } // namespace artis kernel
  172. #endif