Externals.hpp 3.7 KB

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