📄 string.h
字号:
// ***************************************************************
// String version: 1.0
// -------------------------------------------------------------
// File Name: String.h
// Created: 2007/07/18
// Modified: 2007/07/18 23:03
// Author: William.Liang
// Msn: lwq49@msn.com
// Email: lwq49@21cn.com, lwq49@msn.com
// Description:
//
// Purpose:
// -------------------------------------------------------------
// license:
//
// The contents of this file are subject to the Mozilla Public
// License Version 1.1 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy
// of the License at http://www.mozilla.org/MPL/ Software dis-
// tributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or im-
// plied. See the License for the specific language governing
// rights and limitations under the License.
//
// The Initial Developer of the Original Code is William.Liang .
// Copyright (C) 2007 - All Rights Reserved.
// ***************************************************************
#include "stdafx.h"
#include "tchar.h"
//***************************************************************
// 对象引用计数器
//***************************************************************
template<class TYPE, class ARG_TYPE=const TYPE&>
class RCObject{ // base class for reference
public:
TYPE* m_lpData; //引用计数对象
protected:
int* m_nRefCount; //引用计数
public:
//构造函数
RCObject();
RCObject(const RCObject& RefObject);
//析构函数
virtual ~RCObject();
//操作符重载
public:
//重载=符
RCObject& operator=(const RCObject& RefObject);
//重载!=符
bool operator!=(const RCObject& RefObject);
//重载==符
bool operator==(const RCObject& RefObject);
//重载模板参数符
operator ARG_TYPE();
public:
//增加引用
void AddReference();
//减少引用
void DeReference();
//新引用
void Renew();
//是否被引用
bool IsReference() const;
//释放关联对象
void Release();
};
//构造函数
template<class TYPE, class ARG_TYPE>
inline RCObject<TYPE, ARG_TYPE>::RCObject(){
m_nRefCount = new int;
*m_nRefCount = 1;
m_lpData = NULL;
}
//构造函数
template<class TYPE, class ARG_TYPE>
inline RCObject<TYPE, ARG_TYPE>::RCObject(const RCObject& RefObject){
m_nRefCount = NULL;
m_lpData = NULL;
*this = RefObject;
}
//析构函数
template<class TYPE, class ARG_TYPE>
inline RCObject<TYPE, ARG_TYPE>::~RCObject() throw(){
//减少引用计数
DeReference();
//判断是否仍被引用,否则删除计数器指针
if(!IsReference()){
SafeDelete(m_nRefCount);
}
}
//增加引用
template<class TYPE, class ARG_TYPE>
inline void RCObject<TYPE, ARG_TYPE>::AddReference(){
if(m_nRefCount)
++(*m_nRefCount);
}
//减少引用
template<class TYPE, class ARG_TYPE>
inline void RCObject<TYPE, ARG_TYPE>::DeReference(){
if(m_nRefCount)
if (--(*m_nRefCount) <= 0){
Release();
}
}
//新引用
template<class TYPE, class ARG_TYPE>
inline void RCObject<TYPE, ARG_TYPE>::Renew(){
//减少原引用
DeReference();
//判断是否仍被引用,否则删除计数器指针
if(!IsReference()){
SafeDelete(m_nRefCount);
}
//新的计数地址
m_nRefCount = new int;
*m_nRefCount = 1;
}
//是否被引用
template<class TYPE, class ARG_TYPE>
inline bool RCObject<TYPE, ARG_TYPE>::IsReference() const{
if(m_nRefCount)
return *m_nRefCount > 0;
return false;
}
//释放关联对象
template<class TYPE, class ARG_TYPE>
inline void RCObject<TYPE, ARG_TYPE>::Release(){
//释放引用对象指针
SafeDelete(m_lpData);
}
//重载=符
template<class TYPE, class ARG_TYPE>
inline RCObject<TYPE, ARG_TYPE>& RCObject<TYPE, ARG_TYPE>::operator=(const RCObject& RefObject){
//两者相等则不作任何处理
if((*this)!=RefObject){
//原引用计数减1
DeReference();
//指向拷贝值的地址
m_nRefCount = RefObject.m_nRefCount;
m_lpData = RefObject.m_lpData;
//由于源与目标的计数地址相同,则目标的计数+1,等于所有引用元素的计数器+1
++(*m_nRefCount);
}
return *this;
}
//重载=符
//template<class TYPE, class ARG_TYPE>
//ARG_TYPE RCObject<TYPE, ARG_TYPE>::operator=(TYPE* Value){
// //m_lpData = new ;
// //return (ARG_TYPE)m_lpData;
//}
//重载!=符
template<class TYPE, class ARG_TYPE>
inline bool RCObject<TYPE, ARG_TYPE>::operator!=(const RCObject& RefObject){
return !(*this==RefObject);
}
//重载==符
template<class TYPE, class ARG_TYPE>
inline bool RCObject<TYPE, ARG_TYPE>::operator==(const RCObject& RefObject){
//以引用对象的指针地址为比较目标,地址相同则为相等,否则为不等
if(m_lpData && m_lpData==RefObject.m_lpData)
return true;
return false;
}
//重载模板参数符
template<class TYPE, class ARG_TYPE>
inline RCObject<TYPE, ARG_TYPE>::operator ARG_TYPE(){
return (ARG_TYPE)m_lpData;
}
/////////////////////////////////////////////////////////////////////////////////////
//***************************************************************
// 字串管理类。支持对象引用计数,并自动在引用完毕自动注销
//***************************************************************
class _CString { // class to be used by
protected:
//引用计数器
RCObject<TCHAR, LPCTSTR> m_StringValue;
public:
//构造函数
_CString(){};
_CString(const _CString& Value);
_CString(LPCTSTR Value);
//_CString(int Value);
//重载LPCTSTR
operator LPCTSTR();
//重载int
operator int();
//重载double
operator double();
//重载float
operator float();
//重载bool
operator bool();
//重载==符
bool operator==(_CString& Value);
//重载==符
bool operator==(LPCTSTR Value);
//重载=符
LPCTSTR operator=(LPCTSTR Value);
//重载[]符
TCHAR* operator[](INT_PTR nPos);
//返回长度
INT_PTR GetLength();
//是否为空
bool IsNull();
};
//构造函数
inline _CString::_CString(const _CString& Value){
m_StringValue = Value.m_StringValue;
}
//构造函数
inline _CString::_CString(LPCTSTR Value){
*this = Value;
}
//
//inline _CString::_CString(int Value){
// TCHAR szInt[33];
// itoa(Value, szInt, 10);
// *this = szInt;
//}
//重载LPCTSTR
inline _CString::operator LPCTSTR(){
return (LPCTSTR)m_StringValue;
}
//重载int
inline _CString::operator int(){
if(m_StringValue==NULL) return 0;
int Re = atoi(m_StringValue);
return Re;
}
//重载double
inline _CString::operator double(){
if(m_StringValue==NULL) return 0;
double Re = atof(m_StringValue);
return Re;
}
//重载float
inline _CString::operator float(){
return (float)(double)(*this);
}
//重载bool
inline _CString::operator bool(){
if(m_StringValue==NULL) return false;
return strcmp(m_StringValue, "true")==0;
}
//重载==符
inline bool _CString::operator==(_CString& Value){
if(IsNull() || Value.IsNull()) return false;
return _tcscmp(m_StringValue.m_lpData, Value.m_StringValue.m_lpData)==0;
}
//重载==符
inline bool _CString::operator==(LPCTSTR Value){
if(IsNull()) return false;
return _tcscmp(m_StringValue.m_lpData, Value)==0;
}
//重载=符
inline LPCTSTR _CString::operator=(LPCTSTR Value){
//重置计数器
m_StringValue.Renew();
if(Value==NULL){
m_StringValue.m_lpData = NULL;
}
else{
//拷贝数据
m_StringValue.m_lpData = new TCHAR[strlen(Value)+1];
_tcscpy(m_StringValue.m_lpData, Value);
}
return (LPCTSTR)m_StringValue;
}
//重载[]符
inline TCHAR* _CString::operator[](INT_PTR nPos){
if(m_StringValue.m_lpData && nPos<(INT_PTR)strlen(m_StringValue.m_lpData))
return m_StringValue.m_lpData+nPos;
return NULL;
}
//返回长度
inline INT_PTR _CString::GetLength(){
if(m_StringValue.m_lpData)
return strlen(m_StringValue.m_lpData);
return 0;
}
//是否为空
inline bool _CString::IsNull(){
return m_StringValue.m_lpData==NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -