axmol/cocos/deprecated/CCDictionary.cpp

615 lines
17 KiB
C++
Raw Normal View History

2013-10-12 15:25:45 +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.
****************************************************************************/
#include "deprecated/CCDictionary.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
#include "deprecated/CCInteger.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
#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
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));
}
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);
}
// -----------------------------------------------------------------------
// __Dictionary
2013-10-12 15:25:45 +08:00
__Dictionary::__Dictionary()
: _elements(nullptr)
2013-10-12 15:25:45 +08:00
, _dictType(kDictUnknown)
{
}
__Dictionary::~__Dictionary()
2013-10-12 15:25:45 +08:00
{
CCLOGINFO("deallocing __Dictionary: %p", this);
2013-10-12 15:25:45 +08:00
removeAllObjects();
}
unsigned int __Dictionary::count()
2013-10-12 15:25:45 +08:00
{
return HASH_COUNT(_elements);
}
__Array* __Dictionary::allKeys()
2013-10-12 15:25:45 +08:00
{
int iKeyCount = this->count();
if (iKeyCount <= 0) return nullptr;
2013-10-12 15:25:45 +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)
{
__String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
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)
{
__Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
array->addObject(pOneKey);
2013-10-12 15:25:45 +08:00
CC_SAFE_RELEASE(pOneKey);
}
}
return array;
2013-10-12 15:25:45 +08:00
}
__Array* __Dictionary::allKeysForObject(Ref* object)
2013-10-12 15:25:45 +08:00
{
int iKeyCount = this->count();
if (iKeyCount <= 0) return nullptr;
__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)
{
__String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
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)
{
__Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
array->addObject(pOneKey);
2013-10-12 15:25:45 +08:00
CC_SAFE_RELEASE(pOneKey);
}
}
}
return array;
2013-10-12 15:25:45 +08:00
}
Ref* __Dictionary::objectForKey(const std::string& key)
2013-10-12 15:25:45 +08:00
{
// if dictionary wasn't initialized, return nullptr directly.
if (_dictType == kDictUnknown) return nullptr;
// __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.");
Ref* pRetObject = nullptr;
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_STR(_elements, key.c_str(), pElement);
if (pElement != nullptr)
2013-10-12 15:25:45 +08:00
{
pRetObject = pElement->_object;
}
return pRetObject;
}
Ref* __Dictionary::objectForKey(intptr_t key)
2013-10-12 15:25:45 +08:00
{
// if dictionary wasn't initialized, return nullptr directly.
if (_dictType == kDictUnknown) return nullptr;
// __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.");
Ref* pRetObject = nullptr;
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_PTR(_elements, &key, pElement);
if (pElement != nullptr)
2013-10-12 15:25:45 +08:00
{
pRetObject = pElement->_object;
}
return pRetObject;
}
const __String* __Dictionary::valueForKey(const std::string& key)
2013-10-12 15:25:45 +08:00
{
__String* pStr = dynamic_cast<__String*>(objectForKey(key));
if (pStr == nullptr)
2013-10-12 15:25:45 +08:00
{
pStr = __String::create("");
2013-10-12 15:25:45 +08:00
}
return pStr;
}
const __String* __Dictionary::valueForKey(intptr_t key)
2013-10-12 15:25:45 +08:00
{
__String* pStr = dynamic_cast<__String*>(objectForKey(key));
if (pStr == nullptr)
2013-10-12 15:25:45 +08:00
{
pStr = __String::create("");
2013-10-12 15:25:45 +08:00
}
return pStr;
}
void __Dictionary::setObject(Ref* pObject, const std::string& key)
2013-10-12 15:25:45 +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.");
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_STR(_elements, key.c_str(), pElement);
if (pElement == nullptr)
2013-10-12 15:25:45 +08:00
{
setObjectUnSafe(pObject, key);
}
else if (pElement->_object != pObject)
{
Ref* pTmpObj = pElement->_object;
2013-10-12 15:25:45 +08:00
pTmpObj->retain();
removeObjectForElememt(pElement);
setObjectUnSafe(pObject, key);
pTmpObj->release();
}
}
void __Dictionary::setObject(Ref* pObject, intptr_t key)
2013-10-12 15:25:45 +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.");
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_PTR(_elements, &key, pElement);
if (pElement == nullptr)
2013-10-12 15:25:45 +08:00
{
setObjectUnSafe(pObject, key);
}
else if (pElement->_object != pObject)
{
Ref* pTmpObj = pElement->_object;
2013-10-12 15:25:45 +08:00
pTmpObj->retain();
removeObjectForElememt(pElement);
setObjectUnSafe(pObject, key);
pTmpObj->release();
}
}
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");
CCASSERT(!key.empty(), "Invalid Argument!");
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_STR(_elements, key.c_str(), pElement);
removeObjectForElememt(pElement);
}
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");
DictElement *pElement = nullptr;
2013-10-12 15:25:45 +08:00
HASH_FIND_PTR(_elements, &key, pElement);
removeObjectForElememt(pElement);
}
void __Dictionary::setObjectUnSafe(Ref* pObject, const std::string& key)
2013-10-12 15:25:45 +08:00
{
pObject->retain();
DictElement* pElement = new (std::nothrow) DictElement(key.c_str(), pObject);
2013-10-12 15:25:45 +08:00
HASH_ADD_STR(_elements, _strKey, pElement);
}
void __Dictionary::setObjectUnSafe(Ref* pObject, const intptr_t key)
2013-10-12 15:25:45 +08:00
{
pObject->retain();
DictElement* pElement = new (std::nothrow) DictElement(key, pObject);
2013-10-12 15:25:45 +08:00
HASH_ADD_PTR(_elements, _intKey, pElement);
}
void __Dictionary::removeObjectsForKeys(__Array* pKey__Array)
2013-10-12 15:25:45 +08:00
{
Ref* pObj = nullptr;
CCARRAY_FOREACH(pKey__Array, pObj)
2013-10-12 15:25:45 +08:00
{
__String* pStr = static_cast<__String*>(pObj);
2013-10-12 15:25:45 +08:00
removeObjectForKey(pStr->getCString());
}
}
void __Dictionary::removeObjectForElememt(DictElement* pElement)
2013-10-12 15:25:45 +08:00
{
if (pElement != nullptr)
2013-10-12 15:25:45 +08:00
{
HASH_DEL(_elements, pElement);
pElement->_object->release();
CC_SAFE_DELETE(pElement);
}
}
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);
}
}
Ref* __Dictionary::randomObject()
2013-10-12 15:25:45 +08:00
{
if (_dictType == kDictUnknown)
{
return nullptr;
2013-10-12 15:25:45 +08:00
}
Ref* key = allKeys()->getRandomObject();
2013-10-12 15:25:45 +08:00
if (_dictType == kDictInt)
{
return objectForKey( static_cast<__Integer*>(key)->getValue());
2013-10-12 15:25:45 +08:00
}
else if (_dictType == kDictStr)
{
return objectForKey( static_cast<__String*>(key)->getCString());
2013-10-12 15:25:45 +08:00
}
else
{
return nullptr;
2013-10-12 15:25:45 +08:00
}
}
__Dictionary* __Dictionary::create()
2013-10-12 15:25:45 +08:00
{
__Dictionary* ret = new (std::nothrow) __Dictionary();
2013-10-12 15:25:45 +08:00
if (ret && ret->init() )
{
ret->autorelease();
}
return ret;
}
bool __Dictionary::init()
2013-10-12 15:25:45 +08:00
{
return true;
}
__Dictionary* __Dictionary::createWithDictionary(__Dictionary* srcDict)
2013-10-12 15:25:45 +08:00
{
return srcDict->clone();
}
static __Array* visitArray(const ValueVector& array);
static __Dictionary* visitDict(const ValueMap& dict)
{
__Dictionary* ret = new (std::nothrow) __Dictionary();
ret->init();
for (auto iter = dict.begin(); iter != dict.end(); ++iter)
{
if (iter->second.getType() == Value::Type::MAP)
{
const ValueMap& subDict = iter->second.asValueMap();
auto sub = visitDict(subDict);
ret->setObject(sub, iter->first);
sub->release();
}
else if (iter->second.getType() == Value::Type::VECTOR)
{
const ValueVector& arr = iter->second.asValueVector();
auto sub = visitArray(arr);
ret->setObject(sub, iter->first);
sub->release();
}
else
{
auto str = new (std::nothrow) __String(iter->second.asString());
ret->setObject(str, iter->first);
str->release();
}
}
return ret;
}
static __Array* visitArray(const ValueVector& array)
{
__Array* ret = new (std::nothrow) __Array();
ret->init();
for(const auto &value : array) {
if (value.getType() == Value::Type::MAP)
{
const ValueMap& subDict = value.asValueMap();
auto sub = visitDict(subDict);
ret->addObject(sub);
sub->release();
}
else if (value.getType() == Value::Type::VECTOR)
{
const ValueVector& arr = value.asValueVector();
auto sub = visitArray(arr);
ret->addObject(sub);
sub->release();
}
else
{
auto str = new (std::nothrow) __String(value.asString());
ret->addObject(str);
str->release();
}
}
return ret;
}
__Dictionary* __Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
2013-10-12 15:25:45 +08:00
{
return visitDict(FileUtils::getInstance()->getValueMapFromFile(pFileName));
2013-10-12 15:25:45 +08:00
}
void __Dictionary::acceptVisitor(DataVisitor &visitor)
2013-10-12 15:25:45 +08:00
{
return visitor.visit(this);
}
__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;
}
static ValueMap ccdictionary_to_valuemap(__Dictionary* dict);
static ValueVector ccarray_to_valuevector(__Array* arr)
2013-10-12 15:25:45 +08:00
{
ValueVector ret;
Ref* obj;
CCARRAY_FOREACH(arr, obj)
2013-12-04 17:28:14 +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
}
return ret;
}
static ValueMap ccdictionary_to_valuemap(__Dictionary* dict)
{
ValueMap ret;
DictElement* pElement = nullptr;
CCDICT_FOREACH(dict, pElement)
{
Ref* obj = pElement->getObject();
__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
}
__Dictionary* __Dictionary::clone() const
2013-10-12 15:25:45 +08:00
{
__Dictionary* newDict = __Dictionary::create();
2013-10-12 15:25:45 +08:00
DictElement* element = nullptr;
Ref* tmpObj = nullptr;
Clonable* obj = nullptr;
2013-10-12 15:25:45 +08:00
if (_dictType == kDictInt)
{
CCDICT_FOREACH(this, element)
{
obj = dynamic_cast<Clonable*>(element->getObject());
if (obj)
{
tmpObj = dynamic_cast<Ref*>(obj->clone());
2013-10-12 15:25:45 +08:00
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<Ref*>(obj->clone());
2013-10-12 15:25:45 +08:00
if (tmpObj)
{
newDict->setObject(tmpObj, element->getStrKey());
}
}
else
{
CCLOGWARN("%s isn't clonable.", typeid(*element->getObject()).name());
}
}
}
return newDict;
}
NS_CC_END