axmol/extensions/CocoStudio/Components/CCComAttribute.cpp

193 lines
4.8 KiB
C++
Raw Normal View History

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
ComAttribute::ComAttribute(void)
: _attributes(NULL)
, _jsonDict(NULL)
2013-06-04 17:38:43 +08:00
{
_name = "ComAttribute";
2013-06-04 17:38:43 +08:00
}
ComAttribute::~ComAttribute(void)
2013-06-04 17:38:43 +08:00
{
CC_SAFE_DELETE(_jsonDict);
CC_SAFE_RELEASE(_attributes);
2013-06-04 17:38:43 +08:00
}
bool ComAttribute::init()
2013-06-04 17:38:43 +08:00
{
_attributes = Dictionary::create();
_attributes->retain();
_jsonDict = new cs::JsonDictionary();
2013-06-04 17:38:43 +08:00
return true;
}
ComAttribute* ComAttribute::create(void)
2013-06-04 17:38:43 +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;
}
void ComAttribute::setInt(const char *key, int value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Integer::create(value), key);
2013-06-04 17:38:43 +08:00
}
void ComAttribute::setDouble(const char *key, double value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Double::create(value), key);
2013-06-04 17:38:43 +08:00
}
void ComAttribute::setFloat(const char *key, float value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Float::create(value), key);
2013-06-04 17:38:43 +08:00
}
void ComAttribute::setBool(const char *key, bool value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(Bool::create(value), key);
2013-06-04 17:38:43 +08:00
}
void ComAttribute::setCString(const char *key, const char *value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(String::create(value), key);
2013-06-04 17:38:43 +08:00
}
void ComAttribute::setObject(const char *key, Object *value)
2013-06-04 17:38:43 +08:00
{
CCASSERT(key != NULL, "Argument must be non-nil");
_attributes->setObject(value, key);
2013-06-04 17:38:43 +08:00
}
int ComAttribute::getInt(const char *key) const
2013-06-04 17:38:43 +08:00
{
Object *ret = _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
if( ret )
{
if( Integer *obj=dynamic_cast<Integer*>(ret) )
2013-06-04 17:38:43 +08:00
return obj->getValue();
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;
}
double ComAttribute::getDouble(const char *key) const
2013-06-04 17:38:43 +08:00
{
Object *ret = _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
if( ret )
{
if( Double *obj=dynamic_cast<Double*>(ret) )
2013-06-04 17:38:43 +08:00
return obj->getValue();
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;
}
float ComAttribute::getFloat(const char *key) const
2013-06-04 17:38:43 +08:00
{
Object *ret = _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
if( ret )
{
if( Float *obj=dynamic_cast<Float*>(ret) )
2013-06-04 17:38:43 +08:00
return obj->getValue();
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;
}
bool ComAttribute::getBool(const char *key) const
2013-06-04 17:38:43 +08:00
{
Object *ret = _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
if( ret )
{
if( Bool *boolobj=dynamic_cast<Bool*>(ret) )
2013-06-04 17:38:43 +08:00
return boolobj->getValue();
if( String *strobj=dynamic_cast<String*>(ret) )
2013-06-04 17:38:43 +08:00
return strobj->boolValue();
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;
}
const char* ComAttribute::getCString(const char *key) const
2013-06-04 17:38:43 +08:00
{
Object *ret = _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
if( ret )
{
if( String *str=dynamic_cast<String*>(ret) )
2013-06-04 17:38:43 +08:00
return str->getCString();
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;
}
Object* ComAttribute::getObject(const char *key) const
2013-06-04 17:38:43 +08:00
{
return _attributes->objectForKey(key);
2013-06-04 17:38:43 +08:00
}
cs::JsonDictionary* ComAttribute::getDict() const
{
return _jsonDict;
}
2013-06-04 17:38:43 +08:00
NS_CC_EXT_END