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