Externals.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. const Any& get(unsigned int index) const
  49. { return externals.at(index).second; }
  50. const std::string& name(unsigned int index) const
  51. { return external_names.at(index); }
  52. bool check(typename U::type t) const
  53. {
  54. bool OK = true;
  55. typename std::vector <
  56. std::pair < double,
  57. Any > >::const_iterator it = externals.begin();
  58. while (it != externals.end() and OK) {
  59. OK = it->first == t;
  60. ++it;
  61. }
  62. return OK;
  63. }
  64. template < typename W >
  65. void E_(std::initializer_list < element < W > > list)
  66. {
  67. for (typename std::initializer_list < element < W > >::iterator it =
  68. list.begin(); it != list.end(); ++it) {
  69. if (externals.size() <= it->index) {
  70. externals.resize(it->index + 1, std::make_pair(-1, Any()));
  71. external_names.resize(it->index + 1, std::string());
  72. }
  73. externals[it->index] = std::make_pair(-1, it->var);
  74. external_names[it->index] = it->name;
  75. #ifdef WITH_TRACE
  76. utils::Trace < utils::DoubleTime >::trace()
  77. << utils::TraceElement < utils::DoubleTime >(
  78. true,
  79. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  80. utils::DoubleTime::null,
  81. utils::EXTERNAL_DECL)
  82. << utils::KernelInfo(it->name, false);
  83. utils::Trace < utils::DoubleTime >::trace().flush();
  84. #endif
  85. }
  86. }
  87. template < typename W >
  88. void external_(unsigned int index, const std::string& name, W T::* var)
  89. {
  90. if (externals.size() <= index) {
  91. externals.resize(index + 1, std::make_pair(-1, Any()));
  92. external_names.resize(index + 1, std::string());
  93. }
  94. externals[index] = std::make_pair(-1, var);
  95. external_names[index] = name;
  96. #ifdef WITH_TRACE
  97. utils::Trace < utils::DoubleTime >::trace()
  98. << utils::TraceElement < utils::DoubleTime >(
  99. true,
  100. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  101. utils::DoubleTime::null,
  102. utils::EXTERNAL_DECL)
  103. << utils::KernelInfo(name, false);
  104. utils::Trace < utils::DoubleTime >::trace().flush();
  105. #endif
  106. }
  107. bool is_ready(typename U::type t, unsigned int index) const
  108. { return externals.at(index).first == t; }
  109. template < typename W >
  110. void put(typename U::type t, unsigned int index, W value)
  111. {
  112. if (externals.at(index).first != t) {
  113. Any v = externals.at(index).second;
  114. v.put < T, W >(static_cast < T* >(this), value);
  115. externals.at(index).first = t;
  116. updated = true;
  117. } else {
  118. Any v = externals.at(index).second;
  119. if (static_cast < const T* >(this)->*(v.get < T, W >()) != value) {
  120. v.put < T, W >(static_cast < T* >(this), value);
  121. updated = true;
  122. }
  123. }
  124. #ifdef WITH_TRACE
  125. utils::Trace < utils::DoubleTime >::trace()
  126. << utils::TraceElement < utils::DoubleTime >(
  127. true,
  128. dynamic_cast<T*>(this)->path(dynamic_cast<T*>(this)),
  129. t, utils::PUT)
  130. << utils::KernelInfo(external_names[index], true,
  131. get(index).to_string(dynamic_cast < const T* >(this)));
  132. utils::Trace < utils::DoubleTime >::trace().flush();
  133. #endif
  134. }
  135. virtual void restore(AbstractModel < U, V >* model,
  136. const context::State < U >& state)
  137. {
  138. unsigned int index = 0;
  139. for (typename std::vector < std::pair < typename U::type,
  140. Any > >::iterator it = externals.begin();
  141. it != externals.end(); ++it) {
  142. Any& value = it->second;
  143. if (not value.is_null()) {
  144. value.restore < T >(static_cast < T* >(model),
  145. state.get_external(index));
  146. }
  147. ++index;
  148. }
  149. }
  150. virtual void save(const AbstractModel < U, V >* model,
  151. context::State < U >& state) const
  152. {
  153. unsigned int index = 0;
  154. for (typename std::vector < std::pair < typename U::type,
  155. Any > >::const_iterator it = externals.begin();
  156. it != externals.end(); ++it) {
  157. const Any& value = it->second;
  158. if (not value.is_null()) {
  159. state.add_external(index,
  160. value.save < T >(
  161. static_cast < const T* >(model)));
  162. }
  163. ++index;
  164. }
  165. }
  166. unsigned int size() const
  167. { return externals.size(); }
  168. protected:
  169. bool updated;
  170. std::vector < std::pair < typename U::type, Any > > externals;
  171. std::vector < std::string > external_names;
  172. };
  173. #define External(index, var) \
  174. external_(index, std::string(ESCAPEQUOTE(index)), var)
  175. #define Externals(W,L) E_< W >(UNWRAP2 L)
  176. } } // namespace artis kernel
  177. #endif