-
Notifications
You must be signed in to change notification settings - Fork 19
/
DeviceHolder.h
227 lines (193 loc) · 7.72 KB
/
DeviceHolder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/** @file
@brief Header
@date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2016 Razer Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_DeviceHolder_h_GUID_58F7E011_8D87_49A3_FE26_0DB159405E77
#define INCLUDED_DeviceHolder_h_GUID_58F7E011_8D87_49A3_FE26_0DB159405E77
// Internal Includes
#include "ReturnValue.h"
// Library/third-party includes
#include <openvr_driver.h>
// Standard includes
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <utility>
#include <vector>
namespace osvr {
namespace vive {
/// Holds activated vr::ITrackedDeviceServerDriver pointers in an array
/// according to the ID given at their activation time. For best results,
/// leave all activation/deactivation to this class.
class DeviceHolder {
public:
DeviceHolder() = default;
~DeviceHolder() {
if (deactivateOnShutdown_) {
deactivateAll();
}
}
DeviceHolder(DeviceHolder const &) = delete;
DeviceHolder &operator=(DeviceHolder const &) = delete;
/// move constructible
DeviceHolder(DeviceHolder &&other)
: devices_(std::move(other.devices_)),
deactivateOnShutdown_(other.deactivateOnShutdown_) {
other.disableDeactivateOnShutdown();
}
/// move assignment
DeviceHolder &operator=(DeviceHolder &&other) {
if (&other == this) {
return *this;
}
if (deactivateOnShutdown_) {
deactivateAll();
}
devices_ = std::move(other.devices_);
deactivateOnShutdown_ = other.deactivateOnShutdown_;
other.disableDeactivateOnShutdown();
return *this;
}
using IdReturnValue = ReturnValue<std::uint32_t, bool>;
IdReturnValue
addAndActivateDevice(vr::ITrackedDeviceServerDriver *dev) {
/// check to make sure it's not null and not already in there
if (!dev) {
return IdReturnValue::makeError();
}
auto existing = findDevice(dev);
if (existing) {
/// @todo do we return an error or the existing location? This
/// returns the existing location after re-activating.
dev->Activate(existing.value);
return existing;
}
auto newId = static_cast<std::uint32_t>(devices_.size());
devices_.push_back(dev);
dev->Activate(newId);
return IdReturnValue::makeValue(newId);
}
/// Add and activate a device at a reserved id.
IdReturnValue
addAndActivateDeviceAt(vr::ITrackedDeviceServerDriver *dev,
std::uint32_t idx) {
/// check to make sure it's not null and not already in there
if (!dev) {
return IdReturnValue::makeError();
}
auto existing = findDevice(dev);
if (existing && existing.value != idx) {
// if we already found it in there and it's not at the desired
// index...
return IdReturnValue(existing.value, false);
}
if (existing) {
// well, in this case, we might need to just activate it again.
dev->Activate(idx);
return IdReturnValue::makeValue(idx);
}
if (!(idx < devices_.size())) {
// OK, we need to reserve more room.
reserveIds(idx + 1);
}
if (devices_[idx]) {
// there's already somebody else there...
return IdReturnValue::makeError();
}
/// Finally, if we made it through that, it's our turn.
devices_[idx] = dev;
dev->Activate(idx);
return IdReturnValue::makeValue(idx);
}
/// Reserve the first n ids, if not already allocated, for manual
/// allocation. (More like a non-shrinking resize() than reserve() in
/// c++ standard container terminology, so don't let that confuse you)
/// @return true if this action resulted in any actual addition of null
/// entries to the device list.
bool reserveIds(std::uint32_t n) {
if (devices_.size() < n) {
devices_.resize(n, nullptr);
return true;
}
return false;
}
bool hasDeviceAt(const std::uint32_t idx) const {
return (idx < devices_.size()) && (nullptr != devices_[idx]);
}
vr::ITrackedDeviceServerDriver &
getDevice(const std::uint32_t idx) const {
return *devices_.at(idx);
}
/// @return a (found, index) pair for a non-null device pointer.
IdReturnValue findDevice(vr::ITrackedDeviceServerDriver *dev) {
auto it = std::find(devices_.cbegin(), devices_.cend(), dev);
if (devices_.cend() == it) {
return IdReturnValue::makeError();
}
return IdReturnValue::makeValue(static_cast<std::uint32_t>(
std::distance(devices_.cbegin(), it)));
}
/// @return the number of allocated/reserved ids
std::size_t reservedIds() const { return devices_.size(); }
/// @return the number of active devices (that is, those that were not
/// deactivated through this container and thus non-nullptr)
std::size_t numDevices() const {
return std::count_if(begin(devices_), end(devices_),
[](vr::ITrackedDeviceServerDriver *dev) {
return dev != nullptr;
});
}
/// @return false if there was no device there to deactivate.
bool deactivate(std::uint32_t idx) {
if (idx < devices_.size() && devices_[idx]) {
devices_[idx]->Deactivate();
devices_[idx] = nullptr;
return true;
}
return false;
}
void deactivateAll() {
for (auto &dev : devices_) {
if (dev) {
dev->Deactivate();
dev = nullptr;
}
}
}
/// Set whether all devices should be deactivated on shutdown - defaults
/// to true, so you might just want to set to false if, for instance,
/// you deactivate and power off the devices on shutdown yourself.
void disableDeactivateOnShutdown() { deactivateOnShutdown_ = false; }
bool shouldDeactivateOnShutdown() const {
return deactivateOnShutdown_;
}
/// Seriously, just avoid using this function and it will be OK.
std::vector<vr::ITrackedDeviceServerDriver *> const &
rawDeviceVectorAccess_NOT_RECOMMENDED_TODO_FIXME() const {
return devices_;
}
private:
bool deactivateOnShutdown_ = true;
std::vector<vr::ITrackedDeviceServerDriver *> devices_;
};
} // namespace vive
} // namespace osvr
#endif // INCLUDED_DeviceHolder_h_GUID_58F7E011_8D87_49A3_FE26_0DB159405E77