2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCComAttribute.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAttribute::ComAttribute(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _attributes(NULL)
|
2013-09-09 19:51:26 +08:00
|
|
|
, _jsonDict(NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = "ComAttribute";
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAttribute::~ComAttribute(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-09 19:51:26 +08:00
|
|
|
CC_SAFE_DELETE(_jsonDict);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_attributes);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAttribute::init()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes = Dictionary::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_attributes->retain();
|
2013-09-09 19:51:26 +08:00
|
|
|
|
|
|
|
_jsonDict = new cs::JsonDictionary();
|
2013-06-04 17:38:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAttribute* ComAttribute::create(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAttribute * pRet = new ComAttribute();
|
2013-06-04 17:38:43 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setInt(const char *key, int value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes->setObject(Integer::create(value), key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setDouble(const char *key, double value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes->setObject(Double::create(value), key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setFloat(const char *key, float value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes->setObject(Float::create(value), key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setBool(const char *key, bool value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes->setObject(Bool::create(value), key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setCString(const char *key, const char *value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-20 14:15:53 +08:00
|
|
|
_attributes->setObject(String::create(value), key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAttribute::setObject(const char *key, Object *value)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(key != NULL, "Argument must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
_attributes->setObject(value, key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int ComAttribute::getInt(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *ret = _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
if( ret )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if( Integer *obj=dynamic_cast<Integer*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return obj->getValue();
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Key found, type is not integer");
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
|
|
|
CCLOG("Key not found: '%s'", key );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
double ComAttribute::getDouble(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *ret = _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
if( ret )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if( Double *obj=dynamic_cast<Double*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return obj->getValue();
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Key found, type is not double");
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
|
|
|
CCLOG("Key not found: '%s'", key );
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ComAttribute::getFloat(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *ret = _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
if( ret )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if( Float *obj=dynamic_cast<Float*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return obj->getValue();
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Key found, type is not float");
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
|
|
|
CCLOG("Key not found: '%s'", key );
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAttribute::getBool(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *ret = _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
if( ret )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if( Bool *boolobj=dynamic_cast<Bool*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return boolobj->getValue();
|
2013-06-20 14:15:53 +08:00
|
|
|
if( String *strobj=dynamic_cast<String*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return strobj->boolValue();
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Key found, type is not Bool");
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
|
|
|
CCLOG("Key not found: '%s'", key );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const char* ComAttribute::getCString(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *ret = _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
if( ret )
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if( String *str=dynamic_cast<String*>(ret) )
|
2013-06-04 17:38:43 +08:00
|
|
|
return str->getCString();
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Key found, type is not CString");
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
|
|
|
CCLOG("Key not found: '%s'", key );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* ComAttribute::getObject(const char *key) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _attributes->objectForKey(key);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 19:51:26 +08:00
|
|
|
cs::JsonDictionary* ComAttribute::getDict() const
|
|
|
|
{
|
|
|
|
return _jsonDict;
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:38:43 +08:00
|
|
|
NS_CC_EXT_END
|