cocos2dx中CCString类实现原理的初步学习
来源:互联网 发布:机械手编程语言 编辑:程序博客网 时间:2024/04/29 10:39
cocos2dx是C++实现的一种游戏引擎,CCString是其中的一种字符串数据结构。CCString类的成员变量很简单,只有一个公开的std::string类型的成员变量,去掉所有的成员函数,CCString类如下:
class CC_DLL CCString : public CCObject
{
public:
....
public:
std::string m_sString;
};
CCString类继承自CCObject类,和其他cocos2dx里面CCObject子类一样,都含有一钟静态的create函数,CCString包含如下create静态函数
1)static CCString* create(const std::string &str);
以std::string作为参数创建一个CCString对象
CCString* CCString::create(const std::string& str)
{
CCString* pRet = new CCString(str);
pRet->autorelease();
return pRet;
}
这个函数的实现过程只有简单的3行,第一行采用构造函数new出一个对象,第二行则将对象加入到自动释放池,已到达延迟释放的目的。
2)static CCString* createWithFormat(const char* format, ...) CC_FORMAT_PRINT(1, 2);
以字符串格式及参数创建一个CCString对象
CCString* CCString::createWithFormat(const char* format, ...)
{
CCString* pRet = CCString::create("");
va_list ap;
va_start(ap, format);
pRet->initWithFormatAndValist(format, ap);
va_end(ap);
return pRet;
}
initWithFormatAndValist函数中申请的字符串缓存大小为100k,所以用这种字符串格式及参数创建CCString对象的方式最大支持的数据大小要少于100k。通常在使用过程中用到的数据量都会在几KB之内,所以这个限制肯能满足开发的需求的。
3)static CCString* createWithData(const unsigned char* pData, unsigned long nLen);
以字符串创建一个CCString对象,pData是一段字符串,当nLen < strlen(pData)时,以nLen为长度截断;当nLen>strlen(pData)时,和createWithData(pData, strlen(pData))效果一样;
CCString* CCString::createWithData(const unsigned char* pData, unsigned long nLen)
{
CCString* pRet = NULL;
if (pData != NULL)
{
char* pStr = (char*)malloc(nLen+1);
if (pStr != NULL)
{
pStr[nLen] = '\0';
if (nLen > 0)
{
memcpy(pStr, pData, nLen);
}
pRet = CCString::create(pStr);
free(pStr);
}
}
return pRet;
}
4)static CCString* createWithContentsOfFile(const char* pszFileName);
以文件中存储内容创建一个CCString对象,它的实现方式是把文件内容读入字符串中,在调用createWithData函数实现创建对象;
CCString* CCString::createWithContentsOfFile(const char* pszFileName)
{
unsigned long size = 0;
unsigned char* pData = 0;
CCString* pRet = NULL;
pData = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, "rb", &size);
pRet = CCString::createWithData(pData, size);
CC_SAFE_DELETE_ARRAY(pData);
return pRet;
}
另外,CCString也提供了一些将自身内容转化为常用数据类型(int, unsigned int, float, double,bool)的函数接口。
int CCString::inValue() const;
unsigned int CCString::uintValue() const;
float CCString::floatValue() const;
double CCString::doubleValue() const;
以上这几个函数比较相似,分别调用了atoi,atof这2个将字符串转化为整型和double型系统函数;
int CCString::intValue() const
{
if (length() == 0)
{
return 0;
}
return atoi(m_sString.c_str());
}
bool CCString::boolValue() const;这个函数的实现有必要单独列出来,它考虑了以整形和bool型2种数据类型存入字符串的情况,实现代码如下:
bool CCString::boolValue() const
{
if (length() == 0)
{
return false;
}
if (0 == strcmp(m_sString.c_str(), "0") || 0 == strcmp(m_sString.c_str(), "false"))
{
return false;
}
return true;
}
从上述函数实现中,很容易看到返回false的情况有且只有三种:空字符串;字符串”0”;字符串”false”。
- cocos2dx中CCString类实现原理的初步学习
- cocos2dx 使用CCString在UITextArea中输出%
- cocos2dx里的格式刷CCString
- cocos2dx中菜单响应回调的实现原理
- cocos2dx中菜单响应回调的实现原理
- cocos2dx中菜单响应回调的实现原理
- cocos2dx clippingNode的实现原理
- cocos2dx-2.x CCString代码
- cocos2dx 3.2键盘操控的列表页的初步实现
- 我的Cocos2d-x学习笔记(十九)CCString、CCArray
- cocos2dx中lambda表达式几种用法的初步总结
- cocos2dx中lambda表达式几种用法的初步总结
- cocos2dx 如何将int转成CCString
- CCString的简单使用
- protobuf_实现rpc的初步原理
- CCString
- CCString
- Cocos2dx 中大量读取图片的实现
- hdu 4856
- 弹跳小球 V1.0
- UVA10891- Game of Sum
- 解决ubuntu下网络已断开的问题
- POJ 1183排列
- cocos2dx中CCString类实现原理的初步学习
- 马踏棋盘之字典序遍历棋盘(简单深搜)
- GDB 调试心得
- 弹窗代码集合,自由控制你的弹出窗口
- Linux内核同步—atomic_t
- 线段树二维转一维CodeforcesBeta Round #19 D. Points(好)
- 图的广度遍历和深度遍历
- target-action传值
- Linux内核同步—kref