1 //===-- CFCMutableArray.cpp -------------------------------------*- C++ -*-===// 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 //---------------------------------------------------------------------- 13 // CFCString constructor 14 //---------------------------------------------------------------------- 15 CFCMutableArray::CFCMutableArray(CFMutableArrayRef s) 16 : CFCReleaser<CFMutableArrayRef>(s) {} 17 18 //---------------------------------------------------------------------- 19 // CFCMutableArray copy constructor 20 //---------------------------------------------------------------------- 21 CFCMutableArray::CFCMutableArray(const CFCMutableArray &rhs) 22 : CFCReleaser<CFMutableArrayRef>(rhs) // NOTE: this won't make a copy of the 23 // array, just add a new reference to 24 // it 25 {} 26 27 //---------------------------------------------------------------------- 28 // CFCMutableArray copy constructor 29 //---------------------------------------------------------------------- 30 CFCMutableArray &CFCMutableArray::operator=(const CFCMutableArray &rhs) { 31 if (this != &rhs) 32 *this = rhs; // NOTE: this operator won't make a copy of the array, just add 33 // a new reference to it 34 return *this; 35 } 36 37 //---------------------------------------------------------------------- 38 // Destructor 39 //---------------------------------------------------------------------- 40 CFCMutableArray::~CFCMutableArray() {} 41 42 CFIndex CFCMutableArray::GetCount() const { 43 CFMutableArrayRef array = get(); 44 if (array) 45 return ::CFArrayGetCount(array); 46 return 0; 47 } 48 49 CFIndex CFCMutableArray::GetCountOfValue(CFRange range, 50 const void *value) const { 51 CFMutableArrayRef array = get(); 52 if (array) 53 return ::CFArrayGetCountOfValue(array, range, value); 54 return 0; 55 } 56 57 CFIndex CFCMutableArray::GetCountOfValue(const void *value) const { 58 CFMutableArrayRef array = get(); 59 if (array) 60 return ::CFArrayGetCountOfValue(array, CFRangeMake(0, GetCount()), value); 61 return 0; 62 } 63 64 const void *CFCMutableArray::GetValueAtIndex(CFIndex idx) const { 65 CFMutableArrayRef array = get(); 66 if (array) { 67 const CFIndex num_array_items = ::CFArrayGetCount(array); 68 if (0 <= idx && idx < num_array_items) { 69 return ::CFArrayGetValueAtIndex(array, idx); 70 } 71 } 72 return NULL; 73 } 74 75 bool CFCMutableArray::SetValueAtIndex(CFIndex idx, const void *value) { 76 CFMutableArrayRef array = get(); 77 if (array != NULL) { 78 const CFIndex num_array_items = ::CFArrayGetCount(array); 79 if (0 <= idx && idx < num_array_items) { 80 ::CFArraySetValueAtIndex(array, idx, value); 81 return true; 82 } 83 } 84 return false; 85 } 86 87 bool CFCMutableArray::AppendValue(const void *value, bool can_create) { 88 CFMutableArrayRef array = get(); 89 if (array == NULL) { 90 if (!can_create) 91 return false; 92 array = 93 ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); 94 reset(array); 95 } 96 if (array != NULL) { 97 ::CFArrayAppendValue(array, value); 98 return true; 99 } 100 return false; 101 } 102 103 bool CFCMutableArray::AppendCStringAsCFString(const char *s, 104 CFStringEncoding encoding, 105 bool can_create) { 106 CFMutableArrayRef array = get(); 107 if (array == NULL) { 108 if (!can_create) 109 return false; 110 array = 111 ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); 112 reset(array); 113 } 114 if (array != NULL) { 115 CFCString cf_str(s, encoding); 116 ::CFArrayAppendValue(array, cf_str.get()); 117 return true; 118 } 119 return false; 120 } 121 122 bool CFCMutableArray::AppendFileSystemRepresentationAsCFString( 123 const char *s, bool can_create) { 124 CFMutableArrayRef array = get(); 125 if (array == NULL) { 126 if (!can_create) 127 return false; 128 array = 129 ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); 130 reset(array); 131 } 132 if (array != NULL) { 133 CFCString cf_path; 134 cf_path.SetFileSystemRepresentation(s); 135 ::CFArrayAppendValue(array, cf_path.get()); 136 return true; 137 } 138 return false; 139 } 140