Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_DeviceHandle.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_DeviceHandle.cpp
4 Content : Implementation of device handle class
5 Created : February 5, 2013
6 Authors : Lee Cooper
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 *************************************************************************************/
26 
27 #include "OVR_DeviceHandle.h"
28 
29 #include "OVR_DeviceImpl.h"
30 
31 namespace OVR {
32 
33 //-------------------------------------------------------------------------------------
34 // ***** DeviceHandle
35 
37 {
38  if (pImpl)
39  pImpl->AddRef();
40 }
41 
42 DeviceHandle::DeviceHandle(const DeviceHandle& src) : pImpl(src.pImpl)
43 {
44  if (pImpl)
45  pImpl->AddRef();
46 }
47 
49 {
50  if (pImpl)
51  pImpl->Release();
52 }
53 
55 {
56  if (src.pImpl)
57  src.pImpl->AddRef();
58  if (pImpl)
59  pImpl->Release();
60  pImpl = src.pImpl;
61 }
62 
64 {
65  if (pImpl && pImpl->pDevice)
66  {
67  pImpl->pDevice->AddRef();
68  return pImpl->pDevice;
69  }
70  return NULL;
71 }
72 
73 // Returns true, if the handle contains the same device ptr
74 // as specified in the parameter.
76 {
77  return (pdev && pImpl && pImpl->pDevice) ?
78  pImpl->pDevice == pdev : false;
79 }
80 
82 {
83  return pImpl ? pImpl->Type : Device_None;
84 }
85 
87 {
88  return pImpl ? pImpl->GetDeviceInfo(info) : false;
89 }
91 {
92  // This isn't "atomically safe", but the function only returns the
93  // recent state that may change.
94  return pImpl ? (pImpl->Enumerated && pImpl->pLock->pManager) : false;
95 }
96 
98 {
99  return pImpl ? (pImpl->pDevice != 0) : false;
100 }
101 
103 {
104  if (!pImpl)
105  return 0;
106 
107  DeviceBase* device = 0;
108  Ptr<DeviceManagerImpl> manager= 0;
109 
110  // Since both manager and device pointers can only be destroyed during a lock,
111  // hold it while checking for availability.
112  // AddRef to manager so that it doesn't get released on us.
113  {
114  Lock::Locker deviceLockScope(pImpl->GetLock());
115 
116  if (pImpl->pDevice)
117  {
118  pImpl->pDevice->AddRef();
119  return pImpl->pDevice;
120  }
121  manager = pImpl->GetManagerImpl();
122  }
123 
124  if (manager)
125  {
126  if (manager->GetThreadId() != OVR::GetCurrentThreadId())
127  {
128  // Queue up a CreateDevice request. This fills in '&device' with AddRefed value,
129  // or keep it at null.
130  manager->GetThreadQueue()->PushCallAndWaitResult(
132  &device, pImpl, (DeviceBase*)0);
133  }
134  else
135  device = manager->CreateDevice_MgrThread(pImpl, (DeviceBase*)0);
136  }
137  return device;
138 }
139 
141 {
142  if (pImpl)
143  {
144  pImpl->Release();
145  pImpl = 0;
146  }
147 }
148 
150 {
151  if (GetType() == Device_None)
152  return false;
153 
154  Ptr<DeviceManagerImpl> managerKeepAlive;
155  Lock::Locker lockScope(pImpl->GetLock());
156 
157  DeviceCreateDesc* next = pImpl;
158  // If manager was destroyed, we get removed from the list.
159  if (!pImpl->pNext)
160  return false;
161 
162  managerKeepAlive = next->GetManagerImpl();
163  OVR_ASSERT(managerKeepAlive);
164 
165  do {
166  next = next->pNext;
167 
168  if (managerKeepAlive->Devices.IsNull(next))
169  {
170  pImpl->Release();
171  pImpl = 0;
172  return false;
173  }
174 
175  } while(!args.MatchRule(next->Type, next->Enumerated));
176 
177  next->AddRef();
178  pImpl->Release();
179  pImpl = next;
180 
181  return true;
182 }
183 
184 } // namespace OVR
185 
Lock * GetLock() const
DeviceCreateDesc * pImpl
#define NULL
virtual void AddRef()
bool GetDeviceInfo(DeviceInfo *info) const
DeviceType GetType() const
OVR_FORCE_INLINE C * GetPtr() const
Definition: OVR_RefCount.h:506
Ptr< DeviceManagerLock > pLock
DeviceManagerImpl * GetManagerImpl() const
const DeviceType Type
bool IsDevice(DeviceBase *) const
virtual bool GetDeviceInfo(DeviceInfo *info) const =0
#define OVR_ASSERT(p)
void operator=(const DeviceHandle &src)
bool IsAvailable() const
DeviceBase * GetDevice_AddRef() const
bool MatchRule(DeviceType type, bool available) const
Definition: OVR_Device.h:175
bool enumerateNext(const DeviceEnumerationArgs &args)
bool IsCreated() const
ThreadId GetCurrentThreadId()
DeviceBase * CreateDevice()
DeviceBase * CreateDevice_MgrThread(DeviceCreateDesc *createDesc, DeviceBase *parent=0)