| 1 |
//===-- CFCMutableArray.cpp -----------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "CFCMutableArray.h" |
| 10 |
#include "CFCString.h" |
| 11 |
|
| 12 |
// CFCString constructor |
| 13 |
CFCMutableArray::CFCMutableArray(CFMutableArrayRef s) |
| 14 |
: CFCReleaser<CFMutableArrayRef>(s) {} |
| 15 |
|
| 16 |
// CFCMutableArray copy constructor |
| 17 |
CFCMutableArray::CFCMutableArray(const CFCMutableArray &rhs) = |
| 18 |
default; // NOTE: this won't make a copy of the |
| 19 |
// array, just add a new reference to |
| 20 |
// it |
| 21 |
|
| 22 |
// CFCMutableArray copy constructor |
| 23 |
CFCMutableArray &CFCMutableArray::operator=(const CFCMutableArray &rhs) { |
| 24 |
if (this != &rhs) |
| 25 |
*this = rhs; // NOTE: this operator won't make a copy of the array, just add |
| 26 |
// a new reference to it |
| 27 |
return *this; |
| 28 |
} |
| 29 |
|
| 30 |
// Destructor |
| 31 |
CFCMutableArray::~CFCMutableArray() = default; |
| 32 |
|
| 33 |
CFIndex CFCMutableArray::GetCount() const { |
| 34 |
CFMutableArrayRef array = get(); |
| 35 |
if (array) |
| 36 |
return ::CFArrayGetCount(array); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
CFIndex CFCMutableArray::GetCountOfValue(CFRange range, |
| 41 |
const void *value) const { |
| 42 |
CFMutableArrayRef array = get(); |
| 43 |
if (array) |
| 44 |
return ::CFArrayGetCountOfValue(array, range, value); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
CFIndex CFCMutableArray::GetCountOfValue(const void *value) const { |
| 49 |
CFMutableArrayRef array = get(); |
| 50 |
if (array) |
| 51 |
return ::CFArrayGetCountOfValue(array, CFRangeMake(0, GetCount()), value); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
const void *CFCMutableArray::GetValueAtIndex(CFIndex idx) const { |
| 56 |
CFMutableArrayRef array = get(); |
| 57 |
if (array) { |
| 58 |
const CFIndex num_array_items = ::CFArrayGetCount(array); |
| 59 |
if (0 <= idx && idx < num_array_items) { |
| 60 |
return ::CFArrayGetValueAtIndex(array, idx); |
| 61 |
} |
| 62 |
} |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
bool CFCMutableArray::SetValueAtIndex(CFIndex idx, const void *value) { |
| 67 |
CFMutableArrayRef array = get(); |
| 68 |
if (array != NULL) { |
| 69 |
const CFIndex num_array_items = ::CFArrayGetCount(array); |
| 70 |
if (0 <= idx && idx < num_array_items) { |
| 71 |
::CFArraySetValueAtIndex(array, idx, value); |
| 72 |
return true; |
| 73 |
} |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
bool CFCMutableArray::AppendValue(const void *value, bool can_create) { |
| 79 |
CFMutableArrayRef array = get(); |
| 80 |
if (array == NULL) { |
| 81 |
if (!can_create) |
| 82 |
return false; |
| 83 |
array = |
| 84 |
::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 85 |
reset(array); |
| 86 |
} |
| 87 |
if (array != NULL) { |
| 88 |
::CFArrayAppendValue(array, value); |
| 89 |
return true; |
| 90 |
} |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
bool CFCMutableArray::AppendCStringAsCFString(const char *s, |
| 95 |
CFStringEncoding encoding, |
| 96 |
bool can_create) { |
| 97 |
CFMutableArrayRef array = get(); |
| 98 |
if (array == NULL) { |
| 99 |
if (!can_create) |
| 100 |
return false; |
| 101 |
array = |
| 102 |
::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 103 |
reset(array); |
| 104 |
} |
| 105 |
if (array != NULL) { |
| 106 |
CFCString cf_str(s, encoding); |
| 107 |
::CFArrayAppendValue(array, cf_str.get()); |
| 108 |
return true; |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
bool CFCMutableArray::AppendFileSystemRepresentationAsCFString( |
| 114 |
const char *s, bool can_create) { |
| 115 |
CFMutableArrayRef array = get(); |
| 116 |
if (array == NULL) { |
| 117 |
if (!can_create) |
| 118 |
return false; |
| 119 |
array = |
| 120 |
::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 121 |
reset(array); |
| 122 |
} |
| 123 |
if (array != NULL) { |
| 124 |
CFCString cf_path; |
| 125 |
cf_path.SetFileSystemRepresentation(s); |
| 126 |
::CFArrayAppendValue(array, cf_path.get()); |
| 127 |
return true; |
| 128 |
} |
| 129 |
return false; |
| 130 |
} |