netlist: Restructered the save state system. (nw)

This change makes state saving contract based. Objects which need to
save state need to have the following template member:

		template <typename ST>
		void save_state(ST &st)
		{
			/* Example */
			st.save_item(m_p,   "m_p");
			st.save_item(m_buf, "m_buf");
		}

This member function is called when the object is passed to the
state manager save function.
This commit is contained in:
couriersud 2020-05-03 23:37:44 +02:00
parent e59af6d5e8
commit b500f2d241
12 changed files with 105 additions and 83 deletions

View File

@ -179,9 +179,8 @@ namespace analog
, m_gB(nlconst::cgmin())
, m_gC(nlconst::cgmin())
, m_V(nlconst::zero())
, m_state_on(*this, "m_state_on", 0)
, m_state_on(*this, "m_state_on", 0u)
{
register_subalias("B", m_RB.P());
register_subalias("E", m_RB.N());
register_subalias("C", m_RC.P());

View File

@ -553,14 +553,13 @@ namespace analog
, m_R(*this, "RI", nlconst::magic(0.1))
, m_V(*this, "V", nlconst::zero())
, m_func(*this,"FUNC", "")
, m_compiled()
, m_compiled(*this, "m_compiled")
, m_funcparam({nlconst::zero()})
{
m_compiled.save_state(*this, "m_compiled");
register_subalias("P", P());
register_subalias("N", N());
if (m_func() != "")
m_compiled.compile(m_func(), std::vector<pstring>({{pstring("T")}}));
m_compiled->compile(m_func(), std::vector<pstring>({{pstring("T")}}));
}
NETLIB_IS_TIMESTEP(m_func() != "")
@ -570,7 +569,7 @@ namespace analog
m_t += step;
m_funcparam[0] = m_t;
this->set_G_V_I(plib::reciprocal(m_R()),
m_compiled.evaluate(m_funcparam),
m_compiled->evaluate(m_funcparam),
nlconst::zero());
}
@ -587,7 +586,7 @@ namespace analog
param_fp_t m_R;
param_fp_t m_V;
param_str_t m_func;
plib::pfunction<nl_fptype> m_compiled;
state_var<plib::pfunction<nl_fptype>> m_compiled;
std::vector<nl_fptype> m_funcparam;
};
@ -602,14 +601,13 @@ namespace analog
, m_t(*this, "m_t", nlconst::zero())
, m_I(*this, "I", nlconst::one())
, m_func(*this,"FUNC", "")
, m_compiled()
, m_compiled(*this, "m_compiled")
, m_funcparam({nlconst::zero()})
{
m_compiled.save_state(*this, "m_compiled");
register_subalias("P", P());
register_subalias("N", N());
if (m_func() != "")
m_compiled.compile(m_func(), std::vector<pstring>({{pstring("T")}}));
m_compiled->compile(m_func(), std::vector<pstring>({{pstring("T")}}));
}
NETLIB_IS_TIMESTEP(m_func() != "")
@ -617,7 +615,7 @@ namespace analog
{
m_t += step;
m_funcparam[0] = m_t;
const nl_fptype I = m_compiled.evaluate(m_funcparam);
const nl_fptype I = m_compiled->evaluate(m_funcparam);
const auto zero(nlconst::zero());
set_mat(zero, zero, -I,
zero, zero, I);
@ -649,7 +647,7 @@ namespace analog
state_var<nl_fptype> m_t;
param_fp_t m_I;
param_str_t m_func;
plib::pfunction<nl_fptype> m_compiled;
state_var<plib::pfunction<nl_fptype>> m_compiled;
std::vector<nl_fptype> m_funcparam;
};

View File

@ -67,10 +67,10 @@
NET_REGISTER_DEVEXT(SYS_DSW, name, pI, p1, p2)
#define SYS_DSW2(name) \
NET_REGISTER_DEVEXT(SYS_DSW2, name)
NET_REGISTER_DEV(SYS_DSW2, name)
#define SYS_COMPD(name) \
NET_REGISTER_DEVEXT(SYS_COMPD, name)
NET_REGISTER_DEV(SYS_COMPD, name)
#define SYS_NOISE_MT_U(name, pSIGMA) \
NET_REGISTER_DEVEXT(SYS_NOISE_MT_U, name, pSIGMA)

