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 
24 const std::string& Connector::getName() const
25 {
26  return name;
27 }
28 
30 {
31  device.plug(*this, time);
32  plugged = &device; // not executed if plug fails
33 }
34 
36 {
37  plugged->unplug(time);
38  plugged = dummy.get();
39 }
40 
42 {
43  return *plugged;
44 }
45 
46 template<typename Archive>
47 void Connector::serialize(Archive& ar, unsigned /*version*/)
48 {
49  std::string plugName;
50  if (!ar.isLoader() && (plugged != dummy.get())) {
51  plugName = plugged->getName();
52  }
53  ar.serialize("plugName", plugName);
54 
55  if (!ar.isLoader()) {
56  if (!plugName.empty()) {
57  ar.beginSection();
58  ar.serializePolymorphic("pluggable", *plugged);
59  ar.endSection();
60  }
61  } else {
62  if (plugName.empty()) {
63  // was not plugged in
64  plugged = dummy.get();
65  } else if (Pluggable* pluggable =
66  pluggingController.findPluggable(plugName)) {
67  plugged = pluggable;
68  // set connector before loading the pluggable so that
69  // the pluggable can test whether it was connected
70  pluggable->setConnector(this);
71  ar.skipSection(false);
72  ar.serializePolymorphic("pluggable", *plugged);
73  } else {
74  // was plugged, but we don't have that pluggable anymore
75  pluggingController.getCliComm().printWarning(
76  "Pluggable \"" + plugName + "\" was plugged in, "
77  "but is not available anymore on this system, "
78  "so it will be ignored.");
79  ar.skipSection(true);
80  plugged = dummy.get();
81  }
82  }
83 }
85 
86 } // namespace openmsx
Represents something you can plug devices into.
Definition: Connector.hh:21
virtual ~Connector()
Definition: Connector.cc:19
void setConnector(Connector *conn)
Definition: Pluggable.cc:53
void printWarning(string_ref message)
Definition: CliComm.cc:28
const std::string & getName() const
Name that identifies this connector.
Definition: Connector.cc:24
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:20
void serialize(Archive &ar, unsigned version)
Definition: Connector.cc:47
void unplug(EmuTime::param time)
This method is called when this pluggable is removed from a conector.
Definition: Pluggable.cc:38
void plug(Connector &connector, EmuTime::param time)
This method is called when this pluggable is inserted in a connector.
Definition: Pluggable.cc:26
#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:35
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:29
Pluggable & getPlugged() const
Returns the Pluggable currently plugged in.
Definition: Connector.cc:41
Connector(PluggingController &pluggingController, string_ref name, std::unique_ptr< Pluggable > dummy)
Creates a new Connector.
Definition: Connector.cc:9