From 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d Mon Sep 17 00:00:00 2001 From: Jef Date: Tue, 24 Sep 2024 14:54:57 +0200 Subject: Initial community commit --- Src/devices/deviceSupportedCommandEnum.cpp | 172 +++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Src/devices/deviceSupportedCommandEnum.cpp (limited to 'Src/devices/deviceSupportedCommandEnum.cpp') diff --git a/Src/devices/deviceSupportedCommandEnum.cpp b/Src/devices/deviceSupportedCommandEnum.cpp new file mode 100644 index 00000000..9466f378 --- /dev/null +++ b/Src/devices/deviceSupportedCommandEnum.cpp @@ -0,0 +1,172 @@ +#include "main.h" +#include "./deviceSupportedCommandEnum.h" +#include + +DeviceSupportedCommandEnum::DeviceSupportedCommandEnum() + : ref(1), commands(NULL), count(0), cursor(0) +{ +} + +DeviceSupportedCommandEnum::~DeviceSupportedCommandEnum() +{ + if (NULL != commands) + { + while(count--) + commands[count]->Release(); + } +} + +HRESULT DeviceSupportedCommandEnum::CreateInstance(ifc_devicesupportedcommand **commands, size_t count, + DeviceSupportedCommandEnum **instance) +{ + size_t index, size; + void *storage; + ifc_devicesupportedcommand *c; + DeviceSupportedCommandEnum *enumerator; + + + if (NULL == instance) + return E_POINTER; + + *instance = NULL; + + size = sizeof(DeviceSupportedCommandEnum) + (sizeof(ifc_devicesupportedcommand**) * count); + storage = calloc(1, size); + if (NULL == storage) + return E_OUTOFMEMORY; + + enumerator = new(storage) DeviceSupportedCommandEnum(); + if (NULL == enumerator) + { + free(storage); + return E_FAIL; + } + + enumerator->commands = (ifc_devicesupportedcommand**)(((BYTE*)enumerator) + sizeof(DeviceSupportedCommandEnum)); + + for (index = 0; index < count; index++) + { + c = commands[index]; + if (NULL != c) + { + enumerator->commands[enumerator->count] = c; + c->AddRef(); + enumerator->count++; + } + } + + *instance = enumerator; + return S_OK; +} + + + +size_t DeviceSupportedCommandEnum::AddRef() +{ + return InterlockedIncrement((LONG*)&ref); +} + +size_t DeviceSupportedCommandEnum::Release() +{ + if (0 == ref) + return ref; + + LONG r = InterlockedDecrement((LONG*)&ref); + if (0 == r) + delete(this); + + return r; +} + +int DeviceSupportedCommandEnum::QueryInterface(GUID interface_guid, void **object) +{ + if (NULL == object) return E_POINTER; + + if (IsEqualIID(interface_guid, IFC_DeviceSupportedCommandEnum)) + *object = static_cast(this); + else + { + *object = NULL; + return E_NOINTERFACE; + } + + if (NULL == *object) + return E_UNEXPECTED; + + AddRef(); + return S_OK; +} + +HRESULT DeviceSupportedCommandEnum::Next(ifc_devicesupportedcommand **objects, size_t bufferMax, size_t *fetched) +{ + size_t available, copied, index; + ifc_devicesupportedcommand **source; + + if (NULL == objects) + return E_POINTER; + + if (0 == bufferMax) + return E_INVALIDARG; + + if (cursor >= count) + { + if (NULL != fetched) + *fetched = 0; + + return S_FALSE; + } + + available = count - cursor; + copied = ((available > bufferMax) ? bufferMax : available); + + source = commands + cursor; + CopyMemory(objects, source, copied * sizeof(ifc_devicesupportedcommand*)); + + for(index = 0; index < copied; index++) + objects[index]->AddRef(); + + cursor += copied; + + if (NULL != fetched) + *fetched = copied; + + return (bufferMax == copied) ? S_OK : S_FALSE; +} + +HRESULT DeviceSupportedCommandEnum::Reset(void) +{ + cursor = 0; + return S_OK; +} + +HRESULT DeviceSupportedCommandEnum::Skip(size_t count) +{ + cursor += count; + if (cursor > this->count) + cursor = this->count; + + return (cursor < this->count) ? S_OK : S_FALSE; +} + + +HRESULT DeviceSupportedCommandEnum::GetCount(size_t *count) +{ + if (NULL == count) + return E_POINTER; + + *count = this->count; + + return S_OK; +} + +#define CBCLASS DeviceSupportedCommandEnum +START_DISPATCH; +CB(ADDREF, AddRef) +CB(RELEASE, Release) +CB(QUERYINTERFACE, QueryInterface) +CB(API_NEXT, Next) +CB(API_RESET, Reset) +CB(API_SKIP, Skip) +CB(API_GETCOUNT, GetCount) +END_DISPATCH; +#undef CBCLASS \ No newline at end of file -- cgit