What does the term "opaque type" mean in the context of "CFBundleRef opaque type"?
Asked Answered
C

3

31

Does someone have a good explanation of what an "opaque type" is? I saw that term in context of the CFBundleRef, where they were saying: "CFBundleRef opaque type". Is that a type that's readonly?

Cubby answered 9/4, 2009 at 17:21 Comment(0)
B
71

An "opaque type" is a type where you don't have a full definition for the struct or class. In C, C++ and Objective-C, you can tell the compiler that a type will be defined later by using a forward declaration:

// forward declaration of struct in C, C++ and Objective-C
struct Foo;

// forward declaration of class in C++:
class Bar;

// forward declaration of class in Objective-C:
@class Baz;

The compiler doesn't have enough information to let you do anything directly with the struct or class except declare pointers to it, but this is frequently all you need to do. This allows library and framework creators to hide implementation details. Users of a library or framework then call helper functions to create, manipulate and destroy instances of a forward declared struct or class. For example, a framework creator could create these functions for struct Foo:

struct Foo *createFoo(void);
void addNumberToFoo(struct Foo *foo, int number);
void destroyFoo(struct Foo *foo);

As part of the Core Foundation framework, Apple makes common Objective-C classes like NSString, NSArray and NSBundle available to C programmers through opaque types. C programmers use pointers and helper functions to create, manipulate and destroy instances of these Objective-C classes. Apple calls this "toll-free bridging". They follow a common naming convention: "CF" prefix + class name + "Ref" suffix, where "CF" stands for "Core Foundation" and "Ref" is short for "Reference", meaning it's a pointer.

Balzac answered 9/4, 2009 at 17:45 Comment(2)
one more question about that: Do the creators of the frameworks provide these helper functions, or do the users create them?Cubby
The first part of the answer is correct. The part about toll-free bridging is wrong though. Because a Core Foundation opaque type has an equivalent NS class doesn't mean it's toll-free bridged. For example, NSBundle is not toll-free bridged to CFBundleRef. Please consult the Toll-Free Bridged Types documentation.Antecedence
R
14

An opaque type is a type that "wraps" lower-level types, and is often used when either the underlying implementation is complex, or the user simply does not need to know about the inner workings. Apple has a good page on opaque types here:

https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/OpaqueTypes.html

For example, CFString is an opaque type because it wraps a character array, maintains its length, its encoding, etc., but does not directly allow the user to access these values. Rather it provides methods that access or manipulate internal fields and return to the user the relevant information.

Reeves answered 9/4, 2009 at 17:27 Comment(0)
P
2

It's a future-declared structure. For example:

typedef struct CFBundle *CFBundleRef;

Without the actual definition of "struct CFBundle", your code cannot access anything within a CFBundleRef pointer. This is opaque.

Politico answered 9/4, 2009 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.