1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// license:BSD-3-Clause
// copyright-holders:Couriersud
///
/// \file state_var.h
///
#ifndef NL_CORE_STATE_VAR_H_
#define NL_CORE_STATE_VAR_H_
#include "../nltypes.h"
#include "../plib/pstring.h"
namespace netlist
{
/// \brief A persistent variable template.
/// Use the state_var template to define a variable whose value is saved.
/// Within a device definition use
///
/// ```
/// NETLIB_OBJECT(abc)
/// {
/// NETLIB_CONSTRUCTOR(abc)
/// , m_var(*this, "myvar", 0)
/// ...
/// state_var<unsigned> m_var;
/// }
/// ```
///
template <typename T>
struct state_var
{
public:
using value_type = T;
template <typename O>
//! 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
);
template <typename O>
//! Constructor.
state_var(O & owner, //!< owner must have a netlist() method.
const pstring &name //!< identifier/name for this state variable
);
state_var(state_var &&) noexcept = delete;
state_var &operator=(state_var &&) noexcept = delete;
//! Destructor.
~state_var() noexcept = default;
//! Copy Constructor removed.
constexpr state_var(const state_var &rhs) = delete;
//! Assignment operator to assign value of a state var.
constexpr state_var &operator=(const state_var &rhs) noexcept
{
if (this != &rhs)
m_value = rhs.m_value;
return *this;
} // OSX doesn't like noexcept
//! Assignment operator to assign value of type T.
constexpr state_var &operator=(const T &rhs) noexcept
{
m_value = rhs;
return *this;
}
//! Assignment move operator to assign value of type T.
// constexpr state_var &operator=(T &&rhs) noexcept { std::swap(m_value,
// rhs); return *this; }
constexpr state_var &operator=(T &&rhs) noexcept
{
m_value = std::move(rhs);
return *this;
}
//! Return non-const value of state variable.
constexpr operator T &() noexcept { return m_value; }
//! Return const value of state variable.
constexpr operator const T &() const noexcept { return m_value; }
//! Return non-const value of state variable.
constexpr 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.
constexpr T &operator()() noexcept { return m_value; }
//! Return const value of state variable.
constexpr const T &operator()() const noexcept { return m_value; }
//! Access state variable by ->.
constexpr T *operator->() noexcept { return &m_value; }
//! Access state variable by const ->.
constexpr const T *operator->() const noexcept { return &m_value; }
//! Access state variable by *.
constexpr T *operator*() noexcept { return &m_value; }
//! Access state variable by const *.
constexpr const T *operator*() const noexcept { return &m_value; }
private:
T m_value;
};
/// \brief A persistent array template.
/// Use this state_var template to define an array whose contents are
/// saved. Please refer to \ref state_var.
///
/// \tparam C container class to use.
template <typename C>
struct state_container : public C
{
public:
using value_type = typename C::value_type;
//! Constructor.
template <typename O>
state_container(O & owner, //!< owner must have a netlist() method.
const pstring & name, //!< identifier/name for this state variable
const value_type &value //!< Initial value after construction
);
//! Constructor.
template <typename O>
state_container(O & owner, //!< owner must have a netlist() method.
const pstring & name, //!< identifier/name for this state variable
std::size_t n, //!< number of elements to allocate
const value_type &value //!< Initial value after construction
);
//! Copy Constructor.
state_container(const state_container &rhs) noexcept = default;
//! Destructor.
~state_container() noexcept = default;
//! Move Constructor.
state_container(state_container &&rhs) noexcept = default;
state_container &operator=(
const state_container &rhs) noexcept = default;
state_container &operator=(state_container &&rhs) noexcept = default;
};
// -----------------------------------------------------------------------------
// State variables - predefined and c++11 non-optional
// -----------------------------------------------------------------------------
/// \brief predefined state variable type for uint8_t
using state_var_u8 = state_var<std::uint8_t>;
/// \brief predefined state variable type for int8_t
using state_var_s8 = state_var<std::int8_t>;
/// \brief predefined state variable type for uint32_t
using state_var_u32 = state_var<std::uint32_t>;
/// \brief predefined state variable type for int32_t
using state_var_s32 = state_var<std::int32_t>;
/// \brief predefined state variable type for sig_t
using state_var_sig = state_var<netlist_sig_t>;
template <typename T>
template <typename O>
state_var<T>::state_var(O &owner, const pstring &name, const T &value)
: m_value(value)
{
owner.state().save(owner, m_value, owner.name(), name);
}
template <typename T>
template <typename O>
state_var<T>::state_var(O &owner, const pstring &name)
{
owner.state().save(owner, m_value, owner.name(), name);
}
template <typename C>
template <typename O>
state_container<C>::state_container(O &owner, const pstring &name,
const state_container<C>::value_type &value)
{
owner.state().save(owner, static_cast<C &>(*this), owner.name(), name);
for (std::size_t i = 0; i < this->size(); i++)
(*this)[i] = value;
}
template <typename C>
template <typename O>
state_container<C>::state_container(O &owner, const pstring &name,
std::size_t n, const state_container<C>::value_type &value)
: C(n, value)
{
owner.state().save(owner, static_cast<C &>(*this), owner.name(), name);
}
// -----------------------------------------------------------------------------
// Externals
// -----------------------------------------------------------------------------
extern template struct state_var<std::uint8_t>;
extern template struct state_var<std::uint16_t>;
extern template struct state_var<std::uint32_t>;
extern template struct state_var<std::uint64_t>;
extern template struct state_var<std::int8_t>;
extern template struct state_var<std::int16_t>;
extern template struct state_var<std::int32_t>;
extern template struct state_var<std::int64_t>;
extern template struct state_var<bool>;
} // namespace netlist
namespace plib
{
template <typename X>
struct format_traits<netlist::state_var<X>> : format_traits<X>
{
};
} // namespace plib
#endif // NL_CORE_STATE_VAR_H_
|