/**
* @file artis/kernel/Externals.hpp
* @author See the AUTHORS file
*/
/*
* Copyright (C) 2012-2019 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
#include
#if WIN32
#include
#endif
namespace artis {
namespace kernel {
template
class Externals {
template
struct element {
unsigned int index;
const std::string name;
W T::* var;
element(unsigned int index, const std::string& name, W T::* var)
:
index(index), name(name), var(var) { }
};
public:
Externals()
:updated(false) { }
virtual ~Externals() { }
const Any& get(unsigned int index) const { return externals.at(index).second; }
const std::string& name(unsigned int index) const { return external_names.at(index); }
bool check(typename U::type t) const
{
bool OK = true;
typename std::vector<
std::pair >::const_iterator it = externals.begin();
while (it != externals.end() and OK) {
OK = it->first == t;
++it;
}
return OK;
}
template
void E_(std::initializer_list > list)
{
for (typename std::initializer_list >::iterator it =
list.begin(); it != list.end(); ++it) {
if (externals.size() <= it->index) {
externals.resize(it->index + 1, std::make_pair(-1, Any()));
external_names.resize(it->index + 1, std::string());
}
externals[it->index] = std::make_pair(-1, it->var);
external_names[it->index] = it->name;
#ifdef WITH_TRACE
utils::Trace::trace()
<< utils::TraceElement(
true,
dynamic_cast(this)->path(dynamic_cast(this)),
utils::DoubleTime::null,
utils::EXTERNAL_DECL)
<< utils::KernelInfo(it->name, false);
utils::Trace::trace().flush();
#endif
}
}
template
void external_(unsigned int index, const std::string& name, W T::* var)
{
if (externals.size() <= index) {
externals.resize(index + 1, std::make_pair(-1, Any()));
external_names.resize(index + 1, std::string());
}
externals[index] = std::make_pair(-1, var);
external_names[index] = name;
#ifdef WITH_TRACE
utils::Trace::trace()
<< utils::TraceElement(
true,
dynamic_cast(this)->path(dynamic_cast(this)),
utils::DoubleTime::null,
utils::EXTERNAL_DECL)
<< utils::KernelInfo(name, false);
utils::Trace::trace().flush();
#endif
}
bool is_ready(typename U::type t, unsigned int index) const { return externals.at(index).first == t; }
template
void put(typename U::type t, unsigned int index, W value)
{
if (externals.at(index).first != t) {
Any v = externals.at(index).second;
v.put(static_cast < T* >(this), value);
externals.at(index).first = t;
updated = true;
} else {
Any v = externals.at(index).second;
if (static_cast < const T* >(this)->*(v.get()) != value) {
v.put(static_cast < T* >(this), value);
updated = true;
}
}
#ifdef WITH_TRACE
utils::Trace::trace()
<< utils::TraceElement(
true,
dynamic_cast(this)->path(dynamic_cast(this)),
t, utils::PUT)
<< utils::KernelInfo(external_names[index], true,
get(index).to_string(dynamic_cast < const T* >(this)));
utils::Trace::trace().flush();
#endif
}
virtual void restore(AbstractModel* model,
const context::State& state)
{
unsigned int index = 0;
for (typename std::vector >::iterator it = externals.begin();
it != externals.end(); ++it) {
Any& value = it->second;
if (not value.is_null()) {
value.restore(static_cast < T* >(model),
state.get_external(index));
}
++index;
}
}
virtual void save(const AbstractModel* model,
context::State& state) const
{
unsigned int index = 0;
for (typename std::vector >::const_iterator it = externals.begin();
it != externals.end(); ++it) {
const Any& value = it->second;
if (not value.is_null()) {
state.add_external(index,
value.save(
static_cast < const T* >(model)));
}
++index;
}
}
unsigned int size() const { return externals.size(); }
protected:
bool updated;
std::vector > externals;
std::vector external_names;
};
#define External(index, var) \
external_(index, std::string(ESCAPEQUOTE(index)), var)
#define Externals(W, L) E_< W >(UNWRAP2 L)
}
} // namespace artis kernel
#endif