zoukankan
html css js c++ java
自己写的智能指针源码
//
-------------------------------------------------
//
Smart Pointer
//
-------------------------------------------------
#ifndef _SMART_POINTER_H
#define
_SMART_POINTER_H
extern
std::list
<
short
>
ptrCounter;
template
<
class
T
>
class
smartPtr
{
private
:
T
*
ptr;
short
*
counter;
#ifdef _DEBUG
#define
_DEBUG_FILE_NAME_LEN 32
#define
_DEBUG_RECORD_LEN 32
typedef
struct
__debug_record
{
char
file[_DEBUG_FILE_NAME_LEN];
int
line;
}
debug_record;
public
:
debug_record ptr_changed_rec[_DEBUG_RECORD_LEN];
int
ptr_changed_time;
#endif
//
_DEBUG
public
:
#ifdef _DEBUG
smartPtr(LPCSTR file
=
"
Construction By Default
"
,
int
line
=
__LINE__)
{
#else
smartPtr()
{
#endif
//
_DEBUG
ptr
=
new
T;
ptrCounter.push_back(
1
);
counter
=
&
ptrCounter.back();
#ifdef _DEBUG
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
#ifdef _DEBUG
smartPtr(
const
T
*
np,LPCSTR file
=
"
Construction By Pointer
"
,
int
line
=
__LINE__)
{
#else
smartPtr(
const
T
*
np)
{
#endif
//
_DEBUG
ptr
=
const_cast
<
T
*>
(np);
ptrCounter.push_back(
1
);
counter
=
&
ptrCounter.back();
#ifdef _DEBUG
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
smartPtr(
const
smartPtr
<
T
>
&
np)
{
ptr
=
np.ptr;
counter
=
np.counter;
(
*
counter)
++
;
#ifdef _DEBUG
LPCSTR file
=
"
Construction By Copying
"
;
int
line
=
__LINE__;
memset(ptr_changed_rec,
0
,
sizeof
(debug_record)
*
_DEBUG_RECORD_LEN);
strncpy(ptr_changed_rec[
0
].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[
0
].line
=
line;
ptr_changed_time
=
1
;
#endif
//
_DEBUG
}
void
operator
=
(
const
smartPtr
<
T
>
&
np)
{
if
(counter
!=
NULL)
{
(
*
counter)
--
;
if
((
*
counter)
<=
0
)
delete ptr;
}
ptr
=
np.ptr;
counter
=
np.counter;
(
*
counter)
++
;
#ifdef _DEBUG
LPCSTR file
=
"
Value Assignment
"
;
int
line
=
__LINE__;
strncpy(ptr_changed_rec[ptr_changed_time].file,file,_DEBUG_FILE_NAME_LEN);
ptr_changed_rec[ptr_changed_time].line
=
line;
ptr_changed_time
++
;
#endif
//
_DEBUG
}
~
smartPtr()
{
(
*
counter)
--
;
if
((
*
counter)
<=
0
)
delete ptr;
}
T
*
operator
->
()
{
return
ptr;
}
T
*
real()
{
return
ptr;
}
}
;
#endif
//
_SMART_POINTER_H
查看全文
相关阅读:
os 模块删除图片操作
python base64基本使用
django 跨域
git 回滚
多线程
原生sql子查询 和psql 子查询
Python 3.x 中"HTTP Error 403: Forbidden"问题的解决方案
报错(AttributeError: 'str' object has no attribute 'items')的解决办法
C++虚函数
C++泛型程序设计和多态
原文地址:https://www.cnblogs.com/len3d/p/234612.html
最新文章
9.JavaScript简单计算器的实现
8.javascript获取表单中两个数字,并判断大小
7.javascript如何调试代码
6.javaScript中的二维数组
5.sublime的整行移动
4.php奇葩的地方,反引号``
3.php数据类型中NULL,"",0的比较
2.sublime的配置,
PAT1039
algorithm 头文件
热门文章
PAT1038(两个运行超时 未解决
PAT1034
PAT1033
PAT1032
PAT1031
PAT1029
PAT1028
PAT1027
python 字典、列表、字符串 之间的转换
Running a worker with superuser privileges when the worker accepts messages serialized with pickle i
Copyright © 2011-2022 走看看