123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /**
- * @file artis/kernel/States.hpp
- * @author See the AUTHORS file
- */
- /*
- * Copyright (C) 2012-2017 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_STATES_HPP
- #define __ARTIS_KERNEL_STATES_HPP
- #include <artis/context/State.hpp>
- #include <artis/kernel/Value.hpp>
- #include <vector>
- namespace artis { namespace kernel {
- template < typename T, typename U >
- class States
- {
- template < typename V >
- struct element
- {
- unsigned int index;
- const std::string name;
- V T::* var;
- element(unsigned int index, const std::string& name, V T::* var) :
- index(index), name(name), var(var)
- { }
- };
- public:
- States()
- { }
- virtual ~States()
- { }
- const Any& get(unsigned int index) const
- { return states.at(index); }
- template < typename V >
- void S_(std::initializer_list < element < V > > list)
- {
- for (typename std::initializer_list < element < V > >::iterator it =
- list.begin(); it != list.end(); ++it) {
- if (states.size() <= it->index) {
- states.resize(it->index + 1, Any());
- state_names.resize(it->index + 1, std::string());
- }
- states[it->index] = it->var;
- state_names[it->index] = it->name;
- }
- }
- virtual void restore(const context::State < U >& /* state */)
- {
- // unsigned int index = 0;
- // for (typename std::vector < bool T::* >::iterator it =
- // statesB.begin(); it != statesB.end(); ++it) {
- // state.get_state(index, *it);
- // ++index;
- // }
- // index = 0;
- // for (typename std::vector < int T::* >::iterator it =
- // statesI.begin(); it != statesI.end(); ++it) {
- // state.get_state(index, *it);
- // ++index;
- // }
- // index = 0;
- // for (typename std::vector < double T::* >::iterator it =
- // statesD.begin(); it != statesD.end(); ++it) {
- // state.get_state(index, *it);
- // ++index;
- // }
- }
- virtual void save(context::State < U >& /* state */) const
- {
- // unsigned int index = 0;
- // for (typename std::vector < bool T::* >::const_iterator it =
- // statesB.begin(); it != statesB.end(); ++it) {
- // state.add_state(index, *it);
- // ++index;
- // }
- // index = 0;
- // for (typename std::vector < int T::* >::const_iterator it =
- // statesI.begin(); it != statesI.end(); ++it) {
- // state.add_state(index, *it);
- // ++index;
- // }
- // index = 0;
- // for (typename std::vector < double T::* >::const_iterator it =
- // statesD.begin(); it != statesD.end(); ++it) {
- // state.add_state(index, *it);
- // ++index;
- // }
- }
- template < typename V >
- void state_(unsigned int index, const std::string& name, V T::* var)
- {
- if (states.size() <= index) {
- states.resize(index + 1, Any());
- state_names.resize(index + 1, std::string());
- }
- states[index] = Any(var);
- state_names[index] = name;
- }
- private:
- std::vector < Any > states;
- std::vector < std::string > state_names;
- };
- #define State(index, var) \
- state_(index, std::string(ESCAPEQUOTE(index)), var)
- #define States(V,L) S_< V >(UNWRAP2 L)
- } }
- #endif
|