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