MMDevice
Loading...
Searching...
No Matches
RegisteredDeviceCollection.h
Go to the documentation of this file.
1
2// FILE: RegisteredDeviceCollection.h
3// PROJECT: Micro-Manager
4// SUBSYSTEM: MMDevice - Device adapter kit
5//-----------------------------------------------------------------------------
6// COPYRIGHT: University of California, San Francisco, 2006
7// Board of Regents of the University of Wisconsin System, 2025
8// LICENSE: This file is distributed under the BSD license.
9// License text is included with the source distribution.
10//
11// This file is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty
13// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14//
15// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
16// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
18
19// This header contains internal implementation of device registration.
20// Device adapter code must not use these definitions directly.
21
22#pragma once
23
24#include "MMDeviceConstants.h"
25
26#include <algorithm>
27#include <string>
28#include <vector>
29
30namespace MM {
31namespace internal {
32
33class RegisteredDeviceCollection
34{
35 struct DeviceInfo
36 {
37 std::string name;
39 std::string description;
40 };
41
42 std::vector<DeviceInfo> devices_;
43
44public:
45 void RegisterDevice(const char* deviceName, MM::DeviceType deviceType, const char* deviceDescription)
46 {
47 if (!deviceName)
48 return;
49
50 if (!deviceDescription)
51 // This is a bug; let the programmer know by displaying an ugly string
52 deviceDescription = "(Null description)";
53
54 auto it = std::find_if(devices_.begin(), devices_.end(),
55 [&](const DeviceInfo& dev) { return dev.name == deviceName; });
56 if (it != devices_.end())
57 {
58 // Device with this name already registered
59 // TODO This should be an error
60 return;
61 }
62
63 devices_.push_back(DeviceInfo{ deviceName, deviceType, deviceDescription });
64
65 }
66
67 unsigned GetNumberOfDevices() const
68 {
69 return static_cast<unsigned>(devices_.size());
70 }
71
72 bool GetDeviceName(unsigned deviceIndex, char* name, unsigned bufSize) const
73 {
74 if (deviceIndex >= devices_.size())
75 return false;
76
77 const std::string& deviceName = devices_[deviceIndex].name;
78
79 if (deviceName.size() >= bufSize)
80 return false; // buffer too small, can't truncate the name
81
82 std::snprintf(name, bufSize, "%s", deviceName.c_str());
83 return true;
84 }
85
86 bool GetDeviceType(const char* deviceName, int* type) const
87 {
88 auto it = std::find_if(devices_.begin(), devices_.end(),
89 [&](const DeviceInfo& dev) { return dev.name == deviceName; });
90 if (it == devices_.end())
91 {
92 *type = MM::UnknownType;
93 return false;
94 }
95
96 // Prefer int over enum across DLL boundary so that the module ABI does not
97 // change (somewhat pedantic, but let's be safe).
98 *type = static_cast<int>(it->type);
99
100 return true;
101 }
102
103 bool GetDeviceDescription(const char* deviceName, char* description, unsigned bufSize) const
104 {
105 auto it = std::find_if(devices_.begin(), devices_.end(),
106 [&](const DeviceInfo& dev) { return dev.name == deviceName; });
107 if (it == devices_.end())
108 return false;
109
110 std::snprintf(description, bufSize, "%s", it->description.c_str());
111 return true;
112 }
113};
114
115} // namespace internal
116} // namespace MM
void RegisterDevice(const char *deviceName, MM::DeviceType deviceType, const char *deviceDescription)
Register a device class provided by the device adapter library.
Definition ModuleInterface.cpp:75
MODULE_API unsigned GetNumberOfDevices()
Definition ModuleInterface.cpp:55
MODULE_API bool GetDeviceName(unsigned deviceIndex, char *name, unsigned bufLen)
Definition ModuleInterface.cpp:60
MODULE_API bool GetDeviceDescription(const char *deviceName, char *description, unsigned bufLen)
Definition ModuleInterface.cpp:70
MODULE_API bool GetDeviceType(const char *deviceName, int *type)
Definition ModuleInterface.cpp:65
Definition CameraImageMetadata.h:25
DeviceType
Definition MMDeviceConstants.h:236
@ UnknownType
Definition MMDeviceConstants.h:237