deviceSupportedCommandStore.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "main.h"
  2. #include "./deviceSupportedCommandStore.h"
  3. DeviceSupportedCommandStore::DeviceSupportedCommandStore()
  4. : ref(1)
  5. {
  6. InitializeCriticalSection(&lock);
  7. }
  8. DeviceSupportedCommandStore::~DeviceSupportedCommandStore()
  9. {
  10. RemoveAll();
  11. DeleteCriticalSection(&lock);
  12. }
  13. HRESULT DeviceSupportedCommandStore::CreateInstance(DeviceSupportedCommandStore **instance)
  14. {
  15. if (NULL == instance)
  16. return E_POINTER;
  17. *instance = new DeviceSupportedCommandStore();
  18. if (NULL == *instance)
  19. return E_OUTOFMEMORY;
  20. return S_OK;
  21. }
  22. size_t DeviceSupportedCommandStore::AddRef()
  23. {
  24. return InterlockedIncrement((LONG*)&ref);
  25. }
  26. size_t DeviceSupportedCommandStore::Release()
  27. {
  28. if (0 == ref)
  29. return ref;
  30. LONG r = InterlockedDecrement((LONG*)&ref);
  31. if (0 == r)
  32. delete(this);
  33. return r;
  34. }
  35. int DeviceSupportedCommandStore::QueryInterface(GUID interface_guid, void **object)
  36. {
  37. if (NULL == object) return E_POINTER;
  38. if (IsEqualIID(interface_guid, IFC_DeviceSupportedCommandStore))
  39. *object = static_cast<ifc_devicesupportedcommandstore*>(this);
  40. else
  41. {
  42. *object = NULL;
  43. return E_NOINTERFACE;
  44. }
  45. if (NULL == *object)
  46. return E_UNEXPECTED;
  47. AddRef();
  48. return S_OK;
  49. }
  50. void DeviceSupportedCommandStore::Lock()
  51. {
  52. EnterCriticalSection(&lock);
  53. }
  54. void DeviceSupportedCommandStore::Unlock()
  55. {
  56. LeaveCriticalSection(&lock);
  57. }
  58. int
  59. DeviceSupportedCommandStore::SortCallback(const void *element1, const void *element2)
  60. {
  61. DeviceSupportedCommand *command1, *command2;
  62. command1 = *(DeviceSupportedCommand**)element1;
  63. command2 = *(DeviceSupportedCommand**)element2;
  64. return CompareStringA(CSTR_INVARIANT, 0, command1->GetName(), -1, command2->GetName(), -1) - 2;
  65. }
  66. int
  67. DeviceSupportedCommandStore::SearchCallback(const void *key, const void *element)
  68. {
  69. const char *name;
  70. DeviceSupportedCommand *command;
  71. name = (const char*)key;
  72. command = *(DeviceSupportedCommand**)element;
  73. return CompareStringA(CSTR_INVARIANT, 0, name, -1, command->GetName(), -1) - 2;
  74. }
  75. DeviceSupportedCommand *DeviceSupportedCommandStore::Find(const char *name, size_t *indexOut)
  76. {
  77. DeviceSupportedCommand *command;
  78. int length;
  79. size_t index, count;
  80. if (NULL == name || '\0' == *name)
  81. return NULL;
  82. length = lstrlenA(name);
  83. count = commandList.size();
  84. for(index = 0; index < count; index++)
  85. {
  86. command = commandList[index];
  87. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, length, command->GetName(), -1))
  88. break;
  89. }
  90. if (count == index)
  91. return NULL;
  92. if (NULL != indexOut)
  93. *indexOut = index;
  94. return command;
  95. }
  96. HRESULT DeviceSupportedCommandStore::Add(const char *name, DeviceCommandFlags flags)
  97. {
  98. HRESULT hr;
  99. DeviceSupportedCommand *command;
  100. hr = DeviceSupportedCommand::CreateInstance(name, &command);
  101. if (FAILED(hr))
  102. return hr;
  103. Lock();
  104. if (NULL == Find(name, NULL))
  105. {
  106. command->SetFlags(flags, flags);
  107. commandList.push_back(command);
  108. }
  109. else
  110. {
  111. command->Release();
  112. hr = S_FALSE;
  113. }
  114. Unlock();
  115. return hr;
  116. }
  117. HRESULT DeviceSupportedCommandStore::Remove(const char *name)
  118. {
  119. HRESULT hr;
  120. size_t index;
  121. DeviceSupportedCommand *command;
  122. Lock();
  123. command = Find(name, &index);
  124. if (NULL != command)
  125. {
  126. commandList.erase(commandList.begin() + index);
  127. command->Release();
  128. hr = S_OK;
  129. }
  130. else
  131. {
  132. hr = S_FALSE;
  133. }
  134. Unlock();
  135. return hr;
  136. }
  137. HRESULT DeviceSupportedCommandStore::RemoveAll()
  138. {
  139. Lock();
  140. size_t index = commandList.size();
  141. while(index--)
  142. {
  143. DeviceSupportedCommand *command = commandList[index];
  144. command->Release();
  145. }
  146. commandList.clear();
  147. Unlock();
  148. return S_OK;
  149. }
  150. HRESULT DeviceSupportedCommandStore::GetFlags(const char *name, DeviceCommandFlags *flagsOut)
  151. {
  152. HRESULT hr;
  153. DeviceSupportedCommand *command;
  154. if (NULL == flagsOut)
  155. return E_POINTER;
  156. Lock();
  157. command = Find(name, NULL);
  158. hr = (NULL != command) ? command->GetFlags(flagsOut) : E_FAIL;
  159. Unlock();
  160. return hr;
  161. }
  162. HRESULT DeviceSupportedCommandStore::SetFlags(const char *name, DeviceCommandFlags mask, DeviceCommandFlags value)
  163. {
  164. HRESULT hr;
  165. DeviceSupportedCommand *command;
  166. Lock();
  167. command = Find(name, NULL);
  168. hr = (NULL != command) ? command->SetFlags(mask, value) : E_FAIL;
  169. Unlock();
  170. return hr;
  171. }
  172. HRESULT DeviceSupportedCommandStore::Get(const char *name, ifc_devicesupportedcommand **command)
  173. {
  174. HRESULT hr;
  175. if (NULL == command)
  176. return E_POINTER;
  177. Lock();
  178. *command = Find(name, NULL);
  179. if (NULL != *command)
  180. {
  181. (*command)->AddRef();
  182. hr = S_OK;
  183. }
  184. else
  185. hr = E_FAIL;
  186. Unlock();
  187. return hr;
  188. }
  189. HRESULT DeviceSupportedCommandStore::GetActive(ifc_devicesupportedcommand **command)
  190. {
  191. return E_NOTIMPL;
  192. }
  193. HRESULT DeviceSupportedCommandStore::Enumerate(ifc_devicesupportedcommandenum **enumerator)
  194. {
  195. HRESULT hr;
  196. Lock();
  197. hr = DeviceSupportedCommandEnum::CreateInstance(
  198. reinterpret_cast<ifc_devicesupportedcommand**>(commandList.size() ? &commandList.at(0) : nullptr),
  199. commandList.size(),
  200. reinterpret_cast<DeviceSupportedCommandEnum**>(enumerator));
  201. Unlock();
  202. return hr;
  203. }
  204. HRESULT DeviceSupportedCommandStore::Clone(ifc_devicesupportedcommandstore **instance, BOOL fullCopy)
  205. {
  206. HRESULT hr;
  207. DeviceSupportedCommandStore *clone;
  208. DeviceSupportedCommand *command;
  209. if (NULL == instance)
  210. return E_POINTER;
  211. Lock();
  212. hr = CreateInstance(&clone);
  213. if (SUCCEEDED(hr))
  214. {
  215. size_t index, count;
  216. count = commandList.size();
  217. for(index = 0; index < count; index++)
  218. {
  219. command = commandList[index];
  220. if (FALSE != fullCopy)
  221. {
  222. DeviceSupportedCommand *commandClone;
  223. hr = command->Clone(&commandClone);
  224. if(SUCCEEDED(hr))
  225. command = commandClone;
  226. else
  227. break;
  228. }
  229. else
  230. command->AddRef();
  231. clone->commandList.push_back(command);
  232. }
  233. }
  234. Unlock();
  235. if (FAILED(hr) && NULL != clone)
  236. {
  237. clone->Release();
  238. *instance = NULL;
  239. }
  240. else
  241. *instance = clone;
  242. return hr;
  243. }
  244. #define CBCLASS DeviceSupportedCommandStore
  245. START_DISPATCH;
  246. CB(ADDREF, AddRef)
  247. CB(RELEASE, Release)
  248. CB(QUERYINTERFACE, QueryInterface)
  249. CB(API_ADD, Add)
  250. CB(API_REMOVE, Remove)
  251. CB(API_REMOVEALL, RemoveAll)
  252. CB(API_GETFLAGS, GetFlags)
  253. CB(API_SETFLAGS, SetFlags)
  254. CB(API_GET, Get)
  255. CB(API_GETACTIVE, GetActive)
  256. CB(API_ENUMERATE, Enumerate)
  257. CB(API_CLONE, Clone)
  258. END_DISPATCH;
  259. #undef CBCLASS