CCArray(c+数组,createWithContentsOfFile)

#ifndef __CCARRAY_H__

#define __CCARRAY_H__


#include "support/data_support/ccCArray.h"


/**

 * @addtogroup data_structures

 * @{

 */


/** @def CCARRAY_FOREACH

A convenience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface.

@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

*/

#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


#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)



NS_CC_BEGIN


class CC_DLL CCArray : public CCObject

{

public:

    ~CCArray();


    /** Create an array */

    static CCArray* create();

    /** Create an array with some objects */

    static CCArray* create(CCObject* pObject, ...);

    /** Create an array with one object */

    static CCArray* createWithObject(CCObject* pObject);

    /** Create an array with capacity */

    static CCArray* createWithCapacity(unsigned int capacity); //capacity 容量

    /** Create an array with an existing array */

    static CCArray* createWithArray(CCArray* otherArray);

    /**

     @brief   Generate a CCArray pointer by file

     @param   pFileName  The file name of *.plist file

     @return  The CCArray pointer generated from the file

     */

    static CCArray* createWithContentsOfFile(const char* pFileName);

    

    /*

     @brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the

     invoker should call release().

     */

    static CCArray* createWithContentsOfFileThreadSafe(const char* pFileName);


    /** 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() const;

    /** Returns capacity of the array */

    unsigned int capacity() const;

    /** Returns index of a certain object, return UINT_MAX if doesn't contain the object */

    unsigned int indexOfObject(CCObject* object) const;

    /** 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) const;

    /** @since 1.1 */

    bool isEqualToArray(CCArray* pOtherArray);

    // 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);


    /** Replace object at index with another object. */

    void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true);


    /** Revers the array */

    void reverseObjects();

    /* Shrinks the array so the memory footprint corresponds with the number of items */

    void reduceMemoryFootprint();

  

    /* override functions */

    virtual CCObject* copyWithZone(CCZone* pZone);


    /* override functions */

    virtual void acceptVisitor(CCDataVisitor &visitor);


public:

    ccArray* data;

    CCArray();

    CCArray(unsigned int capacity);

};


// end of data_structure group

/// @}


NS_CC_END


#endif // __CCARRAY_H__


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值