123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- #include "main.h"
- #include "./deviceObjectStore.h"
- #include <algorithm>
- template <typename T>
- struct GenericComparator
- {
- typedef const char* (T::*GETTER)();
- GETTER m_getterFunc;
- const char *m_data;
- GenericComparator(GETTER getterFunc, const char *data)
- {
- m_getterFunc = getterFunc;
- m_data = data;
- }
- bool operator()(const T& obj)
- {
- return strcmp(((obj).*m_getterFunc)(), m_data) == 0;
- }
- };
- static ifc_deviceobject *
- DeviceObjectStore_FindLocation(const char *name, std::vector<ifc_deviceobject*> &list)
- {
- if (FALSE != IS_STRING_EMPTY(name))
- return NULL;
- //size_t name_length;
- //name_length = lstrlenA(name) * sizeof(char);
- //
- //return (ifc_deviceobject*)bsearch_s(name, buffer, length,
- // sizeof(ifc_deviceobject*),
- // DeviceObjectStore_FindComparer,
- // (void*)name_length);
- //auto it = std::find_if(list.begin(), list.begin(), GenericComparator<ifc_deviceobject>(&ifc_deviceobject::GetName, name));
- const auto it = std::find_if(list.begin(), list.end(),
- [&](ifc_deviceobject* upT) -> bool
- {
- return strcmp(upT->GetName(), name) == 0;
- }
- );
- if (it != list.end())
- {
- return *it;
- }
- return nullptr;
- }
- int DeviceObjectComparer(const char* arg1, const char* arg2)
- {
- return stricmp(arg1, arg2);
- }
- // Created for std::sort
- static bool DeviceObjectStore_SortComparer_V2(const void* element1, const void* element2)
- {
- //return DeviceObjectStore_SortComparer(element1, element2) < 0;
- const char* name1 = (((ifc_deviceobject*)element1))->GetName();
- const char* name2 = (((ifc_deviceobject*)element2))->GetName();
- return DeviceObjectComparer(name1, name2) < 0;
- }
- static ifc_deviceobject *
- DeviceObjectStore_FindUnsortedObject(const char *name, ifc_deviceobject **objects, size_t count)
- {
- size_t index;
- size_t length;
- if (0 == count)
- return NULL;
-
- length = lstrlenA(name) * sizeof(char);
-
- for(index = 0; index < count; index++)
- {
- //if (0 == DeviceObjectStore_NameCompare(name, length, objects[index]->GetName()))
- if(0 == DeviceObjectComparer(name, objects[index]->GetName()))
- return objects[index];
- }
- return NULL;
- }
- DeviceObjectStore::DeviceObjectStore(DeviceObjectCallback addCallback,
- DeviceObjectCallback _removeCallback, void *callbackData)
- {
- this->addCallback = addCallback;
- this->removeCallback = _removeCallback;
- this->callbackData = callbackData;
- InitializeCriticalSection(&lock);
- }
- DeviceObjectStore::~DeviceObjectStore()
- {
- RemoveAll();
- DeleteCriticalSection(&lock);
- }
- void DeviceObjectStore::Lock()
- {
- EnterCriticalSection(&lock);
- }
- void DeviceObjectStore::Unlock()
- {
- LeaveCriticalSection(&lock);
- }
- CRITICAL_SECTION *DeviceObjectStore::GetLock()
- {
- return &lock;
- }
- HRESULT DeviceObjectStore::Add(ifc_deviceobject *object)
- {
- const char *name;
-
- if (NULL == object)
- return E_POINTER;
- name = object->GetName();
- if (NULL == name || '\0' == *name)
- return E_INVALIDARG;
- return (1 == AddRange(&object, 1)) ? S_OK : S_FALSE;
- }
- size_t DeviceObjectStore::AddRange(ifc_deviceobject **objects, size_t count)
- {
- const char *name;
- size_t index, registered, added;
- ifc_deviceobject *object;
-
- if (NULL == objects || 0 == count)
- return 0;
-
- Lock();
- added = 0;
- registered = list.size();
-
- for(index = 0; index < count; index++)
- {
- object = objects[index];
- if (NULL != object)
- {
- name = object->GetName();
-
- if (NULL != name &&
- '\0' != *name &&
- NULL == DeviceObjectStore_FindLocation(name, list))
- //&& NULL == DeviceObjectStore_FindUnsortedObject(name, buffer + registered, added))
- {
- list.push_back(object);
- object->AddRef();
- if (NULL != addCallback)
- this->addCallback(this, object, callbackData);
- added++;
- }
- }
- }
-
- if (0 != added)
- {
- //qsort(list.first(), list.size(),
- // sizeof(ifc_deviceobject**),
- // DeviceObjectStore_SortComparer);
- std::sort(list.begin(), list.end(), DeviceObjectStore_SortComparer_V2);
- }
- Unlock();
- return added;
- }
- size_t DeviceObjectStore::AddIndirect(const char **names, size_t count, DeviceObjectCreator callback, void *user)
- {
- size_t index, registered, added;
- ifc_deviceobject *object;
- if (NULL == names || 0 == count || NULL == callback)
- return 0;
- Lock();
- added = 0;
- registered = list.size();
-
- for(index = 0; index < count; index++)
- {
- const char *name = names[index];
- if (NULL != name &&
- '\0' != *name &&
- NULL == DeviceObjectStore_FindLocation(name, list) )
- //&& NULL == DeviceObjectStore_FindUnsortedObject(name, buffer + registered, added))
- {
- object = callback(name, user);
- if (NULL != object)
- {
- list.push_back(object);
-
- if (NULL != addCallback)
- this->addCallback(this, object, callbackData);
- added++;
- }
- }
- }
-
- if (0 != added)
- {
- //qsort(list.first(), list.size(),
- // sizeof(ifc_deviceobject**),
- // DeviceObjectStore_SortComparer);
- std::sort(list.begin(), list.end(), DeviceObjectStore_SortComparer_V2);
- }
- Unlock();
- return added;
- }
- HRESULT DeviceObjectStore::Remove(const char *name)
- {
- HRESULT hr = hr = S_FALSE;
-
- if (NULL == name || '\0' == *name)
- return E_INVALIDARG;
- Lock();
- //object_ptr = DeviceObjectStore_FindLocation(name, list);
- //if (NULL != object_ptr)
- //{
- // hr = S_OK;
- // ifc_deviceobject *object = *object_ptr;
- // size_t index = (size_t)(object_ptr - buffer);
- // list.erase(list.begin() + index);
- // if (NULL != removeCallback)
- // removeCallback(this, object, callbackData);
- // object->Release();
- //}
- //else
- //{
- // hr = S_FALSE;
- //}
-
- const auto it = std::find_if(list.begin(), list.end(),
- [&](ifc_deviceobject* upT) -> bool
- {
- return strcmp(upT->GetName(), name) == 0;
- }
- );
- if (it != list.end())
- {
- ifc_deviceobject* object = *it;
- list.erase(it);
- if (NULL != removeCallback)
- {
- removeCallback(this, object, callbackData);
- }
- object->Release();
- hr = S_OK;
- }
- Unlock();
- return hr;
- }
- void DeviceObjectStore::RemoveAll()
- {
- Lock();
- size_t index = list.size();
- while(index--)
- {
- ifc_deviceobject *object = list[index];
- if (NULL != removeCallback)
- removeCallback(this, object, callbackData);
- object->Release();
- }
- list.clear();
- Unlock();
- }
- HRESULT DeviceObjectStore::Find(const char *name, ifc_deviceobject **object)
- {
- HRESULT hr;
- ifc_deviceobject *object_ptr;
- if (NULL == name || '\0' == *name)
- return E_INVALIDARG;
- if (NULL == object)
- return E_POINTER;
- Lock();
- object_ptr = DeviceObjectStore_FindLocation(name, list);
-
- if (NULL != object_ptr)
- {
- if (NULL != object)
- {
- *object = object_ptr;
- (*object)->AddRef();
- }
- hr = S_OK;
- }
- else
- {
- if (NULL != object)
- *object = NULL;
- hr = S_FALSE;
- }
-
- Unlock();
- return hr;
- }
- HRESULT DeviceObjectStore::Enumerate(DeviceObjectEnum **enumerator)
- {
- HRESULT hr;
- if (NULL == enumerator)
- return E_POINTER;
- Lock();
- hr = DeviceObjectEnum::CreateInstance(list.size() ? &list.at(0) : nullptr, list.size(), enumerator);
-
- Unlock();
- return hr;
- }
|