2012-03-19 14:53:44 +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 __CCDICTIONARY_H__
|
|
|
|
#define __CCDICTIONARY_H__
|
|
|
|
|
|
|
|
#include "support/data_support/uthash.h"
|
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCArray.h"
|
2012-04-14 19:11:57 +08:00
|
|
|
#include "CCString.h"
|
2012-03-19 14:53:44 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
class CCDictionary;
|
|
|
|
|
|
|
|
class CC_DLL CCDictElement
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-04-14 19:11:57 +08:00
|
|
|
#define MAX_KEY_LEN 256
|
2012-03-20 15:04:53 +08:00
|
|
|
public:
|
|
|
|
CCDictElement(const char* pszKey, CCObject* pObject)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
m_pObject = pObject;
|
2012-04-14 19:11:57 +08:00
|
|
|
|
|
|
|
const char* pStart = pszKey;
|
|
|
|
|
|
|
|
int len = strlen(pszKey);
|
|
|
|
if (len > MAX_KEY_LEN )
|
|
|
|
{
|
|
|
|
char* pEnd = (char*)&pszKey[len-1];
|
|
|
|
pStart = pEnd - (MAX_KEY_LEN-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(m_szKey, pStart);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCDictElement(int iKey, CCObject* pObject)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
m_iKey = iKey;
|
|
|
|
m_pObject = pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char* getStrKey() const
|
|
|
|
{
|
2012-03-20 23:30:18 +08:00
|
|
|
CCAssert(m_szKey[0] != '\0', "Should not call this function for integer dictionary");
|
2012-03-20 15:04:53 +08:00
|
|
|
return m_szKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getIntKey() const
|
|
|
|
{
|
2012-03-20 23:30:18 +08:00
|
|
|
CCAssert(m_szKey[0] == '\0', "Should not call this function for string dictionary");
|
2012-03-20 15:04:53 +08:00
|
|
|
return m_iKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CCObject* getObject() const
|
|
|
|
{
|
|
|
|
return m_pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline void init()
|
|
|
|
{
|
|
|
|
m_iKey = 0;
|
|
|
|
m_pObject = NULL;
|
|
|
|
memset(m_szKey, 0, sizeof(m_szKey));
|
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-04-14 19:11:57 +08:00
|
|
|
char m_szKey[MAX_KEY_LEN+1]; /** hash key of string type*/
|
2012-03-20 23:30:18 +08:00
|
|
|
int m_iKey; /** hash key of integer type */
|
|
|
|
CCObject* m_pObject;/** hash value */
|
2012-03-20 15:04:53 +08:00
|
|
|
public:
|
|
|
|
UT_hash_handle hh; /* makes this class hashable */
|
|
|
|
friend class CCDictionary;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define CCDICT_FOREACH(__dict__, __el__) \
|
2012-03-20 21:08:52 +08:00
|
|
|
CCDictElement* pTmp##__dict__##__el__ = NULL; \
|
|
|
|
HASH_ITER(hh, (__dict__)->m_pElements, __el__, pTmp##__dict__##__el__)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CC_DLL CCDictionary : public CCObject
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictionary();
|
|
|
|
~CCDictionary();
|
2012-03-19 14:53:44 +08:00
|
|
|
|
|
|
|
/// return the number of items
|
|
|
|
unsigned int count();
|
|
|
|
|
|
|
|
/// return all the keys
|
|
|
|
CCArray* allKeys();
|
|
|
|
|
|
|
|
/** @warning : We use '==' to compare two objects*/
|
|
|
|
CCArray* allKeysForObject(CCObject* object);
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
CCObject* objectForKey(const CCString& key);
|
2012-03-19 14:53:44 +08:00
|
|
|
CCObject* objectForKey(int key);
|
2012-04-14 19:11:57 +08:00
|
|
|
const CCString* valueForKey(const CCString& key);
|
|
|
|
const CCString* valueForKey(int key);
|
2012-03-19 14:53:44 +08:00
|
|
|
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
void setObject(CCObject* pObject, const CCString& key);
|
|
|
|
void setObject(CCObject* pObject, int key);
|
2012-03-19 14:53:44 +08:00
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
void removeObjectForKey(const CCString& key);
|
|
|
|
void removeObjectForKey(int key);
|
|
|
|
void removeObjectsForKeys(CCArray* pKeyArray);
|
|
|
|
void removeObjectForElememt(CCDictElement* pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
void removeAllObjects();
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
virtual CCObject* copyWithZone(CCZone* pZone);
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
static CCDictionary* dictionaryWithDictionary(CCDictionary* srcDict);
|
2012-04-14 19:11:57 +08:00
|
|
|
/**
|
|
|
|
@brief Generate a CCDictionary pointer by file
|
|
|
|
@param pFileName The file name of *.plist file
|
|
|
|
@return The CCDictionary pointer generated from the file
|
|
|
|
*/
|
|
|
|
static CCDictionary* dictionaryWithContentsOfFile(const char *pFileName);
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
|
|
|
*/
|
|
|
|
static CCDictionary* dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
|
2012-03-19 14:53:44 +08:00
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
private:
|
|
|
|
void setObjectUnSafe(CCObject* pObject, const CCString& key);
|
|
|
|
void setObjectUnSafe(CCObject* pObject, const int key);
|
|
|
|
|
2012-03-19 14:53:44 +08:00
|
|
|
public:
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement* m_pElements;
|
2012-03-19 14:53:44 +08:00
|
|
|
private:
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2012-03-19 14:53:44 +08:00
|
|
|
enum CCDictType
|
|
|
|
{
|
|
|
|
kCCDictUnknown = 0,
|
|
|
|
kCCDictStr,
|
|
|
|
kCCDictInt
|
|
|
|
};
|
|
|
|
CCDictType m_eDictType;
|
|
|
|
CCDictType m_eOldDictType;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CCDICTIONARY_H__ */
|