123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- #ifndef __CCARRAY_H__
- #define __CCARRAY_H__
- #define CC_USE_ARRAY_VECTOR 0
- #if CC_USE_ARRAY_VECTOR
- #include <vector>
- #include <algorithm>
- #include "base/CCRef.h"
- #include "base/ccMacros.h"
- #else
- #include "base/ccCArray.h"
- #endif
- #include "base/CCDataVisitor.h"
- #if CC_USE_ARRAY_VECTOR
- template < class T >
- class RCPtr
- {
- public:
-
-
- RCPtr(T* ptr = nullptr)
- : _ptr(ptr)
- {
- if(ptr != nullptr) {ptr->retain();}
- }
-
- RCPtr(const RCPtr &ptr)
- : _ptr(ptr._ptr)
- {
- if(_ptr != NULL) {_ptr->retain();}
- }
-
- RCPtr(RCPtr &&ptr)
- : _ptr(ptr._ptr)
- {
- ptr._ptr = nullptr;
- }
- ~RCPtr()
- {
- if(_ptr != nullptr) {_ptr->release();}
- }
-
-
- RCPtr &operator=(T* ptr)
- {
-
-
-
- if(ptr != nullptr) {ptr->retain();}
- if(_ptr != nullptr) {_ptr->release();}
- _ptr = ptr;
- return (*this);
- }
-
- RCPtr &operator=(const RCPtr &ptr)
- {
- return (*this) = ptr._ptr;
- }
-
- T* get() const
- {
- return _ptr;
- }
-
-
-
-
- T* operator->() const {return _ptr;}
- T &operator*() const {return *_ptr;}
- explicit operator T*() const {return _ptr;}
- explicit operator bool() const {return _ptr != nullptr;}
- bool operator==(const RCPtr &ptr) {return _ptr == ptr._ptr;}
- bool operator==(const T *ptr) {return _ptr == ptr;}
- private:
- T *_ptr;
- };
- #endif
- #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
- #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; \
- __arr__ <= __end__ && (((__object__) = *__arr__) != NULL); \
- __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; \
- __arr__ >= __end__ && (((__object__) = *__arr__) != NULL); \
- __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, \
- **__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
- #define arrayMakeObjectsPerformSelector(pArray, func, elementType) \
- do { \
- if(pArray && pArray->count() > 0) \
- { \
- Ref* 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) \
- { \
- Ref* 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 Ref, public Clonable
- {
- public:
-
- static __Array* create();
-
- static __Array* create(Ref* object, ...) CC_REQUIRES_NULL_TERMINATION;
-
- static __Array* createWithObject(Ref* object);
-
- static __Array* createWithCapacity(ssize_t capacity);
-
- static __Array* createWithArray(__Array* otherArray);
-
- static __Array* createWithContentsOfFile(const std::string& pFileName);
-
-
- static __Array* createWithContentsOfFileThreadSafe(const std::string& pFileName);
-
- ~__Array();
-
- bool init();
-
- bool initWithObject(Ref* object);
-
- bool initWithObjects(Ref* object, ...) CC_REQUIRES_NULL_TERMINATION;
-
- bool initWithCapacity(ssize_t capacity);
-
- bool initWithArray(__Array* otherArray);
-
-
- ssize_t count() const
- {
- #if CC_USE_ARRAY_VECTOR
- return data.size();
- #else
- return data->num;
- #endif
- }
-
- ssize_t capacity() const
- {
- #if CC_USE_ARRAY_VECTOR
- return data.capacity();
- #else
- return data->max;
- #endif
- }
-
- ssize_t getIndexOfObject(Ref* object) const;
-
- CC_DEPRECATED_ATTRIBUTE ssize_t indexOfObject(Ref* object) const { return getIndexOfObject(object); }
-
- Ref* getObjectAtIndex(ssize_t 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 Ref* objectAtIndex(ssize_t index) { return getObjectAtIndex(index); }
-
- Ref* getLastObject()
- {
- #if CC_USE_ARRAY_VECTOR
- return data.back().get();
- #else
- if(data->num > 0)
- return data->arr[data->num-1];
-
- return nullptr;
- #endif
- }
-
- CC_DEPRECATED_ATTRIBUTE Ref* lastObject() { return getLastObject(); }
-
- Ref* getRandomObject();
-
- CC_DEPRECATED_ATTRIBUTE Ref* randomObject() { return getRandomObject(); }
-
- bool containsObject(Ref* object) const;
-
- bool isEqualToArray(__Array* otherArray);
-
-
- void addObject(Ref* object);
-
-
- void addObjectsFromArray(__Array* otherArray);
-
- void insertObject(Ref* object, ssize_t index);
-
- void setObject(Ref* object, ssize_t index);
-
- void fastSetObject(Ref* object, ssize_t index)
- {
- #if CC_USE_ARRAY_VECTOR
- setObject(object, index);
- #else
-
- data->arr[index] = object;
- #endif
- }
-
- void swap( ssize_t indexOne, ssize_t 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
- }
-
-
- void removeLastObject(bool releaseObj = true);
-
- void removeObject(Ref* object, bool releaseObj = true);
-
- void removeObjectAtIndex(ssize_t index, bool releaseObj = true);
-
- void removeObjectsInArray(__Array* otherArray);
-
- void removeAllObjects();
-
- void fastRemoveObject(Ref* object);
-
- void fastRemoveObjectAtIndex(ssize_t index);
-
-
- void exchangeObject(Ref* object1, Ref* object2);
-
- void exchangeObjectAtIndex(ssize_t index1, ssize_t index2);
-
- void replaceObjectAtIndex(ssize_t index, Ref* object, bool releaseObject = true);
-
- void reverseObjects();
-
- void reduceMemoryFootprint();
-
-
- virtual void acceptVisitor(DataVisitor &visitor);
-
- virtual __Array* clone() const override;
-
-
-
- #if CC_USE_ARRAY_VECTOR
- typedef std::vector<RCPtr<Object>>::iterator iterator;
- typedef std::vector<RCPtr<Object>>::const_iterator const_iterator;
-
- iterator begin() { return data.begin(); }
-
- iterator end() { return data.end(); }
- const_iterator cbegin() { return data.cbegin(); }
-
- const_iterator cend() { return data.cend(); }
- std::vector<RCPtr<Object>> data;
- #else
-
- Ref** begin() { return &data->arr[0]; }
-
- Ref** end() { return &data->arr[data->num]; }
- ccArray* data;
- #endif
-
- __Array();
- };
- NS_CC_END
- #endif
|