123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #include "main.h"
- #include "./deviceSupportedCommandStore.h"
- DeviceSupportedCommandStore::DeviceSupportedCommandStore()
- : ref(1)
- {
- InitializeCriticalSection(&lock);
- }
- DeviceSupportedCommandStore::~DeviceSupportedCommandStore()
- {
- RemoveAll();
- DeleteCriticalSection(&lock);
- }
- HRESULT DeviceSupportedCommandStore::CreateInstance(DeviceSupportedCommandStore **instance)
- {
- if (NULL == instance)
- return E_POINTER;
- *instance = new DeviceSupportedCommandStore();
- if (NULL == *instance)
- return E_OUTOFMEMORY;
- return S_OK;
- }
- size_t DeviceSupportedCommandStore::AddRef()
- {
- return InterlockedIncrement((LONG*)&ref);
- }
- size_t DeviceSupportedCommandStore::Release()
- {
- if (0 == ref)
- return ref;
-
- LONG r = InterlockedDecrement((LONG*)&ref);
- if (0 == r)
- delete(this);
-
- return r;
- }
- int DeviceSupportedCommandStore::QueryInterface(GUID interface_guid, void **object)
- {
- if (NULL == object) return E_POINTER;
-
- if (IsEqualIID(interface_guid, IFC_DeviceSupportedCommandStore))
- *object = static_cast<ifc_devicesupportedcommandstore*>(this);
- else
- {
- *object = NULL;
- return E_NOINTERFACE;
- }
- if (NULL == *object)
- return E_UNEXPECTED;
- AddRef();
- return S_OK;
- }
- void DeviceSupportedCommandStore::Lock()
- {
- EnterCriticalSection(&lock);
- }
- void DeviceSupportedCommandStore::Unlock()
- {
- LeaveCriticalSection(&lock);
- }
- int
- DeviceSupportedCommandStore::SortCallback(const void *element1, const void *element2)
- {
- DeviceSupportedCommand *command1, *command2;
- command1 = *(DeviceSupportedCommand**)element1;
- command2 = *(DeviceSupportedCommand**)element2;
-
- return CompareStringA(CSTR_INVARIANT, 0, command1->GetName(), -1, command2->GetName(), -1) - 2;
- }
- int
- DeviceSupportedCommandStore::SearchCallback(const void *key, const void *element)
- {
- const char *name;
- DeviceSupportedCommand *command;
- name = (const char*)key;
- command = *(DeviceSupportedCommand**)element;
-
- return CompareStringA(CSTR_INVARIANT, 0, name, -1, command->GetName(), -1) - 2;
- }
- DeviceSupportedCommand *DeviceSupportedCommandStore::Find(const char *name, size_t *indexOut)
- {
- DeviceSupportedCommand *command;
- int length;
- size_t index, count;
- if (NULL == name || '\0' == *name)
- return NULL;
-
- length = lstrlenA(name);
- count = commandList.size();
- for(index = 0; index < count; index++)
- {
- command = commandList[index];
- if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, length, command->GetName(), -1))
- break;
- }
-
- if (count == index)
- return NULL;
- if (NULL != indexOut)
- *indexOut = index;
-
- return command;
- }
- HRESULT DeviceSupportedCommandStore::Add(const char *name, DeviceCommandFlags flags)
- {
- HRESULT hr;
- DeviceSupportedCommand *command;
-
- hr = DeviceSupportedCommand::CreateInstance(name, &command);
- if (FAILED(hr))
- return hr;
- Lock();
- if (NULL == Find(name, NULL))
- {
- command->SetFlags(flags, flags);
- commandList.push_back(command);
- }
- else
- {
- command->Release();
- hr = S_FALSE;
- }
-
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::Remove(const char *name)
- {
- HRESULT hr;
- size_t index;
- DeviceSupportedCommand *command;
- Lock();
- command = Find(name, &index);
- if (NULL != command)
- {
- commandList.erase(commandList.begin() + index);
- command->Release();
- hr = S_OK;
- }
- else
- {
- hr = S_FALSE;
- }
-
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::RemoveAll()
- {
- Lock();
- size_t index = commandList.size();
- while(index--)
- {
- DeviceSupportedCommand *command = commandList[index];
- command->Release();
- }
- commandList.clear();
- Unlock();
- return S_OK;
- }
- HRESULT DeviceSupportedCommandStore::GetFlags(const char *name, DeviceCommandFlags *flagsOut)
- {
- HRESULT hr;
- DeviceSupportedCommand *command;
- if (NULL == flagsOut)
- return E_POINTER;
- Lock();
- command = Find(name, NULL);
- hr = (NULL != command) ? command->GetFlags(flagsOut) : E_FAIL;
-
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::SetFlags(const char *name, DeviceCommandFlags mask, DeviceCommandFlags value)
- {
- HRESULT hr;
- DeviceSupportedCommand *command;
- Lock();
- command = Find(name, NULL);
- hr = (NULL != command) ? command->SetFlags(mask, value) : E_FAIL;
-
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::Get(const char *name, ifc_devicesupportedcommand **command)
- {
- HRESULT hr;
- if (NULL == command)
- return E_POINTER;
- Lock();
- *command = Find(name, NULL);
- if (NULL != *command)
- {
- (*command)->AddRef();
- hr = S_OK;
- }
- else
- hr = E_FAIL;
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::GetActive(ifc_devicesupportedcommand **command)
- {
- return E_NOTIMPL;
- }
- HRESULT DeviceSupportedCommandStore::Enumerate(ifc_devicesupportedcommandenum **enumerator)
- {
- HRESULT hr;
-
- Lock();
- hr = DeviceSupportedCommandEnum::CreateInstance(
- reinterpret_cast<ifc_devicesupportedcommand**>(commandList.size() ? &commandList.at(0) : nullptr),
- commandList.size(),
- reinterpret_cast<DeviceSupportedCommandEnum**>(enumerator));
-
- Unlock();
- return hr;
- }
- HRESULT DeviceSupportedCommandStore::Clone(ifc_devicesupportedcommandstore **instance, BOOL fullCopy)
- {
- HRESULT hr;
- DeviceSupportedCommandStore *clone;
- DeviceSupportedCommand *command;
- if (NULL == instance)
- return E_POINTER;
- Lock();
-
- hr = CreateInstance(&clone);
- if (SUCCEEDED(hr))
- {
- size_t index, count;
-
- count = commandList.size();
-
- for(index = 0; index < count; index++)
- {
- command = commandList[index];
- if (FALSE != fullCopy)
- {
- DeviceSupportedCommand *commandClone;
-
- hr = command->Clone(&commandClone);
- if(SUCCEEDED(hr))
- command = commandClone;
- else
- break;
- }
- else
- command->AddRef();
-
- clone->commandList.push_back(command);
- }
- }
- Unlock();
- if (FAILED(hr) && NULL != clone)
- {
- clone->Release();
- *instance = NULL;
- }
- else
- *instance = clone;
- return hr;
- }
- #define CBCLASS DeviceSupportedCommandStore
- START_DISPATCH;
- CB(ADDREF, AddRef)
- CB(RELEASE, Release)
- CB(QUERYINTERFACE, QueryInterface)
- CB(API_ADD, Add)
- CB(API_REMOVE, Remove)
- CB(API_REMOVEALL, RemoveAll)
- CB(API_GETFLAGS, GetFlags)
- CB(API_SETFLAGS, SetFlags)
- CB(API_GET, Get)
- CB(API_GETACTIVE, GetActive)
- CB(API_ENUMERATE, Enumerate)
- CB(API_CLONE, Clone)
- END_DISPATCH;
- #undef CBCLASS
|