2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCARRAY_H__
|
|
|
|
#define __CCARRAY_H__
|
|
|
|
|
|
|
|
#include "support/data_support/ccCArray.h"
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup data_structures
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** @def CCARRAY_FOREACH
|
2012-09-17 15:02:24 +08:00
|
|
|
A convenience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.4
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
In cocos2d-iphone 1.0.0, This macro have been update to like this:
|
|
|
|
|
|
|
|
#define CCARRAY_FOREACH(__array__, __object__) \
|
|
|
|
if (__array__ && __array__->data->num > 0) \
|
|
|
|
for(id *__arr__ = __array__->data->arr, *end = __array__->data->arr + __array__->data->num-1; \
|
|
|
|
__arr__ <= end && ((__object__ = *__arr__) != nil || true); \
|
|
|
|
__arr__++)
|
|
|
|
|
|
|
|
I found that it's not work in C++. So it keep what it's look like in version 1.0.0-rc3. ---By Bin
|
|
|
|
*/
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_FOREACH(__array__, __object__) \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
|
|
|
for(CCObject** __arr__ = (__array__)->data->arr, **__end__ = (__array__)->data->arr + (__array__)->data->num-1; \
|
|
|
|
__arr__ <= __end__ && (((__object__) = *__arr__) != NULL/* || true*/); \
|
|
|
|
__arr__++)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
|
|
|
for(CCObject** __arr__ = (__array__)->data->arr + (__array__)->data->num-1, **__end__ = (__array__)->data->arr; \
|
|
|
|
__arr__ >= __end__ && (((__object__) = *__arr__) != NULL/* || true*/); \
|
|
|
|
__arr__--)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
2012-11-26 22:11:56 +08:00
|
|
|
#define CCARRAY_VERIFY_TYPE(__array__, __type__) \
|
|
|
|
do { \
|
|
|
|
if ((__array__) && (__array__)->data->num > 0) \
|
|
|
|
for(CCObject** __arr__ = (__array__)->data->arr, \
|
|
|
|
**__end__ = (__array__)->data->arr + (__array__)->data->num-1; __arr__ <= __end__; __arr__++) \
|
|
|
|
CCAssert(dynamic_cast<__type__>(*__arr__), "element type is wrong!"); \
|
2012-04-19 14:35:52 +08:00
|
|
|
} while(false)
|
|
|
|
#else
|
|
|
|
#define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0)
|
|
|
|
#endif
|
|
|
|
|
2012-06-08 15:17:15 +08:00
|
|
|
#define arrayMakeObjectsPerformSelector(pArray, func, elementType) \
|
|
|
|
do { \
|
|
|
|
if(pArray && pArray->count() > 0) \
|
|
|
|
{ \
|
|
|
|
CCObject* child; \
|
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
|
|
|
elementType pNode = (elementType) child; \
|
|
|
|
if(pNode) \
|
|
|
|
{ \
|
|
|
|
pNode->func(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
|
|
|
#define arrayMakeObjectsPerformSelectorWithObject(pArray, func, pObject, elementType) \
|
|
|
|
do { \
|
|
|
|
if(pArray && pArray->count() > 0) \
|
|
|
|
{ \
|
|
|
|
CCObject* child = NULL; \
|
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
|
|
|
elementType pNode = (elementType) child; \
|
|
|
|
if(pNode) \
|
|
|
|
{ \
|
|
|
|
pNode->func(pObject); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class CC_DLL CCArray : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~CCArray();
|
|
|
|
|
|
|
|
/* static functions */
|
2012-06-14 16:05:58 +08:00
|
|
|
/** Create an array
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use create() instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* array();
|
2012-06-14 16:05:58 +08:00
|
|
|
/** Create an array with one object
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use createWithObject(CCObject*) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithObject(CCObject* pObject);
|
2012-06-14 16:05:58 +08:00
|
|
|
/** Create an array with some objects
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use create(CCObject*, ...) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithObjects(CCObject* pObject, ...);
|
2012-06-14 16:05:58 +08:00
|
|
|
/** Create an array with capacity
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use createWithCapacity(unsigned int) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithCapacity(unsigned int capacity);
|
2012-06-14 16:05:58 +08:00
|
|
|
/** Create an array with an existing array
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use createWithArray(CCArray*) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithArray(CCArray* otherArray);
|
2012-06-14 16:05:58 +08:00
|
|
|
/**
|
|
|
|
@brief Generate a CCArray pointer by file
|
|
|
|
@param pFileName The file name of *.plist file
|
|
|
|
@return The CCArray pointer generated from the file
|
2012-09-17 15:02:24 +08:00
|
|
|
@deprecated: Please use createWithContentsOfFile(const char*) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithContentsOfFile(const char* pFileName);
|
2012-06-14 16:05:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
2012-08-10 13:58:34 +08:00
|
|
|
@deprecated: Please use createWithContentsOfFileThreadSafe(const char*) instead. This interface will be deprecated sooner or later.
|
2012-06-14 16:05:58 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCArray* arrayWithContentsOfFileThreadSafe(const char* pFileName);
|
2012-06-14 16:05:58 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array */
|
2012-06-14 16:05:58 +08:00
|
|
|
static CCArray* create();
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with some objects */
|
2012-06-14 16:05:58 +08:00
|
|
|
static CCArray* create(CCObject* pObject, ...);
|
2012-07-23 22:49:11 +08:00
|
|
|
/** Create an array with one object */
|
|
|
|
static CCArray* createWithObject(CCObject* pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with capacity */
|
2012-07-23 22:49:11 +08:00
|
|
|
static CCArray* createWithCapacity(unsigned int capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with an existing array */
|
2012-07-23 22:49:11 +08:00
|
|
|
static CCArray* createWithArray(CCArray* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Generate a CCArray pointer by file
|
|
|
|
@param pFileName The file name of *.plist file
|
|
|
|
@return The CCArray pointer generated from the file
|
|
|
|
*/
|
2012-07-23 22:49:11 +08:00
|
|
|
static CCArray* createWithContentsOfFile(const char* pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
|
|
|
*/
|
2012-06-14 16:05:58 +08:00
|
|
|
static CCArray* createWithContentsOfFileThreadSafe(const char* pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Initializes an array */
|
|
|
|
bool init();
|
|
|
|
/** Initializes an array with one object */
|
|
|
|
bool initWithObject(CCObject* pObject);
|
|
|
|
/** Initializes an array with some objects */
|
|
|
|
bool initWithObjects(CCObject* pObject, ...);
|
|
|
|
/** Initializes an array with capacity */
|
|
|
|
bool initWithCapacity(unsigned int capacity);
|
|
|
|
/** Initializes an array with an existing array */
|
|
|
|
bool initWithArray(CCArray* otherArray);
|
|
|
|
|
|
|
|
// Querying an Array
|
|
|
|
|
|
|
|
/** Returns element count of the array */
|
|
|
|
unsigned int count();
|
|
|
|
/** Returns capacity of the array */
|
|
|
|
unsigned int capacity();
|
|
|
|
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
|
|
|
|
unsigned int indexOfObject(CCObject* object);
|
|
|
|
/** Returns an element with a certain index */
|
|
|
|
CCObject* objectAtIndex(unsigned int index);
|
|
|
|
/** Returns last element */
|
|
|
|
CCObject* lastObject();
|
|
|
|
/** Returns a random element */
|
|
|
|
CCObject* randomObject();
|
|
|
|
/** Returns a Boolean value that indicates whether object is present in array. */
|
|
|
|
bool containsObject(CCObject* object);
|
2012-06-14 05:26:28 +08:00
|
|
|
/** @since 1.1 */
|
2012-06-08 14:11:48 +08:00
|
|
|
bool isEqualToArray(CCArray* pOtherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Adding Objects
|
|
|
|
|
|
|
|
/** Add a certain object */
|
|
|
|
void addObject(CCObject* object);
|
|
|
|
/** Add all elements of an existing array */
|
|
|
|
void addObjectsFromArray(CCArray* otherArray);
|
|
|
|
/** Insert a certain object at a certain index */
|
|
|
|
void insertObject(CCObject* object, unsigned int index);
|
|
|
|
|
|
|
|
// Removing Objects
|
|
|
|
|
|
|
|
/** Remove last object */
|
|
|
|
void removeLastObject(bool bReleaseObj = true);
|
|
|
|
/** Remove a certain object */
|
|
|
|
void removeObject(CCObject* object, bool bReleaseObj = true);
|
|
|
|
/** Remove an element with a certain index */
|
|
|
|
void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
|
|
|
|
/** Remove all elements */
|
|
|
|
void removeObjectsInArray(CCArray* otherArray);
|
|
|
|
/** Remove all objects */
|
|
|
|
void removeAllObjects();
|
|
|
|
/** Fast way to remove a certain object */
|
|
|
|
void fastRemoveObject(CCObject* object);
|
|
|
|
/** Fast way to remove an element with a certain index */
|
|
|
|
void fastRemoveObjectAtIndex(unsigned int index);
|
|
|
|
|
|
|
|
// Rearranging Content
|
|
|
|
|
|
|
|
/** Swap two elements */
|
|
|
|
void exchangeObject(CCObject* object1, CCObject* object2);
|
|
|
|
/** Swap two elements with certain indexes */
|
|
|
|
void exchangeObjectAtIndex(unsigned int index1, unsigned int index2);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
|
|
|
/** Replace object at index with another object. */
|
|
|
|
void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Revers the array */
|
|
|
|
void reverseObjects();
|
|
|
|
/* Shrinks the array so the memory footprint corresponds with the number of items */
|
|
|
|
void reduceMemoryFootprint();
|
2012-06-08 16:22:57 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* override functions */
|
|
|
|
virtual CCObject* copyWithZone(CCZone* pZone);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ccArray* data;
|
|
|
|
CCArray();
|
|
|
|
CCArray(unsigned int capacity);
|
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of data_structure group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CCARRAY_H__
|