Externals.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file artis/kernel/Externals.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2016 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/State.hpp>
  24. #include <vector>
  25. namespace artis { namespace kernel {
  26. template < typename T, typename U >
  27. class Externals
  28. {
  29. struct Var
  30. {
  31. };
  32. public:
  33. Externals() : updated(false)
  34. { }
  35. virtual ~Externals()
  36. { }
  37. bool check(typename U::type t) const
  38. {
  39. bool OK = true;
  40. typename std::vector <
  41. std::pair < double,
  42. double T::* > >::const_iterator it = externalsD.begin();
  43. while (it != externalsD.end() and OK) {
  44. OK = it->first == t;
  45. ++it;
  46. }
  47. return OK;
  48. }
  49. void E(std::initializer_list < std::pair < unsigned int,
  50. bool T::* > > externals)
  51. {
  52. for (typename std::initializer_list < std::pair < unsigned int,
  53. bool T::* > >::iterator it =
  54. externals.begin(); it != externals.end(); ++it) {
  55. if (externalsB.size() <= it->first) {
  56. externalsB.resize(it->first + 1);
  57. }
  58. externalsB[it->first] = std::make_pair(-1, it->second);
  59. }
  60. }
  61. void E(std::initializer_list < std::pair < unsigned int,
  62. int T::* > > externals)
  63. {
  64. for (typename std::initializer_list < std::pair < unsigned int,
  65. int T::* > >::iterator it =
  66. externals.begin(); it != externals.end(); ++it) {
  67. if (externalsI.size() <= it->first) {
  68. externalsI.resize(it->first + 1);
  69. }
  70. externalsI[it->first] = std::make_pair(-1, it->second);
  71. }
  72. }
  73. void E(std::initializer_list < std::pair < unsigned int,
  74. double T::* > > externals)
  75. {
  76. for (typename std::initializer_list < std::pair < unsigned int,
  77. double T::* > >::iterator it =
  78. externals.begin(); it != externals.end(); ++it) {
  79. if (externalsD.size() <= it->first) {
  80. externalsD.resize(it->first + 1);
  81. }
  82. externalsD[it->first] = std::make_pair(-1, it->second);
  83. }
  84. }
  85. void externalB(unsigned int index, bool T::* var)
  86. {
  87. if (externalsB.size() <= index) {
  88. externalsB.resize(index + 1);
  89. }
  90. externalsB[index] = std::make_pair(-1, var);
  91. }
  92. void externalI(unsigned int index, int T::* var)
  93. {
  94. if (externalsI.size() <= index) {
  95. externalsI.resize(index + 1);
  96. }
  97. externalsI[index] = std::make_pair(-1, var);
  98. }
  99. void external(unsigned int index, double T::* var)
  100. {
  101. if (externalsD.size() <= index) {
  102. externalsD.resize(index + 1);
  103. }
  104. externalsD[index] = std::make_pair(-1, var);
  105. }
  106. bool is_ready(typename U::type t, unsigned int index) const
  107. { return externalsD.at(index).first == t; }
  108. void put(typename U::type t, unsigned int index, bool value)
  109. {
  110. if (externalsB.at(index).first != t) {
  111. static_cast < T* >(this)->*externalsB.at(index).second = value;
  112. externalsB.at(index).first = t;
  113. updated = true;
  114. } else {
  115. if (static_cast < T* >(this)->*externalsB.at(index).second !=
  116. value) {
  117. static_cast < T* >(this)->*externalsB.at(index).second = value;
  118. updated = true;
  119. }
  120. }
  121. }
  122. void put(typename U::type t, unsigned int index, int value)
  123. {
  124. if (externalsI.at(index).first != t) {
  125. static_cast < T* >(this)->*externalsI.at(index).second = value;
  126. externalsI.at(index).first = t;
  127. updated = true;
  128. } else {
  129. if (static_cast < T* >(this)->*externalsI.at(index).second !=
  130. value) {
  131. static_cast < T* >(this)->*externalsI.at(index).second = value;
  132. updated = true;
  133. }
  134. }
  135. }
  136. void put(typename U::type t, unsigned int index, double value)
  137. {
  138. if (externalsD.at(index).first != t) {
  139. static_cast < T* >(this)->*externalsD.at(index).second = value;
  140. externalsD.at(index).first = t;
  141. updated = true;
  142. } else {
  143. if (static_cast < T* >(this)->*externalsD.at(index).second !=
  144. value) {
  145. static_cast < T* >(this)->*externalsD.at(index).second = value;
  146. updated = true;
  147. }
  148. }
  149. }
  150. protected:
  151. bool updated;
  152. std::vector < std::pair < typename U::type, bool T::* > > externalsB;
  153. std::vector < std::pair < typename U::type, int T::* > > externalsI;
  154. std::vector < std::pair < typename U::type, double T::* > > externalsD;
  155. };
  156. } } // namespace artis kernel
  157. #endif