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
|
2013-06-20 14:13:12 +08:00
|
|
|
A convenience macro to iterate over a Array 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) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr, **__end__ = (__array__)->data->arr + (__array__)->data->num-1; \
|
2012-11-26 22:11:56 +08:00
|
|
|
__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) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr + (__array__)->data->num-1, **__end__ = (__array__)->data->arr; \
|
2012-11-26 22:11:56 +08:00
|
|
|
__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) \
|
2013-06-20 14:13:12 +08:00
|
|
|
for(Object** __arr__ = (__array__)->data->arr, \
|
2012-11-26 22:11:56 +08:00
|
|
|
**__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) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* child; \
|
2012-06-08 15:17:15 +08:00
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
elementType pNode = static_cast<elementType>(child); \
|
2012-06-08 15:17:15 +08:00
|
|
|
if(pNode) \
|
|
|
|
{ \
|
|
|
|
pNode->func(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
|
|
|
#define arrayMakeObjectsPerformSelectorWithObject(pArray, func, pObject, elementType) \
|
|
|
|
do { \
|
|
|
|
if(pArray && pArray->count() > 0) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* child; \
|
2012-06-08 15:17:15 +08:00
|
|
|
CCARRAY_FOREACH(pArray, child) \
|
|
|
|
{ \
|
2013-07-09 14:29:51 +08:00
|
|
|
elementType pNode = static_cast<elementType>(child); \
|
2012-06-08 15:17:15 +08:00
|
|
|
if(pNode) \
|
|
|
|
{ \
|
|
|
|
pNode->func(pObject); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(false)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
class CC_DLL Array : public Object, public Clonable
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
~Array();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Create an array */
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* create();
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with some objects */
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* create(Object* pObject, ...);
|
2012-07-23 22:49:11 +08:00
|
|
|
/** Create an array with one object */
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithObject(Object* pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with capacity */
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithCapacity(unsigned int capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Create an array with an existing array */
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
@brief Generate a Array pointer by file
|
2012-04-19 14:35:52 +08:00
|
|
|
@param pFileName The file name of *.plist file
|
2013-06-20 14:13:12 +08:00
|
|
|
@return The Array pointer generated from the file
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* 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().
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Array* createWithContentsOfFileThreadSafe(const char* pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Initializes an array */
|
|
|
|
bool init();
|
|
|
|
/** Initializes an array with one object */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithObject(Object* pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Initializes an array with some objects */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithObjects(Object* pObject, ...);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Initializes an array with capacity */
|
|
|
|
bool initWithCapacity(unsigned int capacity);
|
|
|
|
/** Initializes an array with an existing array */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Querying an Array
|
|
|
|
|
|
|
|
/** Returns element count of the array */
|
2013-05-04 20:05:06 +08:00
|
|
|
unsigned int count() const;
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns capacity of the array */
|
2013-05-04 20:05:06 +08:00
|
|
|
unsigned int capacity() const;
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int indexOfObject(Object* object) const;
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns an element with a certain index */
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* objectAtIndex(unsigned int index);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns last element */
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* lastObject();
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns a random element */
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* randomObject();
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Returns a Boolean value that indicates whether object is present in array. */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool containsObject(Object* object) const;
|
2012-06-14 05:26:28 +08:00
|
|
|
/** @since 1.1 */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool isEqualToArray(Array* pOtherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Adding Objects
|
|
|
|
|
|
|
|
/** Add a certain object */
|
2013-06-20 14:13:12 +08:00
|
|
|
void addObject(Object* object);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Add all elements of an existing array */
|
2013-06-20 14:13:12 +08:00
|
|
|
void addObjectsFromArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Insert a certain object at a certain index */
|
2013-06-20 14:13:12 +08:00
|
|
|
void insertObject(Object* object, unsigned int index);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Removing Objects
|
|
|
|
|
|
|
|
/** Remove last object */
|
|
|
|
void removeLastObject(bool bReleaseObj = true);
|
|
|
|
/** Remove a certain object */
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeObject(Object* object, bool bReleaseObj = true);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Remove an element with a certain index */
|
|
|
|
void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
|
|
|
|
/** Remove all elements */
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeObjectsInArray(Array* otherArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Remove all objects */
|
|
|
|
void removeAllObjects();
|
|
|
|
/** Fast way to remove a certain object */
|
2013-06-20 14:13:12 +08:00
|
|
|
void fastRemoveObject(Object* object);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Fast way to remove an element with a certain index */
|
|
|
|
void fastRemoveObjectAtIndex(unsigned int index);
|
|
|
|
|
|
|
|
// Rearranging Content
|
|
|
|
|
|
|
|
/** Swap two elements */
|
2013-06-20 14:13:12 +08:00
|
|
|
void exchangeObject(Object* object1, Object* object2);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** 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. */
|
2013-06-20 14:13:12 +08:00
|
|
|
void replaceObjectAtIndex(unsigned int uIndex, Object* pObject, bool bReleaseObject = true);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
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
|
|
|
|
2013-05-10 15:07:05 +08:00
|
|
|
/* override functions */
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void acceptVisitor(DataVisitor &visitor);
|
2013-07-08 23:05:47 +08:00
|
|
|
virtual Array* clone() const;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
public:
|
|
|
|
ccArray* data;
|
2013-06-20 14:13:12 +08:00
|
|
|
Array();
|
|
|
|
Array(unsigned int capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
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__
|