Externals.hpp 6.5 KB

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