Panda3D
clientDevice.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file clientDevice.cxx
10 * @author drose
11 * @date 2001-01-25
12 */
13
14#include "clientDevice.h"
15#include "clientBase.h"
16
17#include "indent.h"
18
19TypeHandle ClientDevice::_type_handle;
20
21/**
22 *
23 */
24ClientDevice::
25ClientDevice(ClientBase *client, TypeHandle device_type,
26 const std::string &device_name) :
27 InputDevice(device_name, DeviceClass::unknown),
28 _client(client),
29 _device_type(device_type)
30{
31 // We have to explicitly ref the client pointer, since we can't use a
32 // PT(ClientBase) for circular include reasons.
33 _client->ref();
34}
35
36/**
37 * We don't actually call disconnect() at the ClientDevice level destructor,
38 * because by the time we get here we're already partly destructed. Instead,
39 * we should call disconnect() from each specific kind of derived class.
40 */
43 nassertv(!is_connected());
44
45 // And now we explicitly unref the client pointer.
46 unref_delete(_client);
47}
48
49/**
50 * Disconnects the ClientDevice from its ClientBase object. The device will
51 * stop receiving updates.
52 *
53 * Normally, you should not need to call this explicitly (and it is probably a
54 * mistake to do so); it will automatically be called when the ClientDevice
55 * object destructs.
56 *
57 * The lock should *not* be held while this call is made; it will explicitly
58 * grab the lock itself.
59 */
61disconnect() {
62 if (is_connected()) {
63 bool disconnected =
64 _client->disconnect_device(_device_type, get_name(), this);
65 set_connected(false);
66 nassertv(disconnected);
67 }
68}
69
70/**
71 * Causes the connected ClientBase to poll all of its clients, if necessary.
72 * This will be a no-op if the client is running in forked mode, or if it has
73 * already polled everything this frame.
74 *
75 * This should generally be called before accessing the data in this
76 * ClientDevice to ensure that it is fresh.
77 */
79do_poll() {
80 _client->poll();
81}
82
83/**
84 *
85 */
87output(std::ostream &out) const {
88 out << get_type() << " " << get_name();
89}
90
91/**
92 *
93 */
94void ClientDevice::
95write(std::ostream &out, int indent_level) const {
96 indent(out, indent_level) << *this << "\n";
97}
An abstract base class for a family of client device interfaces–including trackers,...
Definition: clientBase.h:43
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:29
virtual void output(std::ostream &out) const
Writes a one-line string describing the device.
void disconnect()
Disconnects the ClientDevice from its ClientBase object.
virtual void do_poll() final
Causes the connected ClientBase to poll all of its clients, if necessary.
virtual ~ClientDevice()
We don't actually call disconnect() at the ClientDevice level destructor, because by the time we get ...
This is a structure representing a single input device.
Definition: inputDevice.h:53
is_connected
Returns true if the device is still connected and able to receive data, false otherwise.
Definition: inputDevice.h:244
get_name
Returns a human-readable name for the device.
Definition: inputDevice.h:228
void set_connected(bool connected)
Called to indicate that the device has been disconnected or connected from its host.
Definition: inputDevice.I:383
void ref() const
Explicitly increments the reference count.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void unref_delete(RefCountType *ptr)
This global helper function will unref the given ReferenceCount object, and if the reference count re...