/** * @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 . */ #ifndef __ARTIS_KERNEL_EXTERNALS_HPP #define __ARTIS_KERNEL_EXTERNALS_HPP #include #include 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