Panda3D
clientDevice.cxx
1 // Filename: clientDevice.cxx
2 // Created by: drose (25Jan01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 #include "clientDevice.h"
17 #include "clientBase.h"
18 
19 #include "indent.h"
20 
21 TypeHandle ClientDevice::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: ClientDevice::Constructor
25 // Access: Protected
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 ClientDevice::
29 ClientDevice(ClientBase *client, TypeHandle device_type,
30  const string &device_name) :
31  _client(client),
32  _device_type(device_type),
33  _device_name(device_name)
34 {
35  // We have to explicitly ref the client pointer, since we can't use
36  // a PT(ClientBase) for circular include reasons.
37  _client->ref();
38  _is_connected = false;
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: ClientDevice::Destructor
43 // Access: Public, Virtual
44 // Description: We don't actually call disconnect() at the
45 // ClientDevice level destructor, because by the time
46 // we get here we're already partly destructed.
47 // Instead, we should call disconnect() from each
48 // specific kind of derived class.
49 ////////////////////////////////////////////////////////////////////
52  nassertv(!_is_connected);
53 
54  // And now we explicitly unref the client pointer.
55  unref_delete(_client);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: ClientDevice::disconnect
60 // Access: Public
61 // Description: Disconnects the ClientDevice from its ClientBase
62 // object. The device will stop receiving
63 // updates.
64 //
65 // Normally, you should not need to call this explicitly
66 // (and it is probably a mistake to do so); it will
67 // automatically be called when the ClientDevice object
68 // destructs.
69 //
70 // The lock should *not* be held while this call is
71 // made; it will explicitly grab the lock itself.
72 ////////////////////////////////////////////////////////////////////
73 void ClientDevice::
75  if (_is_connected) {
76  acquire();
77  bool disconnected =
78  _client->disconnect_device(_device_type, _device_name, this);
79  _is_connected = false;
80  unlock();
81  nassertv(disconnected);
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: ClientDevice::poll
87 // Access: Public
88 // Description: Causes the connected ClientBase to poll all of its
89 // clients, if necessary. This will be a no-op if the
90 // client is running in forked mode, or if it has
91 // already polled everything this frame.
92 //
93 // This should generally be called before accessing the
94 // data in this ClientDevice to ensure that it is fresh.
95 ////////////////////////////////////////////////////////////////////
96 void ClientDevice::
97 poll() {
98  _client->poll();
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: ClientDevice::output
103 // Access: Public, Virtual
104 // Description:
105 ////////////////////////////////////////////////////////////////////
106 void ClientDevice::
107 output(ostream &out) const {
108  out << get_type() << " " << get_device_name();
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: ClientDevice::write
113 // Access: Public, Virtual
114 // Description:
115 ////////////////////////////////////////////////////////////////////
116 void ClientDevice::
117 write(ostream &out, int indent_level) const {
118  indent(out, indent_level) << *this << "\n";
119 }
void unlock()
Releases the mutex associated with this particular device.
Definition: clientDevice.I:90
void disconnect()
Disconnects the ClientDevice from its ClientBase object.
const string & get_device_name() const
Returns the device name reported to the ClientBase.
Definition: clientDevice.I:62
virtual ~ClientDevice()
We don&#39;t actually call disconnect() at the ClientDevice level destructor, because by the time we get ...
void ref() const
Explicitly increments the reference count.
An abstract base class for a family of client device interfaces–including trackers, buttons, dials, and other analog inputs.
Definition: clientBase.h:47
void acquire()
Grabs the mutex associated with this particular device.
Definition: clientDevice.I:76
void poll()
Causes the connected ClientBase to poll all of its clients, if necessary.
bool poll()
Initiates a poll of the client devices, if we are not forked and if we have not already polled this f...
Definition: clientBase.I:38
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85