Skip to content

Commit a8a26ed

Browse files
committed
Add CFEqual, CFDictionaryRef.ByReference, CFStringRef.ByReference to CoreFoundation
1 parent ca465ee commit a8a26ed

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Next Release (5.12.0)
77

88
Features
99
--------
10+
* [#1433](https://github.com/java-native-access/jna/pull/1433): Add `CFEqual`, `CFDictionaryRef.ByReference`, `CFStringRef.ByReference` to `c.s.j.p.mac.CoreFoundation` - [@shalupov](https://github.com/shalupov)
1011

1112
Bug Fixes
1213
---------

contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java

+53
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,29 @@ public Pointer getBytePtr() {
377377
* A reference to an immutable {@code CFDictionary} object.
378378
*/
379379
class CFDictionaryRef extends CFTypeRef {
380+
381+
/**
382+
* Placeholder for a reference to a {@code CFDictionary} object.
383+
*/
384+
public static class ByReference extends PointerByReference {
385+
public ByReference() {
386+
super();
387+
}
388+
389+
public ByReference(CoreFoundation.CFStringRef value) {
390+
super(value.getPointer());
391+
}
392+
393+
public CoreFoundation.CFDictionaryRef getDictionaryRefValue() {
394+
Pointer value = super.getValue();
395+
if (value == null) {
396+
return null;
397+
}
398+
399+
return new CoreFoundation.CFDictionaryRef(value);
400+
}
401+
}
402+
380403
public CFDictionaryRef() {
381404
super();
382405
}
@@ -460,6 +483,28 @@ public void setValue(PointerType key, PointerType value) {
460483
* the characteristics and behavior of {@code CFString} objects.
461484
*/
462485
class CFStringRef extends CFTypeRef {
486+
487+
/**
488+
* Placeholder for a reference to a {@code CFString} object.
489+
*/
490+
public static class ByReference extends PointerByReference {
491+
public ByReference() {
492+
}
493+
494+
public ByReference(CoreFoundation.CFStringRef value) {
495+
super(value.getPointer());
496+
}
497+
498+
public CoreFoundation.CFStringRef getStringRefValue() {
499+
Pointer value = super.getValue();
500+
if (value == null) {
501+
return null;
502+
}
503+
504+
return new CoreFoundation.CFStringRef(value);
505+
}
506+
}
507+
463508
public CFStringRef() {
464509
super();
465510
}
@@ -974,6 +1019,14 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, CFIndex c
9741019
*/
9751020
CFIndex CFStringGetMaximumSizeForEncoding(CFIndex length, int encoding);
9761021

1022+
/**
1023+
* Determines whether two Core Foundation objects are considered equal.
1024+
* @param cf1 A CFType object to compare to cf2.
1025+
* @param cf2 A CFType object to compare to cf1.
1026+
* @return true if cf1 and cf2 are of the same type and considered equal, otherwise false.
1027+
*/
1028+
boolean CFEqual(CFTypeRef cf1, CFTypeRef cf2);
1029+
9771030
/**
9781031
* Gets the default allocator object for the current thread.
9791032
*

contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.List;
3939
import java.util.Random;
4040

41+
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
4142
import org.junit.Test;
4243

4344
import com.sun.jna.Memory;
@@ -248,4 +249,55 @@ public void testCFDictionary() {
248249
cfOne.release();
249250
dict.release();
250251
}
252+
253+
@Test
254+
public void testCFStringRefByReference() {
255+
CFStringRef key = CFStringRef.createCFString("key");
256+
CFStringRef value = CFStringRef.createCFString("value");
257+
258+
CFMutableDictionaryRef dict = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
259+
dict.setValue(key, value);
260+
261+
CFStringRef.ByReference byRef = new CFStringRef.ByReference();
262+
assertTrue(dict.getValueIfPresent(key, byRef));
263+
assertTrue(CF.CFEqual(value, byRef.getStringRefValue()));
264+
}
265+
266+
@Test
267+
public void testCFDictionaryRefByReference() {
268+
CFStringRef key = CFStringRef.createCFString("key");
269+
270+
CFMutableDictionaryRef value = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
271+
value.setValue(CFStringRef.createCFString("1"), CFStringRef.createCFString("2"));
272+
273+
CFMutableDictionaryRef dict = CF.CFDictionaryCreateMutable(null, new CFIndex(2), null, null);
274+
dict.setValue(key, value);
275+
276+
CFDictionaryRef.ByReference byRef = new CFDictionaryRef.ByReference();
277+
assertTrue(dict.getValueIfPresent(key, byRef));
278+
assertTrue(CF.CFEqual(value, byRef.getDictionaryRefValue()));
279+
}
280+
281+
@Test
282+
public void testCFEqual() {
283+
CFAllocatorRef alloc = CF.CFAllocatorGetDefault();
284+
285+
CFStringRef s1 = CFStringRef.createCFString("s1");
286+
287+
assertTrue(CF.CFEqual(s1, s1));
288+
assertTrue(CF.CFEqual(s1, CFStringRef.createCFString("s1")));
289+
290+
assertFalse(CF.CFEqual(s1, CFStringRef.createCFString("s2")));
291+
292+
CFMutableDictionaryRef dict1 = CF.CFDictionaryCreateMutable(alloc, new CFIndex(2), null, null);
293+
dict1.setValue(s1, s1);
294+
CFMutableDictionaryRef dict2 = CF.CFDictionaryCreateMutable(alloc, new CFIndex(2), null, null);
295+
dict2.setValue(s1, s1);
296+
297+
assertNotEquals(dict1.getPointer(), dict2.getPointer());
298+
assertTrue(CF.CFEqual(dict1, dict2));
299+
300+
dict2.setValue(s1, CFStringRef.createCFString("s2"));
301+
assertFalse(CF.CFEqual(dict1, dict2));
302+
}
251303
}

0 commit comments

Comments
 (0)