Opaque Types

Core Foundation relies heavily on what are known as "opaque types." An opaque type is roughly equivalent to a class in object-oriented programming. It is an aggregate data type plus a suite of functions that operate on objects of that type. The individual fields of an object based on an opaque type are hidden from clients, but the type's functions offer access to most values of these fields. Figure 3 depicts an opaque type in the data it "hides" and in the interface it presents to clients.

Note: "Class" is not used to refer to opaque types because, despite the conceptual similarity of class and opaque type, many might find the term confusing. However, the Core Foundation documentation frequently refers to specific, data-bearing instances of these types as "objects."


Core Foundation has many opaque types, and the names of these types reflect their intended uses. For example, CFString is an opaque type that "represents" and operates on Unicode character arrays. ("CF" is, of course, a prefix for Core Foundation.) CFArray is an opaque type for indexed-based collection functionality. The functions, constants, and other secondary data types in support of an opaque type are generally defined in a header file having the name of the type; CFArray.h , for example, contains the symbol definitions for the CFArray type.

A major programming-interface convention in Core Foundation is to use the name of the opaque type that is most closely related to a symbol as the symbol's prefix. For functions, this prefix identifies not only the type to which the function "belongs" but usually the type of object that is the target of the function's action. (An exception to this convention are constants, which put "k" before the type prefix.) Here are a few examples from the header files:

/* from CFDictionary.h */
CF_EXPORT CFIndex CFDictionaryGetCountOfKey(CFDictionaryRef dict, const void *key);
/* from CFString.h */
typedef UInt32 CFStringEncoding;
/* from CFCharacterSet.h */typedef enum {
    kCFCharacterSetControl = 1,
    kCFCharacterSetWhitespace,    
    kCFCharacterSetWhitespaceAndNewline,    
    kCFCharacterSetDecimalDigit,    
    kCFCharacterSetLetter,    
    kCFCharacterSetLowercaseLetter,    
    kCFCharacterSetUppercaseLetter,    
    kCFCharacterSetNonBase,    
    kCFCharacterSetDecomposable,    
    kCFCharacterSetAlphaNumeric,    
    kCFCharacterSetPunctuation,    
    kCFCharacterSetIllegal
} CFCharacterSetPredefinedSet;

See Other Programming-Interface Conventions for more on programming-interface naming and usage conventions.

Figure 3 An opaque type

Advantages of Opaque Types

Object References

Base Types and "Polymorphic" Functions

Varieties of Objects


© 1999 Apple Computer, Inc. – (Last Updated 07 September 99)