123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #include "main.h"
- #include "./deviceSupportedCommandEnum.h"
- #include <new>
- 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<ifc_devicesupportedcommandenum*>(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
|