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