/source/Host/macosx/cfcpp/CFCMutableDictionary.cpp

https://gitlab.com/jorjpimm/lldb · C++ · 529 lines · 427 code · 46 blank · 56 comment · 78 complexity · 5a1de490b6fc74f3024d0d62ea2c6f7b MD5 · raw file

  1. //===-- CFCMutableDictionary.cpp --------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "CFCMutableDictionary.h"
  10. #include "CFCString.h"
  11. //----------------------------------------------------------------------
  12. // CFCString constructor
  13. //----------------------------------------------------------------------
  14. CFCMutableDictionary::CFCMutableDictionary(CFMutableDictionaryRef s) :
  15. CFCReleaser<CFMutableDictionaryRef> (s)
  16. {
  17. }
  18. //----------------------------------------------------------------------
  19. // CFCMutableDictionary copy constructor
  20. //----------------------------------------------------------------------
  21. CFCMutableDictionary::CFCMutableDictionary(const CFCMutableDictionary& rhs) :
  22. CFCReleaser<CFMutableDictionaryRef> (rhs)
  23. {
  24. }
  25. //----------------------------------------------------------------------
  26. // CFCMutableDictionary copy constructor
  27. //----------------------------------------------------------------------
  28. const CFCMutableDictionary&
  29. CFCMutableDictionary::operator=(const CFCMutableDictionary& rhs)
  30. {
  31. if (this != &rhs)
  32. *this = rhs;
  33. return *this;
  34. }
  35. //----------------------------------------------------------------------
  36. // Destructor
  37. //----------------------------------------------------------------------
  38. CFCMutableDictionary::~CFCMutableDictionary()
  39. {
  40. }
  41. CFIndex
  42. CFCMutableDictionary::GetCount() const
  43. {
  44. CFMutableDictionaryRef dict = get();
  45. if (dict)
  46. return ::CFDictionaryGetCount (dict);
  47. return 0;
  48. }
  49. CFIndex
  50. CFCMutableDictionary::GetCountOfKey(const void *key) const
  51. {
  52. CFMutableDictionaryRef dict = get();
  53. if (dict)
  54. return ::CFDictionaryGetCountOfKey (dict, key);
  55. return 0;
  56. }
  57. CFIndex
  58. CFCMutableDictionary::GetCountOfValue(const void *value) const
  59. {
  60. CFMutableDictionaryRef dict = get();
  61. if (dict)
  62. return ::CFDictionaryGetCountOfValue (dict, value);
  63. return 0;
  64. }
  65. void
  66. CFCMutableDictionary::GetKeysAndValues(const void **keys, const void **values) const
  67. {
  68. CFMutableDictionaryRef dict = get();
  69. if (dict)
  70. ::CFDictionaryGetKeysAndValues (dict, keys, values);
  71. }
  72. const void *
  73. CFCMutableDictionary::GetValue(const void *key) const
  74. {
  75. CFMutableDictionaryRef dict = get();
  76. if (dict)
  77. return ::CFDictionaryGetValue (dict, key);
  78. return NULL;
  79. }
  80. Boolean
  81. CFCMutableDictionary::GetValueIfPresent(const void *key, const void **value_handle) const
  82. {
  83. CFMutableDictionaryRef dict = get();
  84. if (dict)
  85. return ::CFDictionaryGetValueIfPresent (dict, key, value_handle);
  86. return false;
  87. }
  88. CFMutableDictionaryRef
  89. CFCMutableDictionary::Dictionary(bool can_create)
  90. {
  91. CFMutableDictionaryRef dict = get();
  92. if (can_create && dict == NULL)
  93. {
  94. dict = ::CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  95. reset ( dict );
  96. }
  97. return dict;
  98. }
  99. bool
  100. CFCMutableDictionary::AddValue(CFStringRef key, const void *value, bool can_create)
  101. {
  102. CFMutableDictionaryRef dict = Dictionary(can_create);
  103. if (dict != NULL)
  104. {
  105. // Let the dictionary own the CFNumber
  106. ::CFDictionaryAddValue (dict, key, value);
  107. return true;
  108. }
  109. return false;
  110. }
  111. bool
  112. CFCMutableDictionary::SetValue(CFStringRef key, const void *value, bool can_create)
  113. {
  114. CFMutableDictionaryRef dict = Dictionary(can_create);
  115. if (dict != NULL)
  116. {
  117. // Let the dictionary own the CFNumber
  118. ::CFDictionarySetValue (dict, key, value);
  119. return true;
  120. }
  121. return false;
  122. }
  123. bool
  124. CFCMutableDictionary::AddValueSInt8(CFStringRef key, int8_t value, bool can_create)
  125. {
  126. CFMutableDictionaryRef dict = Dictionary(can_create);
  127. if (dict != NULL)
  128. {
  129. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, &value));
  130. if (cf_number.get())
  131. {
  132. // Let the dictionary own the CFNumber
  133. ::CFDictionaryAddValue (dict, key, cf_number.get());
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. bool
  140. CFCMutableDictionary::SetValueSInt8(CFStringRef key, int8_t value, bool can_create)
  141. {
  142. CFMutableDictionaryRef dict = Dictionary(can_create);
  143. if (dict != NULL)
  144. {
  145. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, &value));
  146. if (cf_number.get())
  147. {
  148. // Let the dictionary own the CFNumber
  149. ::CFDictionarySetValue (dict, key, cf_number.get());
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. bool
  156. CFCMutableDictionary::AddValueSInt16(CFStringRef key, int16_t value, bool can_create)
  157. {
  158. CFMutableDictionaryRef dict = Dictionary(can_create);
  159. if (dict != NULL)
  160. {
  161. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt16Type, &value));
  162. if (cf_number.get())
  163. {
  164. // Let the dictionary own the CFNumber
  165. ::CFDictionaryAddValue (dict, key, cf_number.get());
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. bool
  172. CFCMutableDictionary::SetValueSInt16(CFStringRef key, int16_t value, bool can_create)
  173. {
  174. CFMutableDictionaryRef dict = Dictionary(can_create);
  175. if (dict != NULL)
  176. {
  177. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt16Type, &value));
  178. if (cf_number.get())
  179. {
  180. // Let the dictionary own the CFNumber
  181. ::CFDictionarySetValue (dict, key, cf_number.get());
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. bool
  188. CFCMutableDictionary::AddValueSInt32(CFStringRef key, int32_t value, bool can_create)
  189. {
  190. CFMutableDictionaryRef dict = Dictionary(can_create);
  191. if (dict != NULL)
  192. {
  193. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt32Type, &value));
  194. if (cf_number.get())
  195. {
  196. // Let the dictionary own the CFNumber
  197. ::CFDictionaryAddValue (dict, key, cf_number.get());
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. bool
  204. CFCMutableDictionary::SetValueSInt32(CFStringRef key, int32_t value, bool can_create)
  205. {
  206. CFMutableDictionaryRef dict = Dictionary(can_create);
  207. if (dict != NULL)
  208. {
  209. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt32Type, &value));
  210. if (cf_number.get())
  211. {
  212. // Let the dictionary own the CFNumber
  213. ::CFDictionarySetValue (dict, key, cf_number.get());
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. bool
  220. CFCMutableDictionary::AddValueSInt64(CFStringRef key, int64_t value, bool can_create)
  221. {
  222. CFMutableDictionaryRef dict = Dictionary(can_create);
  223. if (dict != NULL)
  224. {
  225. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &value));
  226. if (cf_number.get())
  227. {
  228. // Let the dictionary own the CFNumber
  229. ::CFDictionaryAddValue (dict, key, cf_number.get());
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. bool
  236. CFCMutableDictionary::SetValueSInt64(CFStringRef key, int64_t value, bool can_create)
  237. {
  238. CFMutableDictionaryRef dict = Dictionary(can_create);
  239. if (dict != NULL)
  240. {
  241. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &value));
  242. if (cf_number.get())
  243. {
  244. // Let the dictionary own the CFNumber
  245. ::CFDictionarySetValue (dict, key, cf_number.get());
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. bool
  252. CFCMutableDictionary::AddValueUInt8(CFStringRef key, uint8_t value, bool can_create)
  253. {
  254. CFMutableDictionaryRef dict = Dictionary(can_create);
  255. if (dict != NULL)
  256. {
  257. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  258. int16_t sval = value;
  259. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt16Type, &sval));
  260. if (cf_number.get())
  261. {
  262. // Let the dictionary own the CFNumber
  263. ::CFDictionaryAddValue (dict, key, cf_number.get());
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. bool
  270. CFCMutableDictionary::SetValueUInt8(CFStringRef key, uint8_t value, bool can_create)
  271. {
  272. CFMutableDictionaryRef dict = Dictionary(can_create);
  273. if (dict != NULL)
  274. {
  275. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  276. int16_t sval = value;
  277. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt16Type, &sval));
  278. if (cf_number.get())
  279. {
  280. // Let the dictionary own the CFNumber
  281. ::CFDictionarySetValue (dict, key, cf_number.get());
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. bool
  288. CFCMutableDictionary::AddValueUInt16(CFStringRef key, uint16_t value, bool can_create)
  289. {
  290. CFMutableDictionaryRef dict = Dictionary(can_create);
  291. if (dict != NULL)
  292. {
  293. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  294. int32_t sval = value;
  295. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt32Type, &sval));
  296. if (cf_number.get())
  297. {
  298. // Let the dictionary own the CFNumber
  299. ::CFDictionaryAddValue (dict, key, cf_number.get());
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. bool
  306. CFCMutableDictionary::SetValueUInt16(CFStringRef key, uint16_t value, bool can_create)
  307. {
  308. CFMutableDictionaryRef dict = Dictionary(can_create);
  309. if (dict != NULL)
  310. {
  311. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  312. int32_t sval = value;
  313. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt32Type, &sval));
  314. if (cf_number.get())
  315. {
  316. // Let the dictionary own the CFNumber
  317. ::CFDictionarySetValue (dict, key, cf_number.get());
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. bool
  324. CFCMutableDictionary::AddValueUInt32(CFStringRef key, uint32_t value, bool can_create)
  325. {
  326. CFMutableDictionaryRef dict = Dictionary(can_create);
  327. if (dict != NULL)
  328. {
  329. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  330. int64_t sval = value;
  331. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &sval));
  332. if (cf_number.get())
  333. {
  334. // Let the dictionary own the CFNumber
  335. ::CFDictionaryAddValue (dict, key, cf_number.get());
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. bool
  342. CFCMutableDictionary::SetValueUInt32(CFStringRef key, uint32_t value, bool can_create)
  343. {
  344. CFMutableDictionaryRef dict = Dictionary(can_create);
  345. if (dict != NULL)
  346. {
  347. // Have to promote to the next size type so things don't appear negative of the MSBit is set...
  348. int64_t sval = value;
  349. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &sval));
  350. if (cf_number.get())
  351. {
  352. // Let the dictionary own the CFNumber
  353. ::CFDictionarySetValue (dict, key, cf_number.get());
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. bool
  360. CFCMutableDictionary::AddValueUInt64(CFStringRef key, uint64_t value, bool can_create)
  361. {
  362. CFMutableDictionaryRef dict = Dictionary(can_create);
  363. if (dict != NULL)
  364. {
  365. // The number may appear negative if the MSBit is set in "value". Due to a limitation of
  366. // CFNumber, there isn't a way to have it show up otherwise as of this writing.
  367. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &value));
  368. if (cf_number.get())
  369. {
  370. // Let the dictionary own the CFNumber
  371. ::CFDictionaryAddValue (dict, key, cf_number.get());
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. bool
  378. CFCMutableDictionary::SetValueUInt64(CFStringRef key, uint64_t value, bool can_create)
  379. {
  380. CFMutableDictionaryRef dict = Dictionary(can_create);
  381. if (dict != NULL)
  382. {
  383. // The number may appear negative if the MSBit is set in "value". Due to a limitation of
  384. // CFNumber, there isn't a way to have it show up otherwise as of this writing.
  385. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &value));
  386. if (cf_number.get())
  387. {
  388. // Let the dictionary own the CFNumber
  389. ::CFDictionarySetValue (dict, key, cf_number.get());
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. bool
  396. CFCMutableDictionary::AddValueDouble(CFStringRef key, double value, bool can_create)
  397. {
  398. CFMutableDictionaryRef dict = Dictionary(can_create);
  399. if (dict != NULL)
  400. {
  401. // The number may appear negative if the MSBit is set in "value". Due to a limitation of
  402. // CFNumber, there isn't a way to have it show up otherwise as of this writing.
  403. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberDoubleType, &value));
  404. if (cf_number.get())
  405. {
  406. // Let the dictionary own the CFNumber
  407. ::CFDictionaryAddValue (dict, key, cf_number.get());
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. bool
  414. CFCMutableDictionary::SetValueDouble(CFStringRef key, double value, bool can_create)
  415. {
  416. CFMutableDictionaryRef dict = Dictionary(can_create);
  417. if (dict != NULL)
  418. {
  419. // The number may appear negative if the MSBit is set in "value". Due to a limitation of
  420. // CFNumber, there isn't a way to have it show up otherwise as of this writing.
  421. CFCReleaser<CFNumberRef> cf_number(::CFNumberCreate (kCFAllocatorDefault, kCFNumberDoubleType, &value));
  422. if (cf_number.get())
  423. {
  424. // Let the dictionary own the CFNumber
  425. ::CFDictionarySetValue (dict, key, cf_number.get());
  426. return true;
  427. }
  428. }
  429. return false;
  430. }
  431. bool
  432. CFCMutableDictionary::AddValueCString(CFStringRef key, const char *cstr, bool can_create)
  433. {
  434. CFMutableDictionaryRef dict = Dictionary(can_create);
  435. if (dict != NULL)
  436. {
  437. CFCString cf_str(cstr, kCFStringEncodingUTF8);
  438. if (cf_str.get())
  439. {
  440. // Let the dictionary own the CFNumber
  441. ::CFDictionaryAddValue (dict, key, cf_str.get());
  442. return true;
  443. }
  444. }
  445. return false;
  446. }
  447. bool
  448. CFCMutableDictionary::SetValueCString(CFStringRef key, const char *cstr, bool can_create)
  449. {
  450. CFMutableDictionaryRef dict = Dictionary(can_create);
  451. if (dict != NULL)
  452. {
  453. CFCString cf_str(cstr, kCFStringEncodingUTF8);
  454. if (cf_str.get())
  455. {
  456. // Let the dictionary own the CFNumber
  457. ::CFDictionarySetValue (dict, key, cf_str.get());
  458. return true;
  459. }
  460. }
  461. return false;
  462. }
  463. void
  464. CFCMutableDictionary::RemoveAllValues()
  465. {
  466. CFMutableDictionaryRef dict = get();
  467. if (dict)
  468. ::CFDictionaryRemoveAllValues(dict);
  469. }
  470. void
  471. CFCMutableDictionary::RemoveValue(const void *value)
  472. {
  473. CFMutableDictionaryRef dict = get();
  474. if (dict)
  475. ::CFDictionaryRemoveValue(dict, value);
  476. }
  477. void
  478. CFCMutableDictionary::ReplaceValue(const void *key, const void *value)
  479. {
  480. CFMutableDictionaryRef dict = get();
  481. if (dict)
  482. ::CFDictionaryReplaceValue (dict, key, value);
  483. }