openMSX
Connector.cc
Go to the documentation of this file.
1 #include "Connector.hh"
2 #include "Pluggable.hh"
3 #include "PluggingController.hh"
4 #include "serialize.hh"
5 #include "CliComm.hh"
6 
7 namespace openmsx {
8 
10  string_ref name_, std::unique_ptr<Pluggable> dummy_)
11  : pluggingController(pluggingController_)
12  , name(name_.str())
13  , dummy(std::move(dummy_))
14 {
15  plugged = dummy.get();
16  pluggingController.registerConnector(*this);
17 }
18 
20 {
21  pluggingController.unregisterConnector(*this);
22 }
23 
25 {
26  device.plug(*this, time);
27  plugged = &device; // not executed if plug fails
28 }
29 
31 {
32  plugged->unplug(time);
33  plugged = dummy.get();
34 }
35 
36 template<typename Archive>
37 void Connector::serialize(Archive& ar, unsigned /*version*/)
38 {
39  std::string plugName;
40  if (!ar.isLoader() && (plugged != dummy.get())) {
41  plugName = plugged->getName();
42  }
43  ar.serialize("plugName", plugName);
44 
45  if (!ar.isLoader()) {
46  if (!plugName.empty()) {
47  ar.beginSection();
48  ar.serializePolymorphic("pluggable", *plugged);
49  ar.endSection();
50  }
51  } else {
52  if (plugName.empty()) {
53  // was not plugged in
54  plugged = dummy.get();
55  } else if (Pluggable* pluggable =
56  pluggingController.findPluggable(plugName)) {
57  plugged = pluggable;
58  // set connector before loading the pluggable so that
59  // the pluggable can test whether it was connected
60  pluggable->setConnector(this);
61  ar.skipSection(false);
62  ar.serializePolymorphic("pluggable", *plugged);
63  } else {
64  // was plugged, but we don't have that pluggable anymore
65  pluggingController.getCliComm().printWarning(
66  "Pluggable \"" + plugName + "\" was plugged in, "
67  "but is not available anymore on this system, "
68  "so it will be ignored.");
69  ar.skipSection(true);
70  plugged = dummy.get();
71  }
72  }
73 }
75 
76 } // namespace openmsx
Represents something you can plug devices into.
Definition: Connector.hh:21
void setConnector(Connector *conn)
Definition: Pluggable.hh:59
void printWarning(string_ref message)
Definition: CliComm.cc:28
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.
Pluggable * findPluggable(string_ref name) const
Return the Pluggable with given name or nullptr if there is none with this name.
CliComm & getCliComm()
Access to the MSX specific CliComm, so that Connectors can get it.
virtual const std::string & getName() const
Name used to identify this pluggable.
Definition: Pluggable.cc:16
void serialize(Archive &ar, unsigned version)
Definition: Connector.cc:37
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
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:802
void unregisterConnector(Connector &connector)
virtual void unplug(EmuTime::param time)
This unplugs the currently inserted Pluggable from this Connector.
Definition: Connector.cc:30
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:24
Connector(PluggingController &pluggingController, string_ref name, std::unique_ptr< Pluggable > dummy)
Creates a new Connector.
Definition: Connector.cc:9