Panda3D
|
00001 // Filename: clientDevice.cxx 00002 // Created by: drose (25Jan01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 #include "clientDevice.h" 00017 #include "clientBase.h" 00018 00019 #include "indent.h" 00020 00021 TypeHandle ClientDevice::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: ClientDevice::Constructor 00025 // Access: Protected 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 ClientDevice:: 00029 ClientDevice(ClientBase *client, TypeHandle device_type, 00030 const string &device_name) : 00031 _client(client), 00032 _device_type(device_type), 00033 _device_name(device_name) 00034 { 00035 // We have to explicitly ref the client pointer, since we can't use 00036 // a PT(ClientBase) for circular include reasons. 00037 _client->ref(); 00038 _is_connected = false; 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: ClientDevice::Destructor 00043 // Access: Public, Virtual 00044 // Description: We don't actually call disconnect() at the 00045 // ClientDevice level destructor, because by the time 00046 // we get here we're already partly destructed. 00047 // Instead, we should call disconnect() from each 00048 // specific kind of derived class. 00049 //////////////////////////////////////////////////////////////////// 00050 ClientDevice:: 00051 ~ClientDevice() { 00052 nassertv(!_is_connected); 00053 00054 // And now we explicitly unref the client pointer. 00055 unref_delete(_client); 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: ClientDevice::disconnect 00060 // Access: Public 00061 // Description: Disconnects the ClientDevice from its ClientBase 00062 // object. The device will stop receiving 00063 // updates. 00064 // 00065 // Normally, you should not need to call this explicitly 00066 // (and it is probably a mistake to do so); it will 00067 // automatically be called when the ClientDevice object 00068 // destructs. 00069 // 00070 // The lock should *not* be held while this call is 00071 // made; it will explicitly grab the lock itself. 00072 //////////////////////////////////////////////////////////////////// 00073 void ClientDevice:: 00074 disconnect() { 00075 if (_is_connected) { 00076 acquire(); 00077 bool disconnected = 00078 _client->disconnect_device(_device_type, _device_name, this); 00079 _is_connected = false; 00080 unlock(); 00081 nassertv(disconnected); 00082 } 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: ClientDevice::poll 00087 // Access: Public 00088 // Description: Causes the connected ClientBase to poll all of its 00089 // clients, if necessary. This will be a no-op if the 00090 // client is running in forked mode, or if it has 00091 // already polled everything this frame. 00092 // 00093 // This should generally be called before accessing the 00094 // data in this ClientDevice to ensure that it is fresh. 00095 //////////////////////////////////////////////////////////////////// 00096 void ClientDevice:: 00097 poll() { 00098 _client->poll(); 00099 } 00100 00101 //////////////////////////////////////////////////////////////////// 00102 // Function: ClientDevice::output 00103 // Access: Public, Virtual 00104 // Description: 00105 //////////////////////////////////////////////////////////////////// 00106 void ClientDevice:: 00107 output(ostream &out) const { 00108 out << get_type() << " " << get_device_name(); 00109 } 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: ClientDevice::write 00113 // Access: Public, Virtual 00114 // Description: 00115 //////////////////////////////////////////////////////////////////// 00116 void ClientDevice:: 00117 write(ostream &out, int indent_level) const { 00118 indent(out, indent_level) << *this << "\n"; 00119 }