2010-09-06 14:54:14 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __NSMUTABLE_DICTIONARY_H__
|
|
|
|
#define __NSMUTABLE_DICTIONARY_H__
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2010-09-10 14:08:25 +08:00
|
|
|
#include <string>
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCMutableArray.h"
|
2011-01-15 18:05:35 +08:00
|
|
|
#include "ccMacros.h"
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
namespace cocos2d {
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCString;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
template<class _T, class _ValueT = CCObject*>
|
|
|
|
class CCMutableDictionary : public CCObject
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
typedef std::map<_T, _ValueT> CCObjectMap;
|
|
|
|
typedef typename CCObjectMap::iterator CCObjectMapIter;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef pair<_T, _ValueT> Int_Pair;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMap m_Map;
|
2010-08-04 16:54:58 +08:00
|
|
|
bool m_bBegin;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter m_MapIter;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
public:
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableDictionary(void)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
m_bBegin = false;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
~CCMutableDictionary(void)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return the number of items
|
|
|
|
unsigned int count()
|
|
|
|
{
|
|
|
|
return m_Map.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return all the keys
|
|
|
|
std::vector<std::string> allKeys()
|
|
|
|
{
|
|
|
|
std::vector<std::string> tRet;
|
2010-08-18 12:03:31 +08:00
|
|
|
if (m_Map.size() > 0)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter it;
|
2010-08-18 12:03:31 +08:00
|
|
|
for( it = m_Map.begin(); it != m_Map.end(); ++it)
|
|
|
|
{
|
|
|
|
tRet.push_back(it->first);
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
return tRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @warning : We use '==' to compare two objects*/
|
|
|
|
std::vector<std::string> allKeysForObject(_ValueT object)
|
|
|
|
{
|
|
|
|
std::vector<std::string> tRet;
|
2010-08-18 12:03:31 +08:00
|
|
|
if (m_Map.size() > 0)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter it;
|
2010-08-18 12:03:31 +08:00
|
|
|
for( it= m_Map.begin(); it != m_Map.end(); ++it)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2010-08-18 12:03:31 +08:00
|
|
|
if (it->second == object)
|
|
|
|
{
|
|
|
|
tRet.push_back(it->first);
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ValueT objectForKey(_T key) ///<
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter it;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
it = m_Map.find(key);
|
|
|
|
|
|
|
|
if(it == m_Map.end()) //no match case
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool setObject(_ValueT pObject, _T key)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
pair<CCObjectMapIter, bool > pr;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
pr = m_Map.insert( Int_Pair(key, pObject) );
|
|
|
|
|
|
|
|
if(pr.second == true)
|
|
|
|
{
|
|
|
|
pObject->retain();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeObjectForKey(_T key)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter it;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
it = m_Map.find(key);
|
|
|
|
|
|
|
|
if(it == m_Map.end()) //no match case
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(it->second )
|
|
|
|
{
|
|
|
|
it->second->release() ;
|
|
|
|
m_Map.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool begin()
|
|
|
|
{
|
|
|
|
if(m_Map.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_MapIter = m_Map.begin();
|
|
|
|
m_bBegin = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ValueT next(_T* key = NULL)
|
|
|
|
{
|
|
|
|
if(!m_bBegin)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
_ValueT pObject = m_MapIter->second;
|
|
|
|
|
|
|
|
if(m_MapIter == m_Map.end())
|
|
|
|
{
|
|
|
|
m_bBegin = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(key)
|
|
|
|
{
|
|
|
|
*key = m_MapIter->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_MapIter++;
|
|
|
|
|
|
|
|
if(m_MapIter == m_Map.end())
|
|
|
|
{
|
|
|
|
m_bBegin = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void end()
|
|
|
|
{
|
|
|
|
m_bBegin = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeAllObjects()
|
|
|
|
{
|
2010-08-18 12:03:31 +08:00
|
|
|
if (m_Map.size() > 0)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObjectMapIter it;
|
2010-08-18 12:03:31 +08:00
|
|
|
for( it = m_Map.begin(); it != m_Map.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->second)
|
|
|
|
{
|
|
|
|
it->second->release();
|
|
|
|
}
|
|
|
|
}
|
2010-08-04 16:54:58 +08:00
|
|
|
}
|
|
|
|
m_Map.clear();
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
static CCMutableDictionary<_T, _ValueT>* dictionaryWithDictionary(CCMutableDictionary<_T, _ValueT>* srcDict)
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableDictionary<_T, _ValueT>* pNewDict = new CCMutableDictionary<_T, _ValueT>();
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
srcDict->begin();
|
|
|
|
|
|
|
|
_T key;
|
|
|
|
_ValueT value;
|
|
|
|
|
|
|
|
while( (value = srcDict->next(&key)) )
|
|
|
|
{
|
|
|
|
pNewDict->setObject(value, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
srcDict->end();
|
|
|
|
|
|
|
|
return pNewDict;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
#define CCDictionary CCMutableDictionary
|
|
|
|
typedef CCDictionary<std::string, CCString*> CCStringToStringDictionary;
|
2010-08-04 16:54:58 +08:00
|
|
|
}//namespace cocos2d
|
|
|
|
|
|
|
|
|
2010-09-06 14:54:14 +08:00
|
|
|
#endif //__NSMUTABLE_DICTIONARY_H__
|