View File

@ -127,12 +127,11 @@ namespace devices
, m_feedback(*this, "FB")
, m_Q(*this, "Q")
, m_func(*this,"FUNC", "")
, m_compiled()
, m_compiled(*this, "m_compiled")
, m_funcparam({nlconst::zero()})
{
m_compiled.save_state(*this, "m_compiled");
if (m_func() != "")
m_compiled.compile(m_func(), std::vector<pstring>({{pstring("T")}}));
m_compiled->compile(m_func(), std::vector<pstring>({{pstring("T")}}));
connect(m_feedback, m_Q);
}
//NETLIB_RESETI();
@ -141,7 +140,7 @@ namespace devices
NETLIB_UPDATEI()
{
m_funcparam[0] = exec().time().as_fp<nl_fptype>();
const netlist_time m_inc = netlist_time::from_fp(m_compiled.evaluate(m_funcparam));
const netlist_time m_inc = netlist_time::from_fp(m_compiled->evaluate(m_funcparam));
m_Q.push(!m_feedback(), m_inc);
}
@ -150,7 +149,7 @@ namespace devices
logic_output_t m_Q;
param_str_t m_func;
plib::pfunction<nl_fptype> m_compiled;
state_var<plib::pfunction<nl_fptype>> m_compiled;
std::vector<nl_fptype> m_funcparam;
};
@ -396,9 +395,8 @@ namespace devices
, m_N(*this, "N", 1)
, m_func(*this, "FUNC", "A0")
, m_Q(*this, "Q")
, m_compiled()
, m_compiled(*this, "m_compiled")
{
m_compiled.save_state(*this, "m_compiled");
std::vector<pstring> inps;
for (int i=0; i < m_N(); i++)
{
@ -407,7 +405,7 @@ namespace devices
inps.push_back(inpname);
m_vals.push_back(nlconst::zero());
}
m_compiled.compile(m_func(), inps);
m_compiled->compile(m_func(), inps);
}
protected:
@ -422,7 +420,7 @@ namespace devices
{
m_vals[i] = (*m_I[i])();
}
m_Q.push(m_compiled.evaluate(m_vals));
m_Q.push(m_compiled->evaluate(m_vals));
}
private:
@ -432,7 +430,7 @@ namespace devices
std::vector<unique_pool_ptr<analog_input_t>> m_I;
std::vector<nl_fptype> m_vals;
plib::pfunction<nl_fptype> m_compiled;
state_var<plib::pfunction<nl_fptype>> m_compiled;
};
@ -641,10 +639,9 @@ namespace devices
, m_I(*this, "I")
, m_RI(*this, "RI", nlconst::magic(0.1))
, m_sigma(*this, "SIGMA", nlconst::zero())
, m_dis(m_sigma())
, m_mt(*this, "m_mt")
, m_dis(*this, "m_dis",m_sigma())
{
m_mt.save_state(*this, "m_mt");
m_dis.save_state(*this, "m_dis");
register_subalias("1", m_T.P());
register_subalias("2", m_T.N());
@ -654,10 +651,10 @@ namespace devices
NETLIB_UPDATEI()
{
nl_fptype val = m_dis(m_mt);
nl_fptype val = m_dis.var()(m_mt.var());
m_T.change_state([this, val]()
{
m_T.set_G_V_I(plib::reciprocal(m_RI()), val, nlconst::zero());;
m_T.set_G_V_I(plib::reciprocal(m_RI()), val, nlconst::zero());
});
}
@ -671,9 +668,8 @@ namespace devices
logic_input_t m_I;
param_fp_t m_RI;
param_fp_t m_sigma;
engine m_mt;
distribution m_dis;
//std::normal_distribution<nl_fptype> m_dis;
state_var<engine> m_mt;
state_var<distribution> m_dis;
};
} // namespace devices

View File

