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