openMSX
Connector.cc
Go to the documentation of this file.
1#include "Connector.hh"
2#include "PlugException.hh"
3#include "Pluggable.hh"
5#include "serialize.hh"
6#include "CliComm.hh"
7
8namespace openmsx {
9
11 std::string name_, std::unique_ptr<Pluggable> dummy_)
12 : pluggingController(pluggingController_)
13 , name(std::move(name_))
14 , dummy(std::move(dummy_))
15 , plugged(dummy.get())
16{
17 pluggingController.registerConnector(*this);
18}
19
21{
22 pluggingController.unregisterConnector(*this);
23}
24
25void Connector::plug(Pluggable& device, EmuTime::param time)
26{
27 device.plug(*this, time);
28 plugged = &device; // not executed if plug fails
29}
30
31void Connector::unplug(EmuTime::param time)
32{
33 plugged->unplug(time);
34 plugged = dummy.get();
35}
36
37template<typename Archive>
38void Connector::serialize(Archive& ar, unsigned /*version*/)
39{
40 std::string plugName;
41 if constexpr (!Archive::IS_LOADER) {
42 if (plugged != dummy.get()) {
43 plugName = plugged->getName();
44 }
45 }
46 ar.serialize("plugName", plugName);
47
48 if constexpr (!Archive::IS_LOADER) {
49 if (!plugName.empty()) {
50 ar.beginSection();
51 ar.serializePolymorphic("pluggable", *plugged);
52 ar.endSection();
53 }
54 } else {
55 if (plugName.empty()) {
56 // was not plugged in
57 plugged = dummy.get();
58 } else if (Pluggable* pluggable =
59 pluggingController.findPluggable(plugName)) {
60 plugged = pluggable;
61 // set connector before loading the pluggable so that
62 // the pluggable can test whether it was connected
63 pluggable->setConnector(this);
64 ar.skipSection(false);
65 try {
66 ar.serializePolymorphic("pluggable", *plugged);
67 } catch (PlugException& e) {
68 pluggingController.getCliComm().printWarning(
69 "Pluggable \"", plugName, "\" failed to replug: ",
70 e.getMessage());
71 pluggable->setConnector(nullptr);
72 plugged = dummy.get();
73 }
74 } else {
75 // was plugged, but we don't have that pluggable anymore
76 pluggingController.getCliComm().printWarning(
77 "Pluggable \"", plugName, "\" was plugged in, "
78 "but is not available anymore on this system, "
79 "so it will be ignored.");
80 ar.skipSection(true);
81 plugged = dummy.get();
82 }
83 }
84}
86
87} // namespace openmsx
void printWarning(std::string_view message)
Definition: CliComm.cc:10
Represents something you can plug devices into.
Definition: Connector.hh:21
void serialize(Archive &ar, unsigned version)
Definition: Connector.cc:38
virtual void unplug(EmuTime::param time)
This unplugs the currently inserted Pluggable from this Connector.
Definition: Connector.cc:31
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:25
Connector(const Connector &)=delete
Thrown when a plug action fails.
void plug(Connector &connector, EmuTime::param time)
This method is called when this pluggable is inserted in a connector.
Definition: Pluggable.cc:19
void unplug(EmuTime::param time)
This method is called when this pluggable is removed from a connector.
Definition: Pluggable.cc:31
void setConnector(Connector *conn)
Definition: Pluggable.hh:58
virtual std::string_view getName() const
Name used to identify this pluggable.
Definition: Pluggable.cc:14
Central administration of Connectors and Pluggables.
void unregisterConnector(Connector &connector)
Pluggable * findPluggable(std::string_view name) const
Return the Pluggable with given name or nullptr if there is none with this name.
void registerConnector(Connector &connector)
Connectors must be (un)registered.
CliComm & getCliComm()
Access to the MSX specific CliComm, so that Connectors can get it.
constexpr double e
Definition: Math.hh:20
This file implemented 3 utility functions:
Definition: Autofire.cc:9
const T & get(const Event &event)
Definition: Event.hh:724
STL namespace.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1021