cocos2d-x節點(CCString.h)API

本文來自http://blog.csdn.net/runaying ,引用必須註明出處!

cocos2d-x節點(CCString.h)API

溫馨提醒:爲了大家能更好學習,強烈推薦大家看看本人的這篇博客 Cocos2d-X權威指南筆記

CCString 很強大,各種數據類型的轉換(把字符串轉化爲 doublefloatinteger......)。字符串的分割和比較

///\cocos2d-x-3.0alpha0\cocos2dx\cocoa
//CCString 很強大,各種數據類型的轉換(把字符串轉化爲 double、float、integer......)。字符串的分割和比較

#ifndef __CCSTRING_H__
#define __CCSTRING_H__

#if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
#include <string.h>
#endif

#include <stdarg.h>
#include <string>
#include <functional>
#include "CCObject.h"

NS_CC_BEGIN

/**
 * @addtogroup data_structures
 * @{
 */

class CC_DLL String : public Object, public Clonable
{
public:
    /**
     * @js NA
     * @lua NA
     */
    String();
    /**
     * @js NA
     * @lua NA
     */
    String(const char* str);
    /**
     * @js NA
     * @lua NA
     */
    String(const std::string& str);
    /**
     * @js NA
     * @lua NA
     */
    String(const String& str);
    /**
     * @js NA
     * @lua NA
     */
    virtual ~String();
    
    /* 重載(override)賦值運算符
     * @js NA
     * @lua NA
     */
    String& operator= (const String& other);

    /** 使用 format 初始化字符串, 他類似於 C 函數的 'sprintf' 
     * @js NA
     * @lua NA
     */
    bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(2, 3);

    /** 轉換爲int值
     * @js NA
     */
    int intValue() const;

    /**轉換爲 unsigned 值
     * @js NA
     */
    unsigned int uintValue() const;

    /** 轉換爲 float 值 
     * @js NA
     */
    float floatValue() const;

    /** 轉換爲 double 值 
     * @js NA
     */
    double doubleValue() const;

    /** 轉換爲 bool 值 
     * @js NA
     */
    bool boolValue() const;

    /** get the C string 
     * @js NA
     */
    const char* getCString() const;

    /** get  string 的長度
     * @js NA
     */
    unsigned int length() const;

    /** C字符串比較
     * @js NA
     */
    int compare(const char *) const;

    /** 當前值的末尾追加額外的字符 
     * @js NA
     * @lua NA
     */
    void append(const std::string& str);

    /** 當前值的末尾追加(w/ format)額外的字符
     * @js NA
     * @lua NA
     */
    void appendWithFormat(const char* format, ...);

    /** 將字符串分割
     * @js NA
     * @lua NA
     */
    Array* componentsSeparatedByString(const char *delimiter);
    
    /* override functions 
     * @js NA
     */
    virtual bool isEqual(const Object* pObject);

    /** 使用 std string 創建一個字符串, 你也可以使用一個  c string 指針,因爲 std::string 的默認構造函數可以接受一個 c string 指針. 
     *  @return 一個字符串指針,這是一個自動釋放的對象指針,
     *          這意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* create(const std::string& str);

    /**  使用 format 創建字符串, 他類似於 C 函數的 'sprintf' , 默認的緩衝大小是 (1024*100) bytes,
     *  如果你想改變它,你應該在 String.cpp 文件裏面修改 kMaxStringLen 宏.
     *  @return  一個字符串指針,這是一個自動釋放的對象指針,
     *          這意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2);

    /** create 一個 binary data 創建字符串
     *  @return  一個字符串指針,這是一個自動釋放的對象指針,
     *          這意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithData(const unsigned char* pData, unsigned long nLen);

    /** create 一個文件創建字符串
     *  @return  一個字符串指針,這是一個自動釋放的對象指針,
     *          這意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithContentsOfFile(const char* filename);
    /**
     * @js NA
     * @lua NA
     */
    virtual void acceptVisitor(DataVisitor &visitor);
    /**
     * @js NA
     * @lua NA
     */
    virtual String* clone() const;
    
private:

    /** 僅供內部使用 */
    bool initWithFormatAndValist(const char* format, va_list ap);

public:
    std::string _string;
};

struct StringCompare : public std::binary_function<String *, String *, bool> {
    public:
        bool operator() (String * a, String * b) const {
            return strcmp(a->getCString(), b->getCString()) < 0;
        }
};

#define StringMake(str) String::create(str)
#define ccs               StringMake

// end of data_structure group
/// @}

NS_CC_END

#endif //__CCSTRING_H__


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章