Externals.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Macro.hpp>
  24. #include <artis/kernel/State.hpp>
  25. #include <vector>
  26. namespace artis { namespace kernel {
  27. template < typename T, typename U >
  28. class Externals
  29. {
  30. template < typename V >
  31. struct element
  32. {
  33. unsigned int index;
  34. const std::string name;
  35. V T::* var;
  36. element(unsigned int index, const std::string& name, V T::* var) :
  37. index(index), name(name), var(var)
  38. { }
  39. };
  40. public:
  41. Externals() : updated(false)
  42. { }
  43. virtual ~Externals()
  44. { }
  45. bool check(typename U::type t) const
  46. {
  47. bool OK = true;
  48. typename std::vector <
  49. std::pair < double,
  50. Any > >::const_iterator it = externals.begin();
  51. while (it != externals.end() and OK) {
  52. OK = it->first == t;
  53. ++it;
  54. }
  55. return OK;
  56. }
  57. template < typename V >
  58. void E_(std::initializer_list < element < V > > list)
  59. {
  60. for (typename std::initializer_list < element < V > >::iterator it =
  61. list.begin(); it != list.end(); ++it) {
  62. if (externals.size() <= it->index) {
  63. externals.resize(it->index + 1, std::make_pair(-1, Any()));
  64. external_names.resize(it->index + 1, std::string());
  65. }
  66. externals[it->index] = std::make_pair(-1, it->var);
  67. external_names[it->index] = it->name;
  68. }
  69. }
  70. template < typename V >
  71. void external_(unsigned int index, const std::string& name, V T::* var)
  72. {
  73. if (externals.size() <= index) {
  74. externals.resize(index + 1, std::make_pair(-1, Any()));
  75. external_names.resize(index + 1, std::string());
  76. }
  77. externals[index] = std::make_pair(-1, var);
  78. external_names[index] = name;
  79. }
  80. bool is_ready(typename U::type t, unsigned int index) const
  81. { return externals.at(index).first == t; }
  82. template < typename V >
  83. void put(typename U::type t, unsigned int index, V value)
  84. {
  85. if (externals.at(index).first != t) {
  86. Any v = externals.at(index).second;
  87. v.put < T, V >(static_cast < T* >(this), value);
  88. externals.at(index).first = t;
  89. updated = true;
  90. } else {
  91. Any v = externals.at(index).second;
  92. if (static_cast < const T* >(this)->*(v.get < T, V >()) != value) {
  93. v.put < T, V >(static_cast < T* >(this), value);
  94. updated = true;
  95. }
  96. }
  97. }
  98. unsigned int size() const
  99. { return externals.size(); }
  100. protected:
  101. bool updated;
  102. std::vector < std::pair < typename U::type, Any > > externals;
  103. std::vector < std::string > external_names;
  104. };
  105. #define External(index, var) \
  106. external_(index, std::string(ESCAPEQUOTE(index)), var)
  107. #define Externals(V,L) E_< V >(UNWRAP2 L)
  108. } } // namespace artis kernel
  109. #endif