| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Companion guide | |
| Declared in | CFBag.h |
CFBag and its derived mutable type, CFMutableBag, manage non-sequential collections of values called bags in which there can be duplicate values. CFBag creates static bags and CFMutableBag creates dynamic bags.
Use bags or sets as an alternative to arrays when the order of elements isn't important and performance in testing whether a value is contained in the collection is a consideration—while arrays are ordered, testing for membership is slower than with bags or sets. Use bags over sets if you want to allow duplicate values in your collections.
You create a static bag object using either the CFBagCreate or CFBagCreateCopy function. These functions return a bag containing the values you pass in as arguments. (Note that bags can't contain NULL pointers; in most cases, though, you can use the kCFNull constant instead.) Values are not copied but retained using the retain callback provided when the bag was created. Similarly, when a value is removed from a bag, it is released using the release callback.
CFBag provides functions for querying the values of a bag. The CFBagGetCount returns the number of values in a bag, the CFBagContainsValue function checks if a value is in a bag, and CFBagGetValues returns a C array containing all the values in a bag.
The CFBagApplyFunction function lets you apply a function to all values in a bag.
CFBagContainsValue
CFBagGetCount
CFBagGetCountOfValue
CFBagGetValue
CFBagGetValueIfPresent
CFBagGetValues
Calls a function once for each value in a bag.
void CFBagApplyFunction ( CFBagRef theBag, CFBagApplierFunction applier, void *context );
The bag to operate upon.
The callback function to call once for each value in the theBag. If this parameter is not a pointer to a function of the correct prototype, the behavior is undefined. If there are values in the range that the applier function does not expect or cannot properly apply to, the behavior is undefined.
A pointer-sized program-defined value, which is passed as the second parameter to the applier function, but is otherwise unused by this function. If the context is not what is expected by the applier function, the behavior is undefined.
While this function iterates over a mutable collection, it is unsafe for the applier function to change the contents of the collection.
CFBag.h
Reports whether or not a value is in a bag.
Boolean CFBagContainsValue ( CFBagRef theBag, const void *value );
The bag to examine.
The value to match in theBag. The equal callback provided when theBag was created is used to compare. If the equal callback was NULL, pointer equality (in C, ==) is used. If value, or any other value in theBag, is not understood by the equal callback, the behavior is undefined.
true if value is contained in theBag, otherwise false.
CFBag.h
Creates an immutable bag containing specified values.
CFBagRef CFBagCreate ( CFAllocatorRef allocator, const void **values, CFIndex numValues, const CFBagCallBacks *callBacks );
The allocator to use to allocate memory for the new bag and its storage for values. Pass NULL or kCFAllocatorDefault to use the current default allocator.
A C array of the pointer-sized values to be in the new bag. This parameter may be NULL if the numValues parameter is 0. The C array is not changed or freed by this function. values must be a valid pointer to a C array of at least numValues elements.
The number of values to copy from the values C array in the new CFBag object. If the number is negative or is greater than the actual number of values, the behavior is undefined.
A pointer to a CFBagCallBacks structure initialized with the callbacks to use to retain, release, describe, and compare values in the bag. A copy of the contents of the callbacks structure is made, so that a pointer to a structure on the stack can be passed in or can be reused for multiple collection creations. This parameter may be NULL, which is treated as if a valid structure of version 0 with all fields NULL had been passed in. Otherwise, if any of the fields are not valid pointers to functions of the correct type, or this parameter is not a valid pointer to a CFBagCallBacks structure, the behavior is undefined. If any value put into the collection is not one understood by one of the callback functions, the behavior when that callback function is used is undefined. If the collection contains CFType objects only, then pass kCFTypeBagCallBacks as this parameter to use the default callback functions.
A new bag, or NULL if there was a problem creating the object. Ownership follows the Create Rule.
CFBag.h
Creates an immutable bag with the values of another bag.
CFBagRef CFBagCreateCopy ( CFAllocatorRef allocator, CFBagRef theBag );
The allocator to use to allocate memory for the new bag and its storage for values. Pass NULL or kCFAllocatorDefault to use the current default allocator.
The bag to copy. The pointer values from theBag are copied into the new bag. However, the values are also retained by the new bag. The count of the new bag is the same as the count of theBag. The new bag uses the same callbacks as theBag.
A new bag that contains the same values as theBag, or NULL if there was a problem creating the object. Ownership follows the Create Rule.
CFBag.h
Returns the number of values currently in a bag.
CFIndex CFBagGetCount ( CFBagRef theBag );
The bag to examine.
The number of values in theBag.
CFBag.h
Returns the number of times a value occurs in a bag.
CFIndex CFBagGetCountOfValue ( CFBagRef theBag, const void *value );
The bag to examine.
The value for which to find matches in theBag. The equal callback provided when theBag was created is used to compare. If the equal callback was NULL, pointer equality (in C, ==) is used. If value, or any other value in theBag, is not understood by the equal callback, the behavior is undefined.
The number of times value occurs in theBag.
CFBag.h
Returns the type identifier for the CFBag opaque type.
CFTypeID CFBagGetTypeID ( void );
The type identifier for the CFBag opaque type.
CFMutableBag objects have the same type identifier as CFBag objects.
CFBag.h
Returns a requested value from a bag.
const void * CFBagGetValue ( CFBagRef theBag, const void *value );
The bag to examine.
The value for which to find matches in theBag. The equal callback provided when theBag was created is used to compare. If the equal callback was NULL, pointer equality (in C, ==) is used. If value, or any other value in theBag, is not understood by the equal callback, the behavior is undefined.
A pointer to value, or NULL if value is not in theBag. If the value is a Core Foundation object, ownership follows the Get Rule.
Depending on the implementation of the equal callback specified when creating theBag, the value returned may not have the same pointer equality as value.
CFBag.h
Reports whether or not a value is in a bag, and returns that value indirectly if it exists.
Boolean CFBagGetValueIfPresent ( CFBagRef theBag, const void *candidate, const void **value );
The bag to be searched.
The value for which to find matches in theBag. The equal callback provided when theBag was created is used to compare. If the equal callback was NULL, pointer equality (in C, ==) is used. If candidate, or any other value in theBag, is not understood by the equal callback, the behavior is undefined.
A pointer to a value object. Set to the matching value if it exists in the bag, otherwise NULL. If the value is a Core Foundation object, ownership follows the Get Rule.
true if value is present in theBag, otherwise false.
Depending on the implementation of the equal callback specified when creating theBag, the value returned in value may not have the same pointer equality as candidate.
CFBag.h
Fills a buffer with values from a bag.
void CFBagGetValues ( CFBagRef theBag, const void **values );
The bag to examine.
A C array of pointer-sized values to be filled with values from theBag. The value must be a valid C array of the appropriate type and size (that is, a size equal to the count of theBag).
CFBag.hPrototype of a callback function that may be applied to every value in a bag.
typedef void (*CFBagApplierFunction) ( const void *value, void *context );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( const void *value, void *context );
The current value in a bag.
The program-defined context parameter given to the apply function.
This callback is passed to the CFBagApplyFunction function which iterates over the values in a bag and applies the behavior defined in the applier function to each value in a bag.
CFBag.hPrototype of a callback function used to get a description of a value in a bag.
typedef CFStringRef (*CFBagCopyDescriptionCallBack) ( const void *value );
If you name your function MyCallBack, you would declare it like this:
CFStringRef MyCallBack ( const void *value );
The value to be described.
A textual description of value. mmancreate
This callback is passed to CFBagCreate in a CFBagCallBacks structure. This callback is used by the CFCopyDescription function.
CFBag.hPrototype of a callback function used to determine if two values in a bag are equal.
typedef Boolean (*CFBagEqualCallBack) ( const void *value1, const void *value2 );
If you name your function MyCallBack, you would declare it like this:
Boolean MyCallBack ( const void *value1, const void *value2 );
A value in the bag.
Another value in the bag.
true if value1 and value2 are equal, false otherwise.
This callback is passed to CFBagCreate in a CFBagCallBacks structure.
CFBag.hPrototype of a callback function invoked to compute a hash code for a value. Hash codes are used when values are accessed, added, or removed from a collection.
typedef CFHashCode (*CFBagHashCallBack) ( const void *value );
If you name your function MyCallBack, you would declare it like this:
CFHashCode CFBagHashCallBack ( const void *value );
The value used to compute the hash code.
An integer that can be used as a table address in a hash table structure.
This callback is passed to CFBagCreate in a CFBagCallBacks structure.
CFBag.hPrototype of a callback function used to release a value before it’s removed from a bag.
typedef void (*CFBagReleaseCallBack) ( CFAllocatorRef allocator, const void *value );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( CFAllocatorRef allocator, const void *value );
The bag’s allocator.
The value being removed from the bag.
This callback is passed to CFBagCreate in a CFBagCallBacks structure.
CFBag.hPrototype of a callback function used to retain a value being added to a bag.
typedef const void *(*CFBagRetainCallBack) ( CFAllocatorRef allocator, const void *value );
If you name your function MyCallBack, you would declare it like this:
const void *MyCallBack ( CFAllocatorRef allocator, const void *value );
The bag’s allocator.
The value being added to the bag.
The value to store in the bag, which is usually the value parameter passed to this callback, but may be a different value if a different value should be stored in the collection.
This callback is passed to CFBagCreate in a CFBagCallBacks structure.
CFBag.hThis structure contains the callbacks used to retain, release, describe, and compare the values of a CFBag object.
struct CFBagCallBacks {
CFIndex version;
CFBagRetainCallBack retain;
CFBagReleaseCallBack release;
CFBagCopyDescriptionCallBack copyDescription;
CFBagEqualCallBack equal;
CFBagHashCallBack hash;
};
typedef struct CFBagCallBacks CFBagCallBacks;
versionThe version number of this structure. If not one of the defined version numbers for this opaque type, the behavior is undefined. The current version of this structure is 0.
retainThe callback used to retain each value as they are added to the collection. If NULL, values are not retained. See CFBagRetainCallBack for a descriptions of this function’s parameters.
releaseThe callback used to release values as they are removed from the collection. If NULL, values are not released. See CFBagReleaseCallBack for a description of this callback.
copyDescriptionThe callback used to create a descriptive string representation of each value in the collection. If NULL, the collection will create a simple description of each value. See CFBagCopyDescriptionCallBack for a description of this callback.
equalThe callback used to compare values in the collection for equality for some operations. If NULL, the collection will use pointer equality to compare values in the collection. See CFBagEqualCallBack for a description of this callback.
hashThe callback used to compute a hash code for values in a collection. If NULL, the collection computes a hash code by converting the pointer value to an integer. See CFBagHashCallBack for a description of this callback.
CFBag.hA reference to an immutable bag object.
typedef const struct __CFBag *CFBagRef;
CFBag.hCFBag provides some predefined callbacks for your convenience.
const CFBagCallBacks kCFTypeBagCallBacks; const CFBagCallBacks kCFCopyStringBagCallBacks;
kCFTypeBagCallBacksPredefined CFBagCallBacks structure containing a set of callbacks appropriate for use when the values in a CFBag are all CFType-derived objects. The retain callback is CFRetain, the release callback is CFRelease, the copy callback is CFCopyDescription, the equal callback is CFEqual, and the hash callback is CFHash. Therefore, if you use this constant when creating the collection, items are automatically retained when added to the collection, and released when removed from the collection.
Available in Mac OS X v10.0 and later.
Declared in CFBag.h
kCFCopyStringBagCallBacksPredefined CFBagCallBacks structure containing a set of callbacks appropriate for use when the values in a CFBag are all CFString objects. The bag makes immutable copies of the strings placed into it.
Available in Mac OS X v10.0 and later.
Declared in CFBag.h
Last updated: 2007-05-22