axmol/cocos/base/CCSet.cpp

167 lines
3.2 KiB
C++
Raw Normal View History

2013-10-12 15:25:45 +08:00
/****************************************************************************
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.
****************************************************************************/
#include "CCSet.h"
using namespace std;
NS_CC_BEGIN
2013-12-06 18:16:58 +08:00
__Set::__Set(void)
2013-10-12 15:25:45 +08:00
{
_set = new set<Object *>;
}
2013-12-06 18:16:58 +08:00
__Set::__Set(const __Set &r__SetObject)
2013-10-12 15:25:45 +08:00
{
2013-12-06 18:16:58 +08:00
_set = new set<Object *>(*r__SetObject._set);
2013-10-12 15:25:45 +08:00
// call retain of members
2013-12-06 18:16:58 +08:00
__SetIterator iter;
2013-10-12 15:25:45 +08:00
for (iter = _set->begin(); iter != _set->end(); ++iter)
{
if (! (*iter))
{
break;
}
(*iter)->retain();
}
}
2013-12-06 18:16:58 +08:00
__Set::~__Set(void)
2013-10-12 15:25:45 +08:00
{
removeAllObjects();
CC_SAFE_DELETE(_set);
}
2013-12-06 18:16:58 +08:00
void __Set::acceptVisitor(DataVisitor &visitor)
2013-10-12 15:25:45 +08:00
{
visitor.visit(this);
}
2013-12-06 18:16:58 +08:00
__Set * __Set::create()
2013-10-12 15:25:45 +08:00
{
2013-12-06 18:16:58 +08:00
__Set * pRet = new __Set();
2013-10-12 15:25:45 +08:00
if (pRet != NULL)
{
pRet->autorelease();
}
return pRet;
}
2013-12-06 18:16:58 +08:00
__Set* __Set::copy(void)
2013-10-12 15:25:45 +08:00
{
2013-12-06 18:16:58 +08:00
__Set *p__Set = new __Set(*this);
2013-10-12 15:25:45 +08:00
2013-12-06 18:16:58 +08:00
return p__Set;
2013-10-12 15:25:45 +08:00
}
2013-12-06 18:16:58 +08:00
__Set* __Set::mutableCopy(void)
2013-10-12 15:25:45 +08:00
{
return copy();
}
2013-12-06 18:16:58 +08:00
int __Set::count(void)
2013-10-12 15:25:45 +08:00
{
return (int)_set->size();
}
2013-12-06 18:16:58 +08:00
void __Set::addObject(Object *pObject)
2013-10-12 15:25:45 +08:00
{
if (_set->count(pObject) == 0)
{
CC_SAFE_RETAIN(pObject);
_set->insert(pObject);
}
}
2013-12-06 18:16:58 +08:00
void __Set::removeObject(Object *pObject)
2013-10-12 15:25:45 +08:00
{
if (_set->erase(pObject) > 0)
{
CC_SAFE_RELEASE(pObject);
}
}
2013-12-06 18:16:58 +08:00
void __Set::removeAllObjects()
2013-10-12 15:25:45 +08:00
{
2013-12-06 18:16:58 +08:00
__SetIterator it = _set->begin();
__SetIterator tmp;
2013-10-12 15:25:45 +08:00
while (it != _set->end())
{
if (!(*it))
{
break;
}
tmp = it;
++tmp;
Object * obj = *it;
_set->erase(it);
CC_SAFE_RELEASE(obj);
it = tmp;
}
}
2013-12-06 18:16:58 +08:00
bool __Set::containsObject(Object *pObject)
2013-10-12 15:25:45 +08:00
{
return _set->find(pObject) != _set->end();
}
2013-12-06 18:16:58 +08:00
__SetIterator __Set::begin(void)
2013-10-12 15:25:45 +08:00
{
return _set->begin();
}
2013-12-06 18:16:58 +08:00
__SetIterator __Set::end(void)
2013-10-12 15:25:45 +08:00
{
return _set->end();
}
2013-12-06 18:16:58 +08:00
Object* __Set::anyObject()
2013-10-12 15:25:45 +08:00
{
if (!_set || _set->empty())
{
return 0;
}
2013-12-06 18:16:58 +08:00
__SetIterator it;
2013-10-12 15:25:45 +08:00
for( it = _set->begin(); it != _set->end(); ++it)
{
if (*it)
{
return (*it);
}
}
return 0;
}
NS_CC_END