123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /**
- * @file artis/kernel/Externals.hpp
- * @author See the AUTHORS file
- */
- /*
- * Copyright (C) 2012-2016 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef __ARTIS_KERNEL_EXTERNALS_HPP
- #define __ARTIS_KERNEL_EXTERNALS_HPP
- #include <artis/kernel/State.hpp>
- #include <vector>
- namespace artis { namespace kernel {
- template < typename T, typename U >
- class Externals
- {
- struct Var
- {
- };
- public:
- Externals() : updated(false)
- { }
- virtual ~Externals()
- { }
- bool check(typename U::type t) const
- {
- bool OK = true;
- typename std::vector <
- std::pair < double,
- double T::* > >::const_iterator it = externalsD.begin();
- while (it != externalsD.end() and OK) {
- OK = it->first == t;
- ++it;
- }
- return OK;
- }
- void E(std::initializer_list < std::pair < unsigned int,
- bool T::* > > externals)
- {
- for (typename std::initializer_list < std::pair < unsigned int,
- bool T::* > >::iterator it =
- externals.begin(); it != externals.end(); ++it) {
- if (externalsB.size() <= it->first) {
- externalsB.resize(it->first + 1);
- }
- externalsB[it->first] = std::make_pair(-1, it->second);
- }
- }
- void E(std::initializer_list < std::pair < unsigned int,
- int T::* > > externals)
- {
- for (typename std::initializer_list < std::pair < unsigned int,
- int T::* > >::iterator it =
- externals.begin(); it != externals.end(); ++it) {
- if (externalsI.size() <= it->first) {
- externalsI.resize(it->first + 1);
- }
- externalsI[it->first] = std::make_pair(-1, it->second);
- }
- }
- void E(std::initializer_list < std::pair < unsigned int,
- double T::* > > externals)
- {
- for (typename std::initializer_list < std::pair < unsigned int,
- double T::* > >::iterator it =
- externals.begin(); it != externals.end(); ++it) {
- if (externalsD.size() <= it->first) {
- externalsD.resize(it->first + 1);
- }
- externalsD[it->first] = std::make_pair(-1, it->second);
- }
- }
- void externalB(unsigned int index, bool T::* var)
- {
- if (externalsB.size() <= index) {
- externalsB.resize(index + 1);
- }
- externalsB[index] = std::make_pair(-1, var);
- }
- void externalI(unsigned int index, int T::* var)
- {
- if (externalsI.size() <= index) {
- externalsI.resize(index + 1);
- }
- externalsI[index] = std::make_pair(-1, var);
- }
- void external(unsigned int index, double T::* var)
- {
- if (externalsD.size() <= index) {
- externalsD.resize(index + 1);
- }
- externalsD[index] = std::make_pair(-1, var);
- }
- bool is_ready(typename U::type t, unsigned int index) const
- { return externalsD.at(index).first == t; }
- void put(typename U::type t, unsigned int index, bool value)
- {
- if (externalsB.at(index).first != t) {
- static_cast < T* >(this)->*externalsB.at(index).second = value;
- externalsB.at(index).first = t;
- updated = true;
- } else {
- if (static_cast < T* >(this)->*externalsB.at(index).second !=
- value) {
- static_cast < T* >(this)->*externalsB.at(index).second = value;
- updated = true;
- }
- }
- }
- void put(typename U::type t, unsigned int index, int value)
- {
- if (externalsI.at(index).first != t) {
- static_cast < T* >(this)->*externalsI.at(index).second = value;
- externalsI.at(index).first = t;
- updated = true;
- } else {
- if (static_cast < T* >(this)->*externalsI.at(index).second !=
- value) {
- static_cast < T* >(this)->*externalsI.at(index).second = value;
- updated = true;
- }
- }
- }
- void put(typename U::type t, unsigned int index, double value)
- {
- if (externalsD.at(index).first != t) {
- static_cast < T* >(this)->*externalsD.at(index).second = value;
- externalsD.at(index).first = t;
- updated = true;
- } else {
- if (static_cast < T* >(this)->*externalsD.at(index).second !=
- value) {
- static_cast < T* >(this)->*externalsD.at(index).second = value;
- updated = true;
- }
- }
- }
- protected:
- bool updated;
- std::vector < std::pair < typename U::type, bool T::* > > externalsB;
- std::vector < std::pair < typename U::type, int T::* > > externalsI;
- std::vector < std::pair < typename U::type, double T::* > > externalsD;
- };
- } } // namespace artis kernel
- #endif
|