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
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* nl_factory.h
*
*
*/
#ifndef NLFACTORY_H_
#define NLFACTORY_H_
#include "plib/palloc.h"
#include "plib/ptypes.h"
// deprecated!
#define NETLIB_DEVICE_IMPL_DEPRECATED(chip) \
static std::unique_ptr<factory::element_t> NETLIB_NAME(chip ## _c)( \
const pstring &name, const pstring &classname, const pstring &def_param) \
{ \
return std::unique_ptr<factory::element_t>(plib::palloc<factory::device_element_t<NETLIB_NAME(chip)>>(name, classname, def_param, pstring(__FILE__))); \
} \
factory::constructor_ptr_t decl_ ## chip = NETLIB_NAME(chip ## _c);
// the new way ...
#define NETLIB_DEVICE_IMPL(chip, p_name, p_def_param) \
static std::unique_ptr<factory::element_t> NETLIB_NAME(chip ## _c)( \
const pstring &name, const pstring &classname, const pstring &def_param) \
{ \
return std::unique_ptr<factory::element_t>(plib::palloc<factory::device_element_t<NETLIB_NAME(chip)>>(p_name, classname, p_def_param, pstring(__FILE__))); \
} \
factory::constructor_ptr_t decl_ ## chip = NETLIB_NAME(chip ## _c);
#define NETLIB_DEVICE_IMPL_NS(ns, chip) \
static std::unique_ptr<factory::element_t> NETLIB_NAME(chip ## _c)( \
const pstring &name, const pstring &classname, const pstring &def_param) \
{ \
return std::unique_ptr<factory::element_t>(plib::palloc<factory::device_element_t<ns :: NETLIB_NAME(chip)>>(name, classname, def_param, pstring(__FILE__))); \
} \
\
factory::constructor_ptr_t decl_ ## chip = NETLIB_NAME(chip ## _c);
namespace netlist {
class netlist_t;
class device_t;
class setup_t;
namespace factory {
// -----------------------------------------------------------------------------
// net_dev class factory
// -----------------------------------------------------------------------------
class element_t : plib::nocopyassignmove
{
public:
element_t(const pstring &name, const pstring &classname,
const pstring &def_param);
element_t(const pstring &name, const pstring &classname,
const pstring &def_param, const pstring &sourcefile);
virtual ~element_t();
virtual plib::owned_ptr<device_t> Create(netlist_t &anetlist, const pstring &name) = 0;
virtual void macro_actions(netlist_t &anetlist, const pstring &name) {}
const pstring &name() const { return m_name; }
const pstring &classname() const { return m_classname; }
const pstring ¶m_desc() const { return m_def_param; }
const pstring &sourcefile() const { return m_sourcefile; }
private:
pstring m_name; /* device name */
pstring m_classname; /* device class name */
pstring m_def_param; /* default parameter */
pstring m_sourcefile; /* source file */
};
template <class C>
class device_element_t : public element_t
{
public:
device_element_t(const pstring &name, const pstring &classname,
const pstring &def_param)
: element_t(name, classname, def_param) { }
device_element_t(const pstring &name, const pstring &classname,
const pstring &def_param, const pstring &sourcefile)
: element_t(name, classname, def_param, sourcefile) { }
plib::owned_ptr<device_t> Create(netlist_t &anetlist, const pstring &name) override
{
return plib::owned_ptr<device_t>::Create<C>(anetlist, name);
}
};
class list_t : public std::vector<std::unique_ptr<element_t>>
{
public:
explicit list_t(setup_t &m_setup);
~list_t();
template<class device_class>
void register_device(const pstring &name, const pstring &classname,
const pstring &def_param)
{
register_device(std::unique_ptr<element_t>(plib::palloc<device_element_t<device_class>>(name, classname, def_param)));
}
void register_device(std::unique_ptr<element_t> &&factory);
element_t * factory_by_name(const pstring &devname);
template <class C>
bool is_class(element_t *f)
{
return dynamic_cast<device_element_t<C> *>(f) != nullptr;
}
private:
setup_t &m_setup;
};
// -----------------------------------------------------------------------------
// factory_creator_ptr_t
// -----------------------------------------------------------------------------
using constructor_ptr_t = std::unique_ptr<element_t> (*)(const pstring &name, const pstring &classname,
const pstring &def_param);
template <typename T>
std::unique_ptr<element_t> constructor_t(const pstring &name, const pstring &classname,
const pstring &def_param)
{
return std::unique_ptr<element_t>(plib::palloc<device_element_t<T>>(name, classname, def_param));
}
// -----------------------------------------------------------------------------
// factory_lib_entry_t: factory class to wrap macro based chips/elements
// -----------------------------------------------------------------------------
class library_element_t : public element_t
{
public:
library_element_t(setup_t &setup, const pstring &name, const pstring &classname,
const pstring &def_param, const pstring &source)
: element_t(name, classname, def_param, source) { }
plib::owned_ptr<device_t> Create(netlist_t &anetlist, const pstring &name) override;
void macro_actions(netlist_t &anetlist, const pstring &name) override;
private:
};
}
namespace devices {
void initialize_factory(factory::list_t &factory);
}
}
#endif /* NLFACTORY_H_ */
|