@ -195,8 +195,8 @@ namespace netlist
, m_queue(*this)
, m_use_stats(false)
{
state.run_state_manager().save_item(this, static_cast<plib::state_manager_t::callback_t &>(m_queue), "m_queue");
state.run_state_manager().save_item(this, m_time, "m_time");
state.save(*this, static_cast<plib::state_manager_t::callback_t &>(m_queue), "netlist", "m_queue");
state.save(*this, m_time, "netlist", "m_time");
}
// ----------------------------------------------------------------------------------------
@ -655,8 +655,8 @@ namespace netlist
detail::net_t::net_t(netlist_state_t &nl, const pstring &aname, core_terminal_t *railterminal)
: netlist_object_t(nl.exec(), aname)
, m_new_Q(*this, "m_new_Q", 0)
, m_cur_Q (*this, "m_cur_Q", 0)
, m_new_Q(*this, "m_new_Q", netlist_sig_t(0))
, m_cur_Q (*this, "m_cur_Q", netlist_sig_t(0))
, m_in_queue(*this, "m_in_queue", queue_status::DELIVERED)
, m_next_scheduled_time(*this, "m_time", netlist_time_ext::zero())
, m_railterminal(railterminal)

View File

@ -333,11 +333,11 @@ namespace netlist
struct state_var
{
public:
template <typename O>
template <typename O, typename... Args>
//! Constructor.
state_var(O &owner, //!< owner must have a netlist() method.
const pstring &name, //!< identifier/name for this state variable
const T &value //!< Initial value after construction
Args&&... args //!< Initial values for construction of O
);
//! Destructor.
@ -359,6 +359,10 @@ namespace netlist
//! Return const value of state variable.
constexpr operator const T & () const noexcept { return m_value; }
//! Return non-const value of state variable.
C14CONSTEXPR T & var() noexcept { return m_value; }
//! Return const value of state variable.
constexpr const T & var() const noexcept { return m_value; }
//! Return non-const value of state variable.
C14CONSTEXPR T & operator ()() noexcept { return m_value; }
//! Return const value of state variable.
constexpr const T & operator ()() const noexcept { return m_value; }
@ -366,6 +370,10 @@ namespace netlist
C14CONSTEXPR T * ptr() noexcept { return &m_value; }
//! Return const pointer to state variable.
constexpr const T * ptr() const noexcept{ return &m_value; }
//! Access state variable by ->.
C14CONSTEXPR T * operator->() noexcept { return &m_value; }
//! Access state variable by const ->.
constexpr const T * operator->() const noexcept{ return &m_value; }
private:
T m_value;
@ -525,11 +533,6 @@ namespace netlist
netlist_t & exec() noexcept { return m_netlist; }
const netlist_t & exec() const noexcept { return m_netlist; }
// State saving support - allows this to be passed
// to generic save_state members
template<typename C>
void save_item(C &state, const pstring &membername, const pstring &itemname);
private:
netlist_t & m_netlist;
@ -1787,12 +1790,6 @@ namespace netlist
return m_netlist.nlstate();
}
template<typename C>
inline void detail::netlist_object_t::save_item(C &state, const pstring &membername, const pstring &itemname)
{
this->state().save(*this, state, name(), membername + "." + itemname);
}
template<class C, typename... Args>
void device_t::create_and_register_subdevice(const pstring &name, unique_pool_ptr<C> &dev, Args&&... args)
{
@ -2041,9 +2038,9 @@ namespace netlist
}
template <typename T>
template <typename O>
state_var<T>::state_var(O &owner, const pstring &name, const T &value)
: m_value(value)
template <typename O, typename... Args>
state_var<T>::state_var(O &owner, const pstring &name, Args&&... args)
: m_value(std::forward<Args>(args)...)
{
owner.state().save(owner, m_value, owner.name(), name);
}

View File

@ -17,7 +17,7 @@
///
/// \brief Version - Minor.
///
#define NL_VERSION_MINOR 12
#define NL_VERSION_MINOR 13
/// \brief Version - Patch level.
///
#define NL_VERSION_PATCHLEVEL 0

View File

@ -164,13 +164,5 @@ namespace netlist
} // namespace netlist
namespace plib {
template<>
inline void state_manager_t::save_item(const void *owner, netlist::netlist_time &state, const pstring &stname)
{
save_state_ptr(owner, stname, datatype_t(sizeof(netlist::netlist_time::internal_type), true, false), 1, state.get_internaltype_ptr());
}
} // namespace plib
#endif // NLTYPES_H_

View File

