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  std::string name_, std::unique_ptr<Pluggable> dummy_)
12  : pluggingController(pluggingController_)
13  , name(std::move(name_))
14  , dummy(std::move(dummy_))
15 {
16  plugged = dummy.get();
17  pluggingController.registerConnector(*this);
18 }
19 
21 {
22  pluggingController.unregisterConnector(*this);
23 }
24 
25 void Connector::plug(Pluggable& device, EmuTime::param time)
26 {
27  device.plug(*this, time);
28  plugged = &device; // not executed if plug fails
29 }
30 
31 void Connector::unplug(EmuTime::param time)
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
serialize.hh
openmsx::Pluggable::getName
virtual const std::string & getName() const
Name used to identify this pluggable.
Definition: Pluggable.cc:16
Pluggable.hh
openmsx::PlugException
Thrown when a plug action fails.
Definition: PlugException.hh:11
PlugException.hh
openmsx::Pluggable::plug
void plug(Connector &connector, EmuTime::param time)
This method is called when this pluggable is inserted in a connector.
Definition: Pluggable.cc:22
PluggingController.hh
openmsx::Pluggable
Definition: Pluggable.hh:12
openmsx::Connector::serialize
void serialize(Archive &ar, unsigned version)
Definition: Connector.cc:38
openmsx::Pluggable::unplug
void unplug(EmuTime::param time)
This method is called when this pluggable is removed from a conector.
Definition: Pluggable.cc:34
openmsx::CliComm::printWarning
void printWarning(std::string_view message)
Definition: CliComm.cc:10
openmsx::PluggingController::findPluggable
Pluggable * findPluggable(std::string_view name) const
Return the Pluggable with given name or nullptr if there is none with this name.
Definition: PluggingController.cc:217
openmsx::Connector::Connector
Connector(const Connector &)=delete
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::Pluggable::setConnector
void setConnector(Connector *conn)
Definition: Pluggable.hh:58
openmsx::Connector::plug
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:25
openmsx::PluggingController::getCliComm
CliComm & getCliComm()
Access to the MSX specific CliComm, so that Connectors can get it.
Definition: PluggingController.cc:232
openmsx::Connector::unplug
virtual void unplug(EmuTime::param time)
This unplugs the currently inserted Pluggable from this Connector.
Definition: Connector.cc:31
openmsx::PluggingController::unregisterConnector
void unregisterConnector(Connector &connector)
Definition: PluggingController.cc:56
Connector.hh
openmsx::PluggingController::registerConnector
void registerConnector(Connector &connector)
Connectors must be (un)registered.
Definition: PluggingController.cc:50
openmsx::Connector
Represents something you can plug devices into.
Definition: Connector.hh:21
openmsx::Connector::~Connector
~Connector()
Definition: Connector.cc:20
CliComm.hh
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx::PluggingController
Central administration of Connectors and Pluggables.
Definition: PluggingController.hh:22
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5