blob: 304f8b49f7037e4e0bb8b0b5fb81c5acb46c852c (
plain)
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
|
/*******************************************************************************
* Copyright 2009-2016 Jörg Müller
*
* 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.
******************************************************************************/
#include "devices/DeviceManager.h"
#include "devices/IDeviceFactory.h"
#include "devices/IDevice.h"
#include "devices/I3DDevice.h"
#include <limits>
#include <string>
#include <algorithm>
AUD_NAMESPACE_BEGIN
std::unordered_map<std::string, std::shared_ptr<IDeviceFactory>> DeviceManager::m_factories;
std::shared_ptr<IDevice> DeviceManager::m_device;
void DeviceManager::registerDevice(std::string name, std::shared_ptr<IDeviceFactory> factory)
{
m_factories[name] = factory;
}
std::shared_ptr<IDeviceFactory> DeviceManager::getDeviceFactory(std::string name)
{
auto it = m_factories.find(name);
if(it == m_factories.end())
return nullptr;
return it->second;
}
std::shared_ptr<IDeviceFactory> DeviceManager::getDefaultDeviceFactory()
{
int min = std::numeric_limits<int>::min();
std::shared_ptr<IDeviceFactory> result;
for(auto factory : m_factories)
{
if(factory.second->getPriority() >= min)
{
result = factory.second;
min = result->getPriority();
}
}
return result;
}
void DeviceManager::setDevice(std::shared_ptr<IDevice> device)
{
m_device = device;
}
void DeviceManager::openDevice(std::string name)
{
setDevice(getDeviceFactory(name)->openDevice());
}
void DeviceManager::openDefaultDevice()
{
setDevice(getDefaultDeviceFactory()->openDevice());
}
void DeviceManager::releaseDevice()
{
m_device = nullptr;
}
std::shared_ptr<IDevice> DeviceManager::getDevice()
{
return m_device;
}
std::shared_ptr<I3DDevice> DeviceManager::get3DDevice()
{
return std::dynamic_pointer_cast<I3DDevice>(m_device);
}
std::vector<std::string> DeviceManager::getAvailableDeviceNames()
{
struct DeviceNamePriority {
std::string name;
int priority;
};
std::vector<DeviceNamePriority> devices;
devices.reserve(m_factories.size());
for(const auto& pair : m_factories)
devices.push_back({pair.first, pair.second->getPriority()});
auto sort = [](const DeviceNamePriority& lhs, const DeviceNamePriority& rhs){
return lhs.priority > rhs.priority;
};
std::sort(devices.begin(), devices.end(), sort);
std::vector<std::string> names;
names.reserve(devices.size());
for(const auto& device : devices)
names.push_back(device.name);
return names;
}
AUD_NAMESPACE_END
|