cococococococos 2 d-xノード(CCAray.h)API
15664 ワード
本論文はhttp://blog.csdn.net/runaying ,引用は出典を明記しなければならない!
cococococococos 2 d-xノード(CCAray.h)API
みなさんがよりよく勉強できるように、私のブログを見ることを強くおすすめします. Cocos 2 d-X権威ガイドノート
このクラスは非常によく使われています.どのオブジェクトも記憶できます.Cのポインタをシミュレートできます.
cococococococos 2 d-xノード(CCAray.h)API
みなさんがよりよく勉強できるように、私のブログを見ることを強くおすすめします. Cocos 2 d-X権威ガイドノート
このクラスは非常によく使われています.どのオブジェクトも記憶できます.Cのポインタをシミュレートできます.
///\cocos2d-x-3.0alpha0\cocos2dx\cocoa
// , , C ,
#ifndef __CCARRAY_H__
#define __CCARRAY_H__
#define CC_USE_ARRAY_VECTOR 0
#if CC_USE_ARRAY_VECTOR
#include <vector>
#include <algorithm>
#include "cocoa/CCObject.h"
#include "ccMacros.h"
#else
#include "support/data_support/ccCArray.h"
#endif
#if CC_USE_ARRAY_VECTOR
/**
* RCBase
* C
* Original code: http://www.codeproject.com/Articles/64111/Building-a-Quick-and-Handy-Reference-Counting-Clas //
* License: http://www.codeproject.com/info/cpol10.aspx //
*/
template < class T >
class RCPtr
{
public:
// C
//e.g. RCPtr< T > x = new T();
RCPtr(T* ptr = nullptr)
: _ptr(ptr)
{
if(ptr != nullptr) {ptr->retain();}
}
//Copy
RCPtr(const RCPtr &ptr)
: _ptr(ptr._ptr)
{
// printf("Array: copy constructor: %p
", this);
if(_ptr != NULL) {_ptr->retain();}
}
//Move
RCPtr(RCPtr &&ptr)
: _ptr(ptr._ptr)
{
// printf("Array: Move Constructor: %p
", this);
ptr._ptr = nullptr;
}
~RCPtr()
{
// printf("Array: Destructor: %p
", this);
if(_ptr != nullptr) {_ptr->release();}
}
//
//e.g. x = new T();
RCPtr &operator=(T* ptr)
{
// printf("Array: operator= T*: %p
", this);
//
// ptr == _ptr
// David Garlisch )
if(ptr != nullptr) {ptr->retain();}
if(_ptr != nullptr) {_ptr->release();}
_ptr = ptr;
return (*this);
}
// RCPtr
RCPtr &operator=(const RCPtr &ptr)
{
// printf("Array: operator= const&: %p
", this);
return (*this) = ptr._ptr;
}
//Retrieve( )
T* get() const
{
return _ptr;
}
// operators RCPtr
// C .
// operators,
//get() .
T* operator->() const {return _ptr;} //x->member
T &operator*() const {return *_ptr;} //*x, (*x).member
explicit operator T*() const {return _ptr;} //T* y = x;
explicit operator bool() const {return _ptr != nullptr;} //if(x) {/*x is not NULL*/}
bool operator==(const RCPtr &ptr) {return _ptr == ptr._ptr;}
bool operator==(const T *ptr) {return _ptr == ptr;}
private:
T *_ptr; //Actual pointer( )
};
#endif // CC_USE_ARRAY_VECTOR
/**
* @addtogroup data_structures
* @{
*/
/** @def CCARRAY_FOREACH
Array . "fast enumeration" .
@since v0.99.4
*/
/*
In cocos2d-iphone 1.0.0, macro( ) ; :
#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__++)
C++ . 1.0.0-rc3 . ---By Bin
*/
#if CC_USE_ARRAY_VECTOR
#define CCARRAY_FOREACH(__array__, __object__) \
if (__array__) \
for( auto __it__ = (__array__)->data.begin(); \
__it__ != (__array__)->data.end() && ((__object__) = __it__->get()) != nullptr; \
++__it__)
#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \
if (__array__) \
for( auto __it__ = (__array__)->data.rbegin(); \
__it__ != (__array__)->data.rend() && ((__object__) = __it__->get()) != nullptr; \
++__it__ )
#define CCARRAY_VERIFY_TYPE(__array__, __type__) void(0)
#else // ! CC_USE_ARRAY_VECTOR --------------------------
#define CCARRAY_FOREACH(__array__, __object__) \
if ((__array__) && (__array__)->data->num > 0) \
for(Object** __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(Object** __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(Object** __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
#endif // ! CC_USE_ARRAY_VECTOR
// Common defines ----------------------------------------------------------------------------------------------- //
#define arrayMakeObjectsPerformSelector(pArray, func, elementType) \
do { \
if(pArray && pArray->count() > 0) \
{ \
Object* child; \
CCARRAY_FOREACH(pArray, child) \
{ \
elementType pNode = static_cast<elementType>(child); \
if(pNode) \
{ \
pNode->func(); \
} \
} \
} \
} \
while(false)
#define arrayMakeObjectsPerformSelectorWithObject(pArray, func, object, elementType) \
do { \
if(pArray && pArray->count() > 0) \
{ \
Object* child; \
CCARRAY_FOREACH(pArray, child) \
{ \
elementType pNode = static_cast<elementType>(child); \
if(pNode) \
{ \
pNode->func(object); \
} \
} \
} \
} \
while(false)
NS_CC_BEGIN
class CC_DLL Array : public Object, public Clonable
{
public:
/** array. 10
* @js NA
* @lua NA
*/
static Array* create();
/** objects array
* @js NA
*/
static Array* create(Object* object, ...) CC_REQUIRES_NULL_TERMINATION;
/** object array
* @js NA
*/
static Array* createWithObject(Object* object);
/** array
* @js NA
*/
static Array* createWithCapacity(int capacity);
/** array array
* @js NA
*/
static Array* createWithArray(Array* otherArray);
/**
@brief file array
@param pFileName *.plist
@return file array
* @js NA
*/
static Array* createWithContentsOfFile(const char* pFileName);
/*
@brief arrayWithContentsOfFile() , ,
release()。
* @js NA
* @lua NA
*/
static Array* createWithContentsOfFileThreadSafe(const char* pFileName);
/**
* @js NA
* @lua NA
*/
~Array();
/** array
* @js NA
* @lua NA
*/
bool init();
/** object array
* @js NA
* @lua NA
*/
bool initWithObject(Object* object);
/** objects array
* @js NA
* @lua NA
*/
bool initWithObjects(Object* object, ...) CC_REQUIRES_NULL_TERMINATION;
/** array
* @js NA
* @lua NA
*/
bool initWithCapacity(int capacity);
/** array array
* @js NA
* @lua NA
*/
bool initWithArray(Array* otherArray);
// Array
/** Returns array
* @js NA
*/
int count() const
{
#if CC_USE_ARRAY_VECTOR
return data.size();
#else
return data->num;
#endif
}
/** Returns array
* @js NA
*/
int capacity() const
{
#if CC_USE_ARRAY_VECTOR
return data.capacity();
#else
return data->max;
#endif
}
/** Returns certain( ) , return UINT_MAX
* @js NA
* @lua NA
*/
int getIndexOfObject(Object* object) const;
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE int indexOfObject(Object* object) const { return getIndexOfObject(object); }
/** Returns certain
* @js NA
* @lua NA
*/
Object* getObjectAtIndex(int index)
{
CCASSERT(index>=0 && index < count(), "index out of range in getObjectAtIndex()");
#if CC_USE_ARRAY_VECTOR
return data[index].get();
#else
return data->arr[index];
#endif
}
CC_DEPRECATED_ATTRIBUTE Object* objectAtIndex(int index) { return getObjectAtIndex(index); }
/** Returns array
* @js NA
*/
Object* getLastObject()
{
#if CC_USE_ARRAY_VECTOR
return data.back().get();
#else
if(data->num > 0)
return data->arr[data->num-1];
return nullptr;
#endif
}
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE Object* lastObject() { return getLastObject(); }
/** Returns element
* @js NA
* @lua NA
*/
Object* getRandomObject();
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE Object* randomObject() { return getRandomObject(); }
/** Returns object array .
* @js NA
*/
bool containsObject(Object* object) const;
/** @since 1.1
* @js NA
*/
bool isEqualToArray(Array* otherArray);
// Adding Objects
/** Add object
* @js NA
*/
void addObject(Object* object);
/**
* @js NA
*/
/** Add array
* @js NA
*/
void addObjectsFromArray(Array* otherArray);
/** Insert a certain object at a certain index
* @js NA
*/
void insertObject(Object* object, int index);
/** sets object
* @js NA
* @lua NA
*/
void setObject(Object* object, int index);
/** sets object , without retaining( ) .
* @js NA
* @lua NA
*/
void fastSetObject(Object* object, int index)
{
#if CC_USE_ARRAY_VECTOR
setObject(object, index);
#else
// no retain
data->arr[index] = object;
#endif
}
/**
* @js NA
* @lua NA
*/
void swap( int indexOne, int indexTwo )
{
CCASSERT(indexOne >=0 && indexOne < count() && indexTwo >= 0 && indexTwo < count(), "Invalid indices");
#if CC_USE_ARRAY_VECTOR
std::swap(data[indexOne], data[indexTwo]);
#else
std::swap(data->arr[indexOne], data->arr[indexTwo]);
#endif
}
// Removing Objects
/** Remove object
* @js NA
*/
void removeLastObject(bool releaseObj = true);
/** Remove object
* @js NA
*/
void removeObject(Object* object, bool releaseObj = true);
/** Remove certain
* @js NA
*/
void removeObjectAtIndex(int index, bool releaseObj = true);
/** Remove all elements
* @js NA
*/
void removeObjectsInArray(Array* otherArray);
/** Remove all objects
* @js NA
*/
void removeAllObjects();
/**
* @js NA
*/
void fastRemoveObject(Object* object);
/** certain
* @js NA
*/
void fastRemoveObjectAtIndex(int index);
// Rearranging Content
/**
* @js NA
*/
void exchangeObject(Object* object1, Object* object2);
/** 3 certain
* @js NA
*/
void exchangeObjectAtIndex(int index1, int index2);
/**
* @js NA
*/
void replaceObjectAtIndex(int index, Object* object, bool releaseObject = true);
/** Revers( ) the array
* @js NA
*/
void reverseObjects();
/* array , items
* @js NA
*/
void reduceMemoryFootprint();
/* override functions
* @js NA
*/
virtual void acceptVisitor(DataVisitor &visitor);
/**
* @js NA
* @lua NA
*/
virtual Array* clone() const;
// ------------------------------------------
// Iterators
// ------------------------------------------
#if CC_USE_ARRAY_VECTOR
typedef std::vector<RCPtr<Object>>::iterator iterator;
typedef std::vector<RCPtr<Object>>::const_iterator const_iterator;
/**
* @js NA
* @lua NA
*/
iterator begin() { return data.begin(); }
/**
* @js NA
* @lua NA
*/
iterator end() { return data.end(); }
const_iterator cbegin() { return data.cbegin(); }
/**
* @js NA
* @lua NA
*/
const_iterator cend() { return data.cend(); }
std::vector<RCPtr<Object>> data;
#else
/**
* @js NA
* @lua NA
*/
Object** begin() { return &data->arr[0]; }
/**
* @js NA
* @lua NA
*/
Object** end() { return &data->arr[data->num]; }
ccArray* data;
#endif
//protected:
/**
* @js NA
* @lua NA
*/
Array();
};
// end of data_structure group
/// @}
NS_CC_END
#endif // __CCARRAY_H__