CCConfiguration supports default values in the query

Adds tests for the new API as well
This commit is contained in:
Ricardo Quesada 2013-05-30 16:10:31 -07:00
parent 809b020de5
commit f6665d492b
4 changed files with 60 additions and 16 deletions

View File

@ -220,7 +220,7 @@ bool CCConfiguration::supportsShareableVAO(void) const
// //
// generic getters for properties // generic getters for properties
// //
const char *CCConfiguration::getCString( const char *key ) const const char *CCConfiguration::getCString( const char *key, const char *default_value ) const
{ {
CCObject *ret = m_pDefaults->objectForKey(key); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -231,12 +231,11 @@ const char *CCConfiguration::getCString( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return NULL;
} }
/** returns the value of a given key as a boolean */ /** returns the value of a given key as a boolean */
bool CCConfiguration::getBool( const char *key ) const bool CCConfiguration::getBool( const char *key, bool default_value ) const
{ {
CCObject *ret = m_pDefaults->objectForKey(key); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -248,12 +247,11 @@ bool CCConfiguration::getBool( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return false;
} }
/** returns the value of a given key as a double */ /** returns the value of a given key as a double */
double CCConfiguration::getNumber( const char *key ) const double CCConfiguration::getNumber( const char *key, double default_value ) const
{ {
CCObject *ret = m_pDefaults->objectForKey(key); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -270,8 +268,7 @@ double CCConfiguration::getNumber( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return 0.0;
} }
CCObject * CCConfiguration::getObject( const char *key ) const CCObject * CCConfiguration::getObject( const char *key ) const

View File

@ -106,14 +106,17 @@ public:
bool init(void); bool init(void);
/** returns the value of a given key as a string */ /** returns the value of a given key as a string.
const char* getCString( const char *key ) const; If the key is not found, it will return the default value */
const char* getCString( const char *key, const char *default_value=NULL ) const;
/** returns the value of a given key as a boolean */ /** returns the value of a given key as a boolean.
bool getBool( const char *key ) const; If the key is not found, it will return the default value */
bool getBool( const char *key, bool default_value=false ) const;
/** returns the value of a given key as a double */ /** returns the value of a given key as a double.
double getNumber( const char *key ) const; If the key is not found, it will return the default value */
double getNumber( const char *key, double default_value=0.0 ) const;
/** returns the value of a given key as a double */ /** returns the value of a given key as a double */
CCObject * getObject( const char *key ) const; CCObject * getObject( const char *key ) const;

View File

@ -6,11 +6,13 @@
TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig); TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig);
TESTLAYER_CREATE_FUNC(ConfigurationQuery); TESTLAYER_CREATE_FUNC(ConfigurationQuery);
TESTLAYER_CREATE_FUNC(ConfigurationInvalid); TESTLAYER_CREATE_FUNC(ConfigurationInvalid);
TESTLAYER_CREATE_FUNC(ConfigurationDefault);
static NEWTESTFUNC createFunctions[] = { static NEWTESTFUNC createFunctions[] = {
CF(ConfigurationLoadConfig), CF(ConfigurationLoadConfig),
CF(ConfigurationQuery), CF(ConfigurationQuery),
CF(ConfigurationInvalid) CF(ConfigurationInvalid),
CF(ConfigurationDefault)
}; };
static int sceneIdx=-1; static int sceneIdx=-1;
@ -186,3 +188,37 @@ std::string ConfigurationInvalid::subtitle()
{ {
return "Loading an invalid config file"; return "Loading an invalid config file";
} }
//------------------------------------------------------------------
//
// ConfigurationDefault
//
//------------------------------------------------------------------
void ConfigurationDefault::onEnter()
{
ConfigurationBase::onEnter();
const char *c_value = CCConfiguration::sharedConfiguration()->getCString("invalid.key", "no key");
if( strcmp(c_value, "no key") != 0 )
CCLOG("1. Test failed!");
else
CCLOG("1. Test OK!");
bool b_value = CCConfiguration::sharedConfiguration()->getBool("invalid.key", true);
if( ! b_value )
CCLOG("2. Test failed!");
else
CCLOG("2. Test OK!");
double d_value = CCConfiguration::sharedConfiguration()->getNumber("invalid.key", 42.42);
if( d_value != 42.42 )
CCLOG("3. Test failed!");
else
CCLOG("3. Test OK!");
}
std::string ConfigurationDefault::subtitle()
{
return "Tests defaults values";
}

View File

@ -52,4 +52,12 @@ public:
virtual std::string subtitle(); virtual std::string subtitle();
}; };
class ConfigurationDefault : public ConfigurationBase
{
public:
virtual void onEnter();
virtual std::string subtitle();
};
#endif // __CONFIGURATIONTEST_H__ #endif // __CONFIGURATIONTEST_H__