2012-04-19 14:35:52 +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"
|
|
|
|
#include "CCString.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class CCDictionary;
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
class CC_DLL CCDictElement
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
#define MAX_KEY_LEN 256
|
2012-03-20 15:04:53 +08:00
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDictElement(const char* pszKey, CCObject* pObject)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
m_pObject = pObject;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-19 14:35:52 +08:00
|
|
|
return m_szKey;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
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-04-19 14:35:52 +08:00
|
|
|
return m_iKey;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
inline CCObject* getObject() const
|
|
|
|
{
|
|
|
|
return m_pObject;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
private:
|
2012-04-19 14:35:52 +08:00
|
|
|
inline void init()
|
|
|
|
{
|
|
|
|
m_iKey = 0;
|
|
|
|
m_pObject = NULL;
|
|
|
|
memset(m_szKey, 0, sizeof(m_szKey));
|
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char m_szKey[MAX_KEY_LEN+1]; /** hash key of string type*/
|
|
|
|
int m_iKey; /** hash key of integer type */
|
|
|
|
CCObject* m_pObject;/** hash value */
|
|
|
|
public:
|
|
|
|
UT_hash_handle hh; /* makes this class hashable */
|
|
|
|
friend class CCDictionary;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define CCDICT_FOREACH(__dict__, __el__) \
|
|
|
|
CCDictElement* pTmp##__dict__##__el__ = NULL; \
|
|
|
|
HASH_ITER(hh, (__dict__)->m_pElements, __el__, pTmp##__dict__##__el__)
|
|
|
|
|
|
|
|
|
|
|
|
class CC_DLL CCDictionary : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCDictionary();
|
|
|
|
~CCDictionary();
|
|
|
|
|
|
|
|
/// 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-26 11:39:49 +08:00
|
|
|
CCObject* objectForKey(const std::string& key);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* objectForKey(int key);
|
2012-04-26 11:39:49 +08:00
|
|
|
const CCString* valueForKey(const std::string& key);
|
2012-04-19 14:35:52 +08:00
|
|
|
const CCString* valueForKey(int key);
|
|
|
|
|
|
|
|
|
2012-04-26 11:39:49 +08:00
|
|
|
void setObject(CCObject* pObject, const std::string& key);
|
2012-04-19 14:35:52 +08:00
|
|
|
void setObject(CCObject* pObject, int key);
|
|
|
|
|
2012-04-26 11:39:49 +08:00
|
|
|
void removeObjectForKey(const std::string& key);
|
2012-04-19 14:35:52 +08:00
|
|
|
void removeObjectForKey(int key);
|
|
|
|
void removeObjectsForKeys(CCArray* pKeyArray);
|
|
|
|
void removeObjectForElememt(CCDictElement* pElement);
|
|
|
|
void removeAllObjects();
|
|
|
|
|
|
|
|
virtual CCObject* copyWithZone(CCZone* pZone);
|
|
|
|
|
2012-04-26 11:39:49 +08:00
|
|
|
/* static functions */
|
2012-06-14 16:05:58 +08:00
|
|
|
//@warning: This interface will be deprecated in future.
|
2012-06-14 17:18:05 +08:00
|
|
|
static CCDictionary* dictionary();
|
2012-04-26 11:39:49 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
static CCDictionary* dictionaryWithDictionary(CCDictionary* srcDict);
|
2012-04-19 14:35:52 +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
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-14 17:18:05 +08:00
|
|
|
static CCDictionary* dictionaryWithContentsOfFile(const char *pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-14 17:18:05 +08:00
|
|
|
static CCDictionary* dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
|
2012-06-14 16:05:58 +08:00
|
|
|
|
|
|
|
static CCDictionary* create();
|
|
|
|
|
|
|
|
static CCDictionary* createWithDictionary(CCDictionary* srcDict);
|
|
|
|
/**
|
|
|
|
@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* createWithContentsOfFile(const char *pFileName);
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
|
|
|
|
invoker should call release().
|
|
|
|
*/
|
|
|
|
static CCDictionary* createWithContentsOfFileThreadSafe(const char *pFileName);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
private:
|
2012-04-26 11:39:49 +08:00
|
|
|
void setObjectUnSafe(CCObject* pObject, const std::string& key);
|
2012-04-19 14:35:52 +08:00
|
|
|
void setObjectUnSafe(CCObject* pObject, const int key);
|
|
|
|
|
|
|
|
public:
|
|
|
|
CCDictElement* m_pElements;
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum CCDictType
|
|
|
|
{
|
|
|
|
kCCDictUnknown = 0,
|
|
|
|
kCCDictStr,
|
|
|
|
kCCDictInt
|
|
|
|
};
|
|
|
|
CCDictType m_eDictType;
|
|
|
|
CCDictType m_eOldDictType;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CCDICTIONARY_H__ */
|