2013-01-15 17:09:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 - 2013 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "CCString.h"
|
|
|
|
#include "CCInteger.h"
|
2013-02-01 18:48:44 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-01-15 17:09:25 +08:00
|
|
|
// -----------------------------------------------------------------------
|
2013-06-20 14:13:12 +08:00
|
|
|
// DictElement
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement::DictElement(const char* pszKey, Object* pObject)
|
2013-01-15 17:09:25 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pszKey && strlen(pszKey) > 0, "Invalid key value.");
|
2013-06-14 20:46:21 +08:00
|
|
|
_intKey = 0;
|
2013-01-15 17:09:25 +08:00
|
|
|
const char* pStart = pszKey;
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
size_t len = strlen(pszKey);
|
2013-01-15 17:09:25 +08:00
|
|
|
if (len > MAX_KEY_LEN )
|
|
|
|
{
|
|
|
|
char* pEnd = (char*)&pszKey[len-1];
|
|
|
|
pStart = pEnd - (MAX_KEY_LEN-1);
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:46:21 +08:00
|
|
|
strcpy(_strKey, pStart);
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-06-14 20:46:21 +08:00
|
|
|
_object = pObject;
|
2013-01-15 17:09:25 +08:00
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement::DictElement(intptr_t iKey, Object* pObject)
|
2013-01-15 17:09:25 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
_strKey[0] = '\0';
|
|
|
|
_intKey = iKey;
|
|
|
|
_object = pObject;
|
2013-01-15 17:09:25 +08:00
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement::~DictElement()
|
2013-01-15 17:09:25 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
2013-06-20 14:13:12 +08:00
|
|
|
// Dictionary
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary::Dictionary()
|
2013-06-14 20:46:21 +08:00
|
|
|
: _elements(NULL)
|
2013-06-20 14:13:12 +08:00
|
|
|
, _dictType(kDictUnknown)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary::~Dictionary()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
removeAllObjects();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int Dictionary::count()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
return HASH_COUNT(_elements);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* Dictionary::allKeys()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
|
|
|
if (iKeyCount <= 0) return NULL;
|
2012-06-14 16:05:58 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pArray = Array::createWithCapacity(iKeyCount);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement, *tmp;
|
|
|
|
if (_dictType == kDictStr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pOneKey = new String(pElement->_strKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
pArray->addObject(pOneKey);
|
2013-01-15 17:09:25 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (_dictType == kDictInt)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Integer* pOneKey = new Integer(pElement->_intKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
pArray->addObject(pOneKey);
|
2013-01-15 17:09:25 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* Dictionary::allKeysForObject(Object* object)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
|
|
|
if (iKeyCount <= 0) return NULL;
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pArray = Array::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement, *tmp;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictStr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
if (object == pElement->_object)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pOneKey = new String(pElement->_strKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
pArray->addObject(pOneKey);
|
2013-01-15 17:09:25 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (_dictType == kDictInt)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
if (object == pElement->_object)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Integer* pOneKey = new Integer(pElement->_intKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
pArray->addObject(pOneKey);
|
2013-01-15 17:09:25 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Dictionary::objectForKey(const std::string& key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-10-08 14:55:46 +08:00
|
|
|
// if dictionary wasn't initialized, return NULL directly.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown) return NULL;
|
|
|
|
// Dictionary only supports one kind of key, string or integer.
|
|
|
|
// This method uses string as key, therefore we should make sure that the key type of this Dictionary is string.
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary does not use string as key.");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pRetObject = NULL;
|
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
pRetObject = pElement->_object;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Dictionary::objectForKey(intptr_t key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-10-08 14:55:46 +08:00
|
|
|
// if dictionary wasn't initialized, return NULL directly.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown) return NULL;
|
|
|
|
// Dictionary only supports one kind of key, string or integer.
|
|
|
|
// This method uses integer as key, therefore we should make sure that the key type of this Dictionary is integer.
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary does not use integer as key.");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pRetObject = NULL;
|
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
pRetObject = pElement->_object;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const String* Dictionary::valueForKey(const std::string& key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pStr = dynamic_cast<String*>(objectForKey(key));
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pStr == NULL)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pStr = String::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return pStr;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const String* Dictionary::valueForKey(intptr_t key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pStr = dynamic_cast<String*>(objectForKey(key));
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pStr == NULL)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pStr = String::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return pStr;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::setObject(Object* pObject, const std::string& key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key.length() > 0 && pObject != NULL, "Invalid Argument!");
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_dictType = kDictStr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as key.");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement == NULL)
|
|
|
|
{
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
}
|
2013-06-14 20:46:21 +08:00
|
|
|
else if (pElement->_object != pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pTmpObj = pElement->_object;
|
2012-04-19 14:35:52 +08:00
|
|
|
pTmpObj->retain();
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
pTmpObj->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::setObject(Object* pObject, intptr_t key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pObject != NULL, "Invalid Argument!");
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_dictType = kDictInt;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as key.");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement == NULL)
|
|
|
|
{
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
}
|
2013-06-14 20:46:21 +08:00
|
|
|
else if (pElement->_object != pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pTmpObj = pElement->_object;
|
2012-04-19 14:35:52 +08:00
|
|
|
pTmpObj->retain();
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
pTmpObj->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::removeObjectForKey(const std::string& key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as its key");
|
|
|
|
CCASSERT(key.length() > 0, "Invalid Argument!");
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::removeObjectForKey(intptr_t key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-15 17:09:25 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as its key");
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement = NULL;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::setObjectUnSafe(Object* pObject, const std::string& key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pObject->retain();
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement* pElement = new DictElement(key.c_str(), pObject);
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ADD_STR(_elements, _strKey, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::setObjectUnSafe(Object* pObject, const intptr_t key)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pObject->retain();
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement* pElement = new DictElement(key, pObject);
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ADD_PTR(_elements, _intKey, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::removeObjectsForKeys(Array* pKeyArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pKeyArray, pObj)
|
|
|
|
{
|
2013-07-09 05:38:14 +08:00
|
|
|
String* pStr = static_cast<String*>(pObj);
|
2012-04-26 11:39:49 +08:00
|
|
|
removeObjectForKey(pStr->getCString());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::removeObjectForElememt(DictElement* pElement)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_DEL(_elements, pElement);
|
|
|
|
pElement->_object->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::removeAllObjects()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement, *tmp;
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-14 20:46:21 +08:00
|
|
|
HASH_DEL(_elements, pElement);
|
|
|
|
pElement->_object->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE(pElement);
|
2013-03-08 04:13:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Dictionary::randomObject()
|
2012-12-06 22:56:49 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
2012-12-06 22:56:49 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* key = allKeys()->randomObject();
|
2012-12-06 22:56:49 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (_dictType == kDictInt)
|
2012-12-06 22:56:49 +08:00
|
|
|
{
|
2013-07-09 05:38:14 +08:00
|
|
|
return objectForKey( static_cast<Integer*>(key)->getValue());
|
2012-12-06 22:56:49 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (_dictType == kDictStr)
|
2012-12-06 22:56:49 +08:00
|
|
|
{
|
2013-07-09 05:38:14 +08:00
|
|
|
return objectForKey( static_cast<String*>(key)->getCString());
|
2012-12-06 22:56:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* Dictionary::create()
|
2012-04-26 11:39:49 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pRet = new Dictionary();
|
2012-04-26 11:39:49 +08:00
|
|
|
if (pRet != NULL)
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* Dictionary::createWithDictionary(Dictionary* srcDict)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 09:48:41 +08:00
|
|
|
return srcDict->clone();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
return FileUtils::getInstance()->createDictionaryWithContentsOfFile(pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Dictionary::acceptVisitor(DataVisitor &visitor)
|
2013-05-10 15:07:05 +08:00
|
|
|
{
|
|
|
|
return visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* Dictionary::createWithContentsOfFile(const char *pFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pRet = createWithContentsOfFileThreadSafe(pFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Dictionary::writeToFile(const char *fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
return FileUtils::getInstance()->writeToFile(this, fullPath);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Dictionary* Dictionary::clone() const
|
|
|
|
{
|
|
|
|
Dictionary* newDict = new Dictionary();
|
|
|
|
newDict->autorelease();
|
|
|
|
|
|
|
|
DictElement* element = NULL;
|
|
|
|
Object* tmpObj = NULL;
|
|
|
|
Clonable* obj = NULL;
|
|
|
|
if (_dictType == kDictInt)
|
|
|
|
{
|
|
|
|
CCDICT_FOREACH(this, element)
|
|
|
|
{
|
|
|
|
obj = dynamic_cast<Clonable*>(element->getObject());
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
tmpObj = dynamic_cast<Object*>(obj->clone());
|
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
newDict->setObject(tmpObj, element->getIntKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(*element->getObject()).name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_dictType == kDictStr)
|
|
|
|
{
|
|
|
|
CCDICT_FOREACH(this, element)
|
|
|
|
{
|
|
|
|
obj = dynamic_cast<Clonable*>(element->getObject());
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
tmpObj = dynamic_cast<Object*>(obj->clone());
|
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
newDict->setObject(tmpObj, element->getStrKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(*element->getObject()).name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDict;
|
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|