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