15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CCObjectの情報を出力する

Posted at

CCDictionary、CCArrayの中身を簡単に取り出せるマクロが欲しいと思ったので、JSON風に出力する関数・マクロを作ってみました。

void outputObject(CCObject* obj, const char* key, int level);

inline const char* getIndentString(int level)
{
    CCString* str = CCString::create("");
    for(int i = 0; i < level; ++i)
    {
        str = appendCCString(str, CCString::create("   "));
    }
    
    return str->getCString();
}

inline void outputArray(CCArray* array, int level)
{
    CCLOG("%s{", getIndentString(level));
    
    for(int i = 0 ; i < array->count(); ++i)
    {
        CCObject* value = array->objectAtIndex(i);
        
        outputObject(value, GetCStrI(i), level + 1);
    }
    
    CCLOG("%s}", getIndentString(level));
}

inline void outputDictionary(CCDictionary* dictionary, int level)
{
    CCLOG("%s{", getIndentString(level));
    
    CCArray* allKeys = dictionary->allKeys();
    CCObject* obj;
    CCARRAY_FOREACH(allKeys, obj)
    {
        CCString* key = (CCString*)obj;
        
        CCObject* value = dictionary->objectForKey(key->getCString());
        
        outputObject(value, key->getCString(), level + 1);
    }
    
    CCLOG("%s}", getIndentString(level));
}

inline void outputObject(CCObject* obj, const char* key, int level)
{
    const char* initialString;
    if(strlen(key) == 0)
    {
        initialString = "";
    }
    else
    {
        initialString = GetCStrFmt("%s\"%s\" : ", getIndentString(level), key);
    }
    
    if(typeid(*obj) == typeid(CCString))
    {
        CCLOG("%s\"%s\"", initialString, ((CCString*)obj)->getCString());
    }
    else if(typeid(*obj) == typeid(CCInteger))
    {
        CCLOG("%s%d", initialString, ((CCInteger*)obj)->getValue());
    }
    else if(typeid(*obj) == typeid(CCArray))
    {
        CCLOG("%sCCArray( %d Objects )", initialString, ((CCArray*)obj)->count());
        outputArray((CCArray*)obj, level);
    }
    else if(typeid(*obj) == typeid(CCDictionary))
    {
        CCLOG("%sCCDictionary( %d Key/Values)", initialString, ((CCDictionary*)obj)->allKeys()->count());
        outputDictionary((CCDictionary*)obj, level);
    }
    else
    {
        CCLOG("%s%s", initialString, typeid(obj).name());
    }
}

#define OutputCCObject( obj )       ( outputObject( obj , "", 0 ) )

以下のように使います。


CCDictionary* dictionary = CCDictionary::create();
dictionary->setObject(CCString::create("ABC"), "StringSample");
dictionary->setObject(CCInteger::create(01234), "IntegerSample");

CCDictionary* dictA = CCDictionary::create();
dictA->setObject(CCString::create("JP"), "Language");
dictA->setObject(CCString::create("こんにちは"), "Hello");

CCDictionary* dictB = CCDictionary::create();
dictB->setObject(CCString::create("EN"), "Language");
dictB->setObject(CCString::create("HELLO"), "Hello");

dictionary->setObject(CCArray::create(dictA, dictB, NULL), "CollectionSample");

OutputCCObject( dictionary );

出力結果はこんな感じ。


CCDictionary( 3 Key/Values)
{
   "StringSample" : "ABC"
   "IntegerSample" : 668
   "CollectionSample" : CCArray( 2 Objects )
   {
      "0" : CCDictionary( 2 Key/Values)
      {
         "Language" : "JP"
         "Hello" : "こんにちは"
      }
      "1" : CCDictionary( 2 Key/Values)
      {
         "Language" : "EN"
         "Hello" : "HELLO"
      }
   }
}

15
14
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?