@ -92,10 +92,10 @@ namespace plib {
///
value_type evaluate(const values_container &values = values_container()) noexcept;
template <typename ST, typename STR>
void save_state(ST &st, const STR &name)
template <typename ST>
void save_state(ST &st)
{
st.save_item(m_lfsr, name, "m_lfsr");
st.save_item(m_lfsr, "m_lfsr");
}
private:

View File

@ -52,11 +52,11 @@ namespace plib
static constexpr T min() noexcept { return static_cast<T>(0); }
static constexpr T max() noexcept { return ~T(0) >> (sizeof(T)*8 - w); }
template <typename ST, typename STR>
void save_state(ST &st, const STR &name)
template <typename ST>
void save_state(ST &st)
{
st.save_item(m_p, name, "index");
st.save_item(m_mt, name, "mt");
st.save_item(m_p, "index");
st.save_item(m_mt, "mt");
}
void seed(T val) noexcept
@ -142,9 +142,10 @@ namespace plib
* constants<FT>::sqrt3() * m_stddev;
}
template <typename ST, typename STR>
void save_state(ST &st, const STR &name)
template <typename ST>
void save_state(ST &st)
{
plib::unused_var(st);
/* no state to save */
}
@ -169,11 +170,11 @@ namespace plib
return m_buf[m_p++];
}
template <typename ST, typename STR>
void save_state(ST &st, const STR &name)
template <typename ST>
void save_state(ST &st)
{
st.save_item(m_p, name, "m_p");
st.save_item(m_buf, name, "m_buf");
st.save_item(m_p, "m_p");
st.save_item(m_buf, "m_buf");
}
private:

View File

@ -97,10 +97,29 @@ public:
state_manager_t() = default;
struct saver_t
{
saver_t(state_manager_t &sm, const void *owner, const pstring &membername)
: m_sm(sm)
, m_owner(owner)
, m_membername(membername)
{ }
template <typename XS>
void save_item(XS &xstate, const pstring &itemname)
{
m_sm.save_item(m_owner, xstate, m_membername + "." + itemname);
}
state_manager_t &m_sm;
const void * m_owner;
const pstring m_membername;
};
template<typename C>
void save_item(const void *owner, C &state, const pstring &stname)
{
save_state_ptr( owner, stname, dtype<C>(), 1, &state);
save_item_dispatch(owner, state, stname);
}
template<typename C, std::size_t N>
@ -173,12 +192,32 @@ public:
protected:
private:
template<typename C>
typename std::enable_if<plib::is_integral<C>::value || std::is_enum<C>::value
|| plib::is_floating_point<C>::value>::type
save_item_dispatch(const void *owner, C &state, const pstring &stname)
{
save_state_ptr( owner, stname, dtype<C>(), 1, &state);
}
template<typename C>
typename std::enable_if<!(plib::is_integral<C>::value || std::is_enum<C>::value
|| plib::is_floating_point<C>::value)>::type
save_item_dispatch(const void *owner, C &state, const pstring &stname)
{
saver_t sav(*this, owner, stname);
state.save_state(sav);
}
entry_t::list_t m_save;
entry_t::list_t m_custom;
};
template<> inline void state_manager_t::save_item(const void *owner, callback_t &state, const pstring &stname)
template<>
inline void state_manager_t::save_item(const void *owner, callback_t &state, const pstring &stname)
{
m_custom.emplace_back(stname, owner, &state);
state.register_state(*this, stname);

View File

@ -27,13 +27,6 @@ namespace plib
const static bool value = sizeof(T) <= sizeof(U);
};
#if 0
template<typename T, typename U>
struct ptime_res {
using type = typename std::conditional<sizeof(T) >= sizeof(U), T, U>::type;
};
#endif
template <typename TYPE, TYPE RES>
struct ptime final
{
@ -165,7 +158,14 @@ namespace plib
constexpr ptime shr(unsigned shift) const noexcept { return ptime(m_time >> shift); }
// for save states ....
#if 0
C14CONSTEXPR internal_type *get_internaltype_ptr() noexcept { return &m_time; }
#endif
template <typename ST>
void save_state(ST &st)
{
st.save_item(m_time, "m_time");
}
static constexpr ptime from_nsec(internal_type ns) noexcept { return ptime(ns, UINT64_C(1000000000)); }
static constexpr ptime from_usec(internal_type us) noexcept { return ptime(us, UINT64_C( 1000000)); }