OXYGENHUB
◆ main openmsx / src / Connector.cc
2.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "Connector.hh"
2
3
#include "CliComm.hh"
4
#include "PlugException.hh"
5
#include "Pluggable.hh"
6
#include "PluggingController.hh"
7
#include "serialize.hh"
8
9
namespace openmsx {
10
11
Connector::Connector(PluggingController& pluggingController_,
12
                     std::string name_, std::unique_ptr<Pluggable> dummy_)
13
	: pluggingController(pluggingController_)
14
	, name(std::move(name_))
15
	, dummy(std::move(dummy_))
16
	, plugged(dummy.get())
17
{
18
	pluggingController.registerConnector(*this);
19
}
20
21
Connector::~Connector()
22
{
23
	pluggingController.unregisterConnector(*this);
24
}
25
26
void Connector::plug(Pluggable& device, EmuTime time)
27
{
28
	device.plug(*this, time);
29
	plugged = &device; // not executed if plug fails
30
}
31
32
void Connector::unplug(EmuTime time)
33
{
34
	plugged->unplug(time);
35
	plugged = dummy.get();
36
}
37
38
template<typename Archive>
39
void Connector::serialize(Archive& ar, unsigned /*version*/)
40
{
41
	std::string plugName;
42
	if constexpr (!Archive::IS_LOADER) {
43
		if (plugged != dummy.get()) {
44
			plugName = plugged->getName();
45
		}
46
	}
47
	ar.serialize("plugName", plugName);
48
49
	if constexpr (!Archive::IS_LOADER) {
50
		if (!plugName.empty()) {
51
			ar.beginSection();
52
			ar.serializePolymorphic("pluggable", *plugged);
53
			ar.endSection();
54
		}
55
	} else {
56
		if (plugName.empty()) {
57
			// was not plugged in
58
			plugged = dummy.get();
59
		} else if (Pluggable* pluggable =
60
			       pluggingController.findPluggable(plugName)) {
61
			plugged = pluggable;
62
			// set connector before loading the pluggable so that
63
			// the pluggable can test whether it was connected
64
			pluggable->setConnector(this);
65
			ar.skipSection(false);
66
			try {
67
				ar.serializePolymorphic("pluggable", *plugged);
68
			} catch (PlugException& e) {
69
				pluggingController.getCliComm().printWarning(
70
					"Pluggable \"", plugName, "\" failed to replug: ",
71
					e.getMessage());
72
				pluggable->setConnector(nullptr);
73
				plugged = dummy.get();
74
			}
75
		} else {
76
			// was plugged, but we don't have that pluggable anymore
77
			pluggingController.getCliComm().printWarning(
78
				"Pluggable \"", plugName, "\" was plugged in, "
79
				"but is not available anymore on this system, "
80
				"so it will be ignored.");
81
			ar.skipSection(true);
82
			plugged = dummy.get();
83
		}
84
	}
85
}
86
INSTANTIATE_SERIALIZE_METHODS(Connector);
87
88
} // namespace openmsx