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 "CCDictionary.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
2013-10-12 15:25:45 +08:00
#include "CCInteger.h"
#include "2d/platform/CCFileUtils.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
#include "CCBool.h"
#include "CCInteger.h"
#include "CCFloat.h"
#include "CCDouble.h"
#include "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 NULL;
__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 __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 __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 NULL;
__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 __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 __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 NULL directly.
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-10-12 15:25:45 +08:00
CCASSERT(_dictType == kDictStr, "this dictionary does not use string as key.");
Ref* pRetObject = NULL;
2013-10-12 15:25:45 +08:00
DictElement *pElement = NULL;
HASH_FIND_STR(_elements, key.c_str(), pElement);
if (pElement != NULL)
{
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 NULL directly.
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-10-12 15:25:45 +08:00
CCASSERT(_dictType == kDictInt, "this dictionary does not use integer as key.");
Ref* pRetObject = NULL;
2013-10-12 15:25:45 +08:00
DictElement *pElement = NULL;
HASH_FIND_PTR(_elements, &key, pElement);
if (pElement != NULL)
{
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));
2013-10-12 15:25:45 +08:00
if (pStr == NULL)
{
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));
2013-10-12 15:25:45 +08:00
if (pStr == NULL)
{
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.length() > 0 && pObject != NULL, "Invalid Argument!");
if (_dictType == kDictUnknown)
{
_dictType = kDictStr;
}
CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as key.");
DictElement *pElement = NULL;
HASH_FIND_STR(_elements, key.c_str(), pElement);
if (pElement == NULL)
{
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 != NULL, "Invalid Argument!");
if (_dictType == kDictUnknown)
{
_dictType = kDictInt;
}
CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as key.");
DictElement *pElement = NULL;
HASH_FIND_PTR(_elements, &key, pElement);
if (pElement == NULL)
{
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.length() > 0, "Invalid Argument!");
DictElement *pElement = NULL;
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 = NULL;
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 DictElement(key.c_str(), pObject);
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 DictElement(key, pObject);
HASH_ADD_PTR(_elements, _intKey, pElement);
}
void __Dictionary::removeObjectsForKeys(__Array* pKey__Array)
2013-10-12 15:25:45 +08:00
{
Ref* pObj = NULL;
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 != NULL)
{
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 NULL;
}
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 NULL;
}
}
__Dictionary* __Dictionary::create()
2013-10-12 15:25:45 +08:00
{
__Dictionary* ret = new __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 __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 __String(iter->second.asString());
ret->setObject(str, iter->first);
str->release();
}
}
return ret;
}
static __Array* visitArray(const ValueVector& array)
{
__Array* ret = new __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 __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 = NULL;
Ref* tmpObj = NULL;
2013-10-12 15:25:45 +08:00
Clonable* obj = NULL;
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