axmol/cocos/deprecated/CCArray.h

563 lines
17 KiB
C
Raw Normal View History

2013-10-12 15:25:45 +08:00
/****************************************************************************
Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
Copyright (c) 2010-2012 cocos2d-x.org
2015-03-24 20:23:51 +08:00
Copyright (c) 2013-2015 Chukong Technologies Inc.
2013-10-12 15:25:45 +08:00
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.
****************************************************************************/
2015-03-24 20:23:51 +08:00
2013-10-12 15:25:45 +08:00
#ifndef __CCARRAY_H__
#define __CCARRAY_H__
2015-03-24 20:23:51 +08:00
/// @cond DO_NOT_SHOW
2013-10-12 15:25:45 +08:00
#define CC_USE_ARRAY_VECTOR 0
#if CC_USE_ARRAY_VECTOR
#include <vector>
#include <algorithm>
#include "base/CCRef.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
2013-10-12 15:25:45 +08:00
#else
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/ccCArray.h"
2013-10-12 15:25:45 +08:00
#endif
#include "base/CCDataVisitor.h"
2013-10-12 15:25:45 +08:00
#if CC_USE_ARRAY_VECTOR
/**
* A reference counting-managed pointer for classes derived from RCBase which can
* be used as C pointer
* 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:
//Construct using a C pointer
//e.g. RCPtr< T > x = new (std::nothrow) T();
2013-10-12 15:25:45 +08:00
RCPtr(T* ptr = nullptr)
: _ptr(ptr)
{
if(ptr != nullptr) {ptr->retain();}
}
//Copy constructor
RCPtr(const RCPtr &ptr)
: _ptr(ptr._ptr)
{
// printf("Array: copy constructor: %p\n", this);
if(_ptr != NULL) {_ptr->retain();}
}
//Move constructor
RCPtr(RCPtr &&ptr)
: _ptr(ptr._ptr)
{
// printf("Array: Move Constructor: %p\n", this);
ptr._ptr = nullptr;
}
~RCPtr()
{
// printf("Array: Destructor: %p\n", this);
if(_ptr != nullptr) {_ptr->release();}
}
//Assign a pointer
//e.g. x = new (std::nothrow) T();
2013-10-12 15:25:45 +08:00
RCPtr &operator=(T* ptr)
{
// printf("Array: operator= T*: %p\n", this);
//The following grab and release operations have to be performed
//in that order to handle the case where ptr == _ptr
//(See comment below by David Garlisch)
if(ptr != nullptr) {ptr->retain();}
if(_ptr != nullptr) {_ptr->release();}
_ptr = ptr;
return (*this);
}
//Assign another RCPtr
RCPtr &operator=(const RCPtr &ptr)
{
// printf("Array: operator= const&: %p\n", this);
return (*this) = ptr._ptr;
}
//Retrieve actual pointer
T* get() const
{
return _ptr;
}
//Some overloaded operators to facilitate dealing with an RCPtr
//as a conventional C pointer.
//Without these operators, one can still use the less transparent
//get() method to access the pointer.
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
A convenience macro to iterate over a Array 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
*/
#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(Ref** __arr__ = (__array__)->data->arr, **__end__ = (__array__)->data->arr + (__array__)->data->num-1; \
2013-10-12 15:25:45 +08:00
__arr__ <= __end__ && (((__object__) = *__arr__) != NULL/* || true*/); \
__arr__++)
#define CCARRAY_FOREACH_REVERSE(__array__, __object__) \
if ((__array__) && (__array__)->data->num > 0) \
for(Ref** __arr__ = (__array__)->data->arr + (__array__)->data->num-1, **__end__ = (__array__)->data->arr; \
2013-10-12 15:25:45 +08:00
__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(Ref** __arr__ = (__array__)->data->arr, \
2013-10-12 15:25:45 +08:00
**__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) \
{ \
Ref* child; \
2013-10-12 15:25:45 +08:00
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) \
{ \
Ref* child; \
2013-10-12 15:25:45 +08:00
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 Ref, public Clonable
2013-10-12 15:25:45 +08:00
{
public:
/** Creates an empty array. Default capacity is 10
* @js NA
* @lua NA
*/
2013-12-07 10:48:02 +08:00
static __Array* create();
2013-10-12 15:25:45 +08:00
/** Create an array with objects
* @js NA
*/
static __Array* create(Ref* object, ...) CC_REQUIRES_NULL_TERMINATION;
2013-10-12 15:25:45 +08:00
/** Create an array with one object
* @js NA
*/
static __Array* createWithObject(Ref* object);
2013-10-12 15:25:45 +08:00
/** Create an array with a default capacity
* @js NA
*/
static __Array* createWithCapacity(ssize_t capacity);
2013-10-12 15:25:45 +08:00
/** Create an array with from an existing array
* @js NA
*/
2013-12-07 10:48:02 +08:00
static __Array* createWithArray(__Array* otherArray);
2013-10-12 15:25:45 +08:00
/**
@brief Generate a Array pointer by file
@param pFileName The file name of *.plist file
@return The Array pointer generated from the file
* @js NA
*/
static __Array* createWithContentsOfFile(const std::string& pFileName);
2013-10-12 15:25:45 +08:00
/*
@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the
invoker should call release().
* @js NA
* @lua NA
*/
static __Array* createWithContentsOfFileThreadSafe(const std::string& pFileName);
2013-10-12 15:25:45 +08:00
/**
* @js NA
* @lua NA
*/
2013-12-07 10:48:02 +08:00
~__Array();
2013-10-12 15:25:45 +08:00
/** Initializes an array
* @js NA
* @lua NA
*/
bool init();
/** Initializes an array with one object
* @js NA
* @lua NA
*/
bool initWithObject(Ref* object);
2013-10-12 15:25:45 +08:00
/** Initializes an array with some objects
* @js NA
* @lua NA
*/
bool initWithObjects(Ref* object, ...) CC_REQUIRES_NULL_TERMINATION;
2013-10-12 15:25:45 +08:00
/** Initializes an array with capacity
* @js NA
* @lua NA
*/
bool initWithCapacity(ssize_t capacity);
2013-10-12 15:25:45 +08:00
/** Initializes an array with an existing array
* @js NA
* @lua NA
*/
2013-12-07 10:48:02 +08:00
bool initWithArray(__Array* otherArray);
2013-10-12 15:25:45 +08:00
// Querying an Array
/** Returns element count of the array
* @js NA
*/
ssize_t count() const
2013-10-12 15:25:45 +08:00
{
#if CC_USE_ARRAY_VECTOR
return data.size();
#else
return data->num;
#endif
}
/** Returns capacity of the array
* @js NA
*/
ssize_t capacity() const
2013-10-12 15:25:45 +08:00
{
#if CC_USE_ARRAY_VECTOR
return data.capacity();
#else
return data->max;
#endif
}
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object
* @js NA
* @lua NA
*/
ssize_t getIndexOfObject(Ref* object) const;
2013-10-12 15:25:45 +08:00
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE ssize_t indexOfObject(Ref* object) const { return getIndexOfObject(object); }
2013-10-12 15:25:45 +08:00
/** Returns an element with a certain index
* @js NA
* @lua NA
*/
Ref* getObjectAtIndex(ssize_t index)
2013-10-12 15:25:45 +08:00
{
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 Ref* objectAtIndex(ssize_t index) { return getObjectAtIndex(index); }
2013-10-12 15:25:45 +08:00
/** Returns the last element of the array
* @js NA
*/
Ref* getLastObject()
2013-10-12 15:25:45 +08:00
{
#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 Ref* lastObject() { return getLastObject(); }
2013-10-12 15:25:45 +08:00
/** Returns a random element
* @js NA
* @lua NA
*/
Ref* getRandomObject();
2013-10-12 15:25:45 +08:00
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE Ref* randomObject() { return getRandomObject(); }
2013-10-12 15:25:45 +08:00
/** Returns a Boolean value that indicates whether object is present in array.
* @js NA
*/
bool containsObject(Ref* object) const;
2013-10-12 15:25:45 +08:00
/** @since 1.1
* @js NA
*/
2013-12-07 10:48:02 +08:00
bool isEqualToArray(__Array* otherArray);
2013-10-12 15:25:45 +08:00
// Adding Objects
/** Add a certain object
* @js NA
*/
void addObject(Ref* object);
2013-10-12 15:25:45 +08:00
/**
* @js NA
*/
/** Add all elements of an existing array
* @js NA
*/
2013-12-07 10:48:02 +08:00
void addObjectsFromArray(__Array* otherArray);
2013-10-12 15:25:45 +08:00
/** Insert a certain object at a certain index
* @js NA
*/
void insertObject(Ref* object, ssize_t index);
2013-10-12 15:25:45 +08:00
/** sets a certain object at a certain index
* @js NA
* @lua NA
*/
void setObject(Ref* object, ssize_t index);
2013-10-12 15:25:45 +08:00
/** sets a certain object at a certain index without retaining. Use it with caution
* @js NA
* @lua NA
*/
void fastSetObject(Ref* object, ssize_t index)
2013-10-12 15:25:45 +08:00
{
#if CC_USE_ARRAY_VECTOR
setObject(object, index);
#else
// no retain
data->arr[index] = object;
#endif
}
/**
* @js NA
* @lua NA
*/
void swap( ssize_t indexOne, ssize_t indexTwo )
2013-10-12 15:25:45 +08:00
{
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 last object
* @js NA
*/
void removeLastObject(bool releaseObj = true);
/** Remove a certain object
* @js NA
*/
void removeObject(Ref* object, bool releaseObj = true);
2013-10-12 15:25:45 +08:00
/** Remove an element with a certain index
* @js NA
*/
void removeObjectAtIndex(ssize_t index, bool releaseObj = true);
2013-10-12 15:25:45 +08:00
/** Remove all elements
* @js NA
*/
2013-12-07 10:48:02 +08:00
void removeObjectsInArray(__Array* otherArray);
2013-10-12 15:25:45 +08:00
/** Remove all objects
* @js NA
*/
void removeAllObjects();
/** Fast way to remove a certain object
* @js NA
*/
void fastRemoveObject(Ref* object);
2013-10-12 15:25:45 +08:00
/** Fast way to remove an element with a certain index
* @js NA
*/
void fastRemoveObjectAtIndex(ssize_t index);
2013-10-12 15:25:45 +08:00
// Rearranging Content
/** Swap two elements
* @js NA
*/
void exchangeObject(Ref* object1, Ref* object2);
2013-10-12 15:25:45 +08:00
/** Swap two elements with certain indexes
* @js NA
*/
void exchangeObjectAtIndex(ssize_t index1, ssize_t index2);
2013-10-12 15:25:45 +08:00
/** Replace object at index with another object.
* @js NA
*/
void replaceObjectAtIndex(ssize_t index, Ref* object, bool releaseObject = true);
2013-10-12 15:25:45 +08:00
/** Revers the array
* @js NA
*/
void reverseObjects();
/* Shrinks the array so the memory footprint corresponds with the number of items
* @js NA
*/
void reduceMemoryFootprint();
/* override functions
* @js NA
*/
virtual void acceptVisitor(DataVisitor &visitor);
/**
* @js NA
* @lua NA
*/
2013-12-07 10:48:02 +08:00
virtual __Array* clone() const;
2013-10-12 15:25:45 +08:00
// ------------------------------------------
// 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
*/
Ref** begin() { return &data->arr[0]; }
2013-10-12 15:25:45 +08:00
/**
* @js NA
* @lua NA
*/
Ref** end() { return &data->arr[data->num]; }
2013-10-12 15:25:45 +08:00
ccArray* data;
#endif
//protected:
/**
* @js NA
* @lua NA
*/
2013-12-07 10:48:02 +08:00
__Array();
2013-10-12 15:25:45 +08:00
};
// end of data_structure group
/// @}
NS_CC_END
2015-03-24 20:23:51 +08:00
/// @endcond
2013-10-12 15:25:45 +08:00
#endif // __CCARRAY_H__