2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "deprecated/CCDictionary.h"
|
2016-04-18 16:07:48 +08:00
|
|
|
#include <type_traits>
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "deprecated/CCBool.h"
|
|
|
|
#include "deprecated/CCInteger.h"
|
|
|
|
#include "deprecated/CCFloat.h"
|
|
|
|
#include "deprecated/CCDouble.h"
|
|
|
|
#include "deprecated/CCArray.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// DictElement
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
DictElement::DictElement(const char* pszKey, Ref* pObject)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
CCASSERT(pszKey && strlen(pszKey) > 0, "Invalid key value.");
|
|
|
|
_intKey = 0;
|
|
|
|
const char* pStart = pszKey;
|
|
|
|
|
|
|
|
size_t len = strlen(pszKey);
|
|
|
|
if (len > MAX_KEY_LEN )
|
|
|
|
{
|
|
|
|
char* pEnd = (char*)&pszKey[len-1];
|
|
|
|
pStart = pEnd - (MAX_KEY_LEN-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(_strKey, pStart);
|
|
|
|
|
|
|
|
_object = pObject;
|
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
DictElement::DictElement(intptr_t iKey, Ref* pObject)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
_strKey[0] = '\0';
|
|
|
|
_intKey = iKey;
|
|
|
|
_object = pObject;
|
|
|
|
memset(&hh, 0, sizeof(hh));
|
|
|
|
}
|
|
|
|
|
|
|
|
DictElement::~DictElement()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing DictElement: %p", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
2013-12-11 14:39:21 +08:00
|
|
|
// __Dictionary
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary::__Dictionary()
|
2014-04-09 22:53:59 +08:00
|
|
|
: _elements(nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
, _dictType(kDictUnknown)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary::~__Dictionary()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 14:39:21 +08:00
|
|
|
CCLOGINFO("deallocing __Dictionary: %p", this);
|
2013-10-12 15:25:45 +08:00
|
|
|
removeAllObjects();
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
unsigned int __Dictionary::count()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return HASH_COUNT(_elements);
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Array* __Dictionary::allKeys()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
2014-07-10 00:45:27 +08:00
|
|
|
if (iKeyCount <= 0) return nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-07 14:26:52 +08:00
|
|
|
__Array* array = __Array::createWithCapacity(iKeyCount);
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
DictElement *pElement, *tmp;
|
|
|
|
if (_dictType == kDictStr)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
|
2013-12-07 14:26:52 +08:00
|
|
|
array->addObject(pOneKey);
|
2013-10-12 15:25:45 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_dictType == kDictInt)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
|
2013-12-07 14:26:52 +08:00
|
|
|
array->addObject(pOneKey);
|
2013-10-12 15:25:45 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:26:52 +08:00
|
|
|
return array;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
__Array* __Dictionary::allKeysForObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
2014-07-10 00:45:27 +08:00
|
|
|
if (iKeyCount <= 0) return nullptr;
|
2013-12-07 14:26:52 +08:00
|
|
|
__Array* array = __Array::create();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
DictElement *pElement, *tmp;
|
|
|
|
|
|
|
|
if (_dictType == kDictStr)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
|
|
|
{
|
|
|
|
if (object == pElement->_object)
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
|
2013-12-07 14:26:52 +08:00
|
|
|
array->addObject(pOneKey);
|
2013-10-12 15:25:45 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_dictType == kDictInt)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
|
|
|
{
|
|
|
|
if (object == pElement->_object)
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
|
2013-12-07 14:26:52 +08:00
|
|
|
array->addObject(pOneKey);
|
2013-10-12 15:25:45 +08:00
|
|
|
CC_SAFE_RELEASE(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 14:26:52 +08:00
|
|
|
return array;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* __Dictionary::objectForKey(const std::string& key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
// if dictionary wasn't initialized, return nullptr directly.
|
|
|
|
if (_dictType == kDictUnknown) return nullptr;
|
2013-12-11 14:39:21 +08:00
|
|
|
// __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-10-12 15:25:45 +08:00
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary does not use string as key.");
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
Ref* pRetObject = nullptr;
|
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pElement != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pRetObject = pElement->_object;
|
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* __Dictionary::objectForKey(intptr_t key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
// if dictionary wasn't initialized, return nullptr directly.
|
|
|
|
if (_dictType == kDictUnknown) return nullptr;
|
2013-12-11 14:39:21 +08:00
|
|
|
// __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-10-12 15:25:45 +08:00
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary does not use integer as key.");
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
Ref* pRetObject = nullptr;
|
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pElement != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pRetObject = pElement->_object;
|
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
const __String* __Dictionary::valueForKey(const std::string& key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* pStr = dynamic_cast<__String*>(objectForKey(key));
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pStr == nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
pStr = __String::create("");
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
return pStr;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
const __String* __Dictionary::valueForKey(intptr_t key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* pStr = dynamic_cast<__String*>(objectForKey(key));
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pStr == nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
pStr = __String::create("");
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
return pStr;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Dictionary::setObject(Ref* pObject, const std::string& key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-09-01 11:26:09 +08:00
|
|
|
CCASSERT(!key.empty() && pObject != nullptr, "Invalid Argument!");
|
2013-10-12 15:25:45 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
|
|
|
{
|
|
|
|
_dictType = kDictStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as key.");
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pElement == nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
}
|
|
|
|
else if (pElement->_object != pObject)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* pTmpObj = pElement->_object;
|
2013-10-12 15:25:45 +08:00
|
|
|
pTmpObj->retain();
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
pTmpObj->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Dictionary::setObject(Ref* pObject, intptr_t key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
CCASSERT(pObject != nullptr, "Invalid Argument!");
|
2013-10-12 15:25:45 +08:00
|
|
|
if (_dictType == kDictUnknown)
|
|
|
|
{
|
|
|
|
_dictType = kDictInt;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as key.");
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pElement == nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
}
|
|
|
|
else if (pElement->_object != pObject)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* pTmpObj = pElement->_object;
|
2013-10-12 15:25:45 +08:00
|
|
|
pTmpObj->retain();
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
setObjectUnSafe(pObject, key);
|
|
|
|
pTmpObj->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::removeObjectForKey(const std::string& key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (_dictType == kDictUnknown)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as its key");
|
2015-09-01 11:26:09 +08:00
|
|
|
CCASSERT(!key.empty(), "Invalid Argument!");
|
2014-07-10 00:45:27 +08:00
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_STR(_elements, key.c_str(), pElement);
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::removeObjectForKey(intptr_t key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (_dictType == kDictUnknown)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as its key");
|
2014-07-10 00:45:27 +08:00
|
|
|
DictElement *pElement = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_FIND_PTR(_elements, &key, pElement);
|
|
|
|
removeObjectForElememt(pElement);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Dictionary::setObjectUnSafe(Ref* pObject, const std::string& key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pObject->retain();
|
2014-08-28 07:31:57 +08:00
|
|
|
DictElement* pElement = new (std::nothrow) DictElement(key.c_str(), pObject);
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_ADD_STR(_elements, _strKey, pElement);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __Dictionary::setObjectUnSafe(Ref* pObject, const intptr_t key)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pObject->retain();
|
2014-08-28 07:31:57 +08:00
|
|
|
DictElement* pElement = new (std::nothrow) DictElement(key, pObject);
|
2013-10-12 15:25:45 +08:00
|
|
|
HASH_ADD_PTR(_elements, _intKey, pElement);
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::removeObjectsForKeys(__Array* pKey__Array)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
Ref* pObj = nullptr;
|
2013-12-07 14:26:52 +08:00
|
|
|
CCARRAY_FOREACH(pKey__Array, pObj)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* pStr = static_cast<__String*>(pObj);
|
2013-10-12 15:25:45 +08:00
|
|
|
removeObjectForKey(pStr->getCString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::removeObjectForElememt(DictElement* pElement)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pElement != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
HASH_DEL(_elements, pElement);
|
|
|
|
pElement->_object->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::removeAllObjects()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
DictElement *pElement, *tmp;
|
|
|
|
HASH_ITER(hh, _elements, pElement, tmp)
|
|
|
|
{
|
|
|
|
HASH_DEL(_elements, pElement);
|
|
|
|
pElement->_object->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* __Dictionary::randomObject()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (_dictType == kDictUnknown)
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* key = allKeys()->getRandomObject();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
if (_dictType == kDictInt)
|
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
return objectForKey( static_cast<__Integer*>(key)->getValue());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
else if (_dictType == kDictStr)
|
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
return objectForKey( static_cast<__String*>(key)->getCString());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* __Dictionary::create()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Dictionary* ret = new (std::nothrow) __Dictionary();
|
2013-10-12 15:25:45 +08:00
|
|
|
if (ret && ret->init() )
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
bool __Dictionary::init()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* __Dictionary::createWithDictionary(__Dictionary* srcDict)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return srcDict->clone();
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:26:52 +08:00
|
|
|
static __Array* visitArray(const ValueVector& array);
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
static __Dictionary* visitDict(const ValueMap& dict)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Dictionary* ret = new (std::nothrow) __Dictionary();
|
2013-12-03 14:47:35 +08:00
|
|
|
ret->init();
|
|
|
|
|
|
|
|
for (auto iter = dict.begin(); iter != dict.end(); ++iter)
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
if (iter->second.getType() == Value::Type::MAP)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
const ValueMap& subDict = iter->second.asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
auto sub = visitDict(subDict);
|
|
|
|
ret->setObject(sub, iter->first);
|
|
|
|
sub->release();
|
|
|
|
}
|
2013-12-04 17:46:57 +08:00
|
|
|
else if (iter->second.getType() == Value::Type::VECTOR)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
const ValueVector& arr = iter->second.asValueVector();
|
2013-12-03 14:47:35 +08:00
|
|
|
auto sub = visitArray(arr);
|
|
|
|
ret->setObject(sub, iter->first);
|
|
|
|
sub->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
auto str = new (std::nothrow) __String(iter->second.asString());
|
2013-12-03 14:47:35 +08:00
|
|
|
ret->setObject(str, iter->first);
|
|
|
|
str->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:26:52 +08:00
|
|
|
static __Array* visitArray(const ValueVector& array)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
__Array* ret = new (std::nothrow) __Array();
|
2013-12-03 14:47:35 +08:00
|
|
|
ret->init();
|
2013-12-20 05:34:41 +08:00
|
|
|
|
|
|
|
for(const auto &value : array) {
|
2013-12-04 17:46:57 +08:00
|
|
|
if (value.getType() == Value::Type::MAP)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
const ValueMap& subDict = value.asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
auto sub = visitDict(subDict);
|
|
|
|
ret->addObject(sub);
|
|
|
|
sub->release();
|
|
|
|
}
|
2013-12-04 17:46:57 +08:00
|
|
|
else if (value.getType() == Value::Type::VECTOR)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
const ValueVector& arr = value.asValueVector();
|
2013-12-03 14:47:35 +08:00
|
|
|
auto sub = visitArray(arr);
|
|
|
|
ret->addObject(sub);
|
|
|
|
sub->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
auto str = new (std::nothrow) __String(value.asString());
|
2013-12-03 14:47:35 +08:00
|
|
|
ret->addObject(str);
|
|
|
|
str->release();
|
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* __Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-04 17:50:57 +08:00
|
|
|
return visitDict(FileUtils::getInstance()->getValueMapFromFile(pFileName));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
void __Dictionary::acceptVisitor(DataVisitor &visitor)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* __Dictionary::createWithContentsOfFile(const char *pFileName)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
auto ret = createWithContentsOfFileThreadSafe(pFileName);
|
|
|
|
if (ret != nullptr)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-04 14:59:24 +08:00
|
|
|
static ValueMap ccdictionary_to_valuemap(__Dictionary* dict);
|
|
|
|
|
|
|
|
static ValueVector ccarray_to_valuevector(__Array* arr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-04 14:59:24 +08:00
|
|
|
ValueVector ret;
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-04 14:59:24 +08:00
|
|
|
CCARRAY_FOREACH(arr, obj)
|
2013-12-04 17:28:14 +08:00
|
|
|
{
|
2014-01-04 14:59:24 +08:00
|
|
|
Value arrElement;
|
|
|
|
|
|
|
|
__String* strVal = nullptr;
|
|
|
|
__Dictionary* dictVal = nullptr;
|
|
|
|
__Array* arrVal = nullptr;
|
|
|
|
__Double* doubleVal = nullptr;
|
|
|
|
__Bool* boolVal = nullptr;
|
|
|
|
__Float* floatVal = nullptr;
|
|
|
|
__Integer* intVal = nullptr;
|
|
|
|
|
|
|
|
if ((strVal = dynamic_cast<__String *>(obj))) {
|
|
|
|
arrElement = Value(strVal->getCString());
|
|
|
|
} else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) {
|
|
|
|
arrElement = ccdictionary_to_valuemap(dictVal);
|
|
|
|
} else if ((arrVal = dynamic_cast<__Array*>(obj))) {
|
|
|
|
arrElement = ccarray_to_valuevector(arrVal);
|
|
|
|
} else if ((doubleVal = dynamic_cast<__Double*>(obj))) {
|
|
|
|
arrElement = Value(doubleVal->getValue());
|
|
|
|
} else if ((floatVal = dynamic_cast<__Float*>(obj))) {
|
|
|
|
arrElement = Value(floatVal->getValue());
|
|
|
|
} else if ((intVal = dynamic_cast<__Integer*>(obj))) {
|
|
|
|
arrElement = Value(intVal->getValue());
|
|
|
|
} else if ((boolVal = dynamic_cast<__Bool*>(obj))) {
|
|
|
|
arrElement = boolVal->getValue() ? Value(true) : Value(false);
|
|
|
|
} else {
|
|
|
|
CCASSERT(false, "the type isn't suppored.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.push_back(arrElement);
|
2013-12-04 17:28:14 +08:00
|
|
|
}
|
2014-01-04 14:59:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ValueMap ccdictionary_to_valuemap(__Dictionary* dict)
|
|
|
|
{
|
|
|
|
ValueMap ret;
|
|
|
|
DictElement* pElement = nullptr;
|
|
|
|
CCDICT_FOREACH(dict, pElement)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = pElement->getObject();
|
2014-01-04 14:59:24 +08:00
|
|
|
|
|
|
|
__String* strVal = nullptr;
|
|
|
|
__Dictionary* dictVal = nullptr;
|
|
|
|
__Array* arrVal = nullptr;
|
|
|
|
__Double* doubleVal = nullptr;
|
|
|
|
__Bool* boolVal = nullptr;
|
|
|
|
__Float* floatVal = nullptr;
|
|
|
|
__Integer* intVal = nullptr;
|
|
|
|
|
|
|
|
Value dictElement;
|
|
|
|
|
|
|
|
if ((strVal = dynamic_cast<__String *>(obj))) {
|
|
|
|
dictElement = Value(strVal->getCString());
|
|
|
|
} else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) {
|
|
|
|
dictElement = ccdictionary_to_valuemap(dictVal);
|
|
|
|
} else if ((arrVal = dynamic_cast<__Array*>(obj))) {
|
|
|
|
dictElement = ccarray_to_valuevector(arrVal);
|
|
|
|
} else if ((doubleVal = dynamic_cast<__Double*>(obj))) {
|
|
|
|
dictElement = Value(doubleVal->getValue());
|
|
|
|
} else if ((floatVal = dynamic_cast<__Float*>(obj))) {
|
|
|
|
dictElement = Value(floatVal->getValue());
|
|
|
|
} else if ((intVal = dynamic_cast<__Integer*>(obj))) {
|
|
|
|
dictElement = Value(intVal->getValue());
|
|
|
|
} else if ((boolVal = dynamic_cast<__Bool*>(obj))) {
|
|
|
|
dictElement = boolVal->getValue() ? Value(true) : Value(false);
|
|
|
|
} else {
|
|
|
|
CCASSERT(false, "the type isn't suppored.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* key = pElement->getStrKey();
|
|
|
|
if (key && strlen(key) > 0)
|
|
|
|
{
|
|
|
|
ret[key] = dictElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool __Dictionary::writeToFile(const char *fullPath)
|
|
|
|
{
|
|
|
|
ValueMap dict = ccdictionary_to_valuemap(this);
|
2013-12-04 17:28:14 +08:00
|
|
|
return FileUtils::getInstance()->writeToFile(dict, fullPath);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* __Dictionary::clone() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 14:39:21 +08:00
|
|
|
__Dictionary* newDict = __Dictionary::create();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
DictElement* element = nullptr;
|
|
|
|
Ref* tmpObj = nullptr;
|
|
|
|
Clonable* obj = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
if (_dictType == kDictInt)
|
|
|
|
{
|
2016-06-03 13:42:44 +08:00
|
|
|
DictElement* tmp = nullptr;
|
|
|
|
HASH_ITER(hh, _elements, element, tmp)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
obj = dynamic_cast<Clonable*>(element->getObject());
|
|
|
|
if (obj)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
tmpObj = dynamic_cast<Ref*>(obj->clone());
|
2013-10-12 15:25:45 +08:00
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
newDict->setObject(tmpObj, element->getIntKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 16:07:48 +08:00
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(std::remove_pointer<decltype(element->getObject())>::type).name());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_dictType == kDictStr)
|
|
|
|
{
|
2016-06-03 13:42:44 +08:00
|
|
|
DictElement* tmp = nullptr;
|
|
|
|
HASH_ITER(hh, _elements, element, tmp)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
obj = dynamic_cast<Clonable*>(element->getObject());
|
|
|
|
if (obj)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
tmpObj = dynamic_cast<Ref*>(obj->clone());
|
2013-10-12 15:25:45 +08:00
|
|
|
if (tmpObj)
|
|
|
|
{
|
|
|
|
newDict->setObject(tmpObj, element->getStrKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 16:07:48 +08:00
|
|
|
CCLOGWARN("%s isn't clonable.", typeid(std::remove_pointer<decltype(element->getObject())>::type).name());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|