axmol/cocos2dx/include/CCArray.h

156 lines
5.9 KiB
C
Raw Normal View History

/****************************************************************************
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"
/** @def CCARRAY_FOREACH
A convience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface.
@since v0.99.4
*/
2011-07-08 14:15:51 +08:00
/*
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
*/
#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++)
#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--)
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
#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!"); \
} while(false)
#else
#define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0)
#endif
NS_CC_BEGIN
class CC_DLL CCArray : public CCObject
{
public:
~CCArray();
2012-02-23 10:06:36 +08:00
/** Create an array */
static CCArray* array();
2012-02-23 10:06:36 +08:00
/** Create an array with capacity */
static CCArray* arrayWithCapacity(unsigned int capacity);
2012-02-23 10:06:36 +08:00
/** Create an array with an existing array */
static CCArray* arrayWithArray(CCArray* otherArray);
2012-02-23 10:06:36 +08:00
/** Initializes an array */
bool init();
2012-02-23 10:06:36 +08:00
/** Initializes an array with capacity */
bool initWithCapacity(unsigned int capacity);
2012-02-23 10:06:36 +08:00
/** Initializes an array with an existing array */
bool initWithArray(CCArray* otherArray);
// Querying an Array
2012-02-23 10:06:36 +08:00
/** Returns element count of the array */
unsigned int count();
2012-02-23 10:06:36 +08:00
/** Returns capacity of the array */
unsigned int capacity();
2012-02-23 10:06:36 +08:00
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
unsigned int indexOfObject(CCObject* object);
2012-02-23 10:06:36 +08:00
/** Returns an element with a certain index */
CCObject* objectAtIndex(unsigned int index);
2012-02-23 10:06:36 +08:00
/** Returns last element */
CCObject* lastObject();
2012-02-23 10:06:36 +08:00
/** Returns a random element */
CCObject* randomObject();
2012-02-23 10:06:36 +08:00
/** Returns a Boolean value that indicates whether object is present in array. */
bool containsObject(CCObject* object);
// Adding Objects
2012-02-23 10:06:36 +08:00
/** Add a certain object */
void addObject(CCObject* object);
2012-02-23 10:06:36 +08:00
/** Add all elements of an existing array */
void addObjectsFromArray(CCArray* otherArray);
2012-02-23 10:06:36 +08:00
/** Insert a certain object at a certain index */
void insertObject(CCObject* object, unsigned int index);
// Removing Objects
2012-02-23 10:06:36 +08:00
/** Remove last object */
void removeLastObject(bool bReleaseObj = true);
2012-02-23 10:06:36 +08:00
/** Remove a certain object */
void removeObject(CCObject* object, bool bReleaseObj = true);
2012-02-23 10:06:36 +08:00
/** Remove an element with a certain index */
void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
2012-02-23 10:06:36 +08:00
/** Remove all elements */
void removeObjectsInArray(CCArray* otherArray);
2012-02-23 10:06:36 +08:00
/** Remove all objects */
void removeAllObjects();
2012-02-23 10:06:36 +08:00
/** Fast way to remove a certain object */
void fastRemoveObject(CCObject* object);
2012-02-23 10:06:36 +08:00
/** Fast way to remove an element with a certain index */
void fastRemoveObjectAtIndex(unsigned int index);
// Rearranging Content
2012-02-23 10:06:36 +08:00
/** Swap two elements */
void exchangeObject(CCObject* object1, CCObject* object2);
2012-02-23 10:06:36 +08:00
/** Swap two elements with certain indexes */
void exchangeObjectAtIndex(unsigned int index1, unsigned int index2);
2012-02-23 10:06:36 +08:00
/** Revers the array */
void reverseObjects();
2012-02-23 10:06:36 +08:00
/* Shrinks the array so the memory footprint corresponds with the number of items */
void reduceMemoryFootprint();
void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true);
/** TODO: deep copy array. */
virtual CCObject* copyWithZone(CCZone* pZone) {CCAssert(false, "");return NULL;}
public:
ccArray* data;
CCArray() : data(NULL) {};
};
NS_CC_END
#endif // __CCARRAY_H__