-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathptr.h
More file actions
executable file
·87 lines (76 loc) · 1.44 KB
/
ptr.h
File metadata and controls
executable file
·87 lines (76 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _PTR_H_
#define _PTR_H_
#include <cstddef>
#include <stdexcept>
#include <iostream>
/*
template <class T>
T* clone(const T* tp) {
return tp->clone();
}*/
/*#include "core.h"
core* clone(const core* tp)
{
return tp->clone();
}*/
#include "core.h"
template <class T>
class Ptr {
public :
Ptr() : p(0), refptr(new std::size_t(1)) { }
Ptr(T* t) : p(t), refptr(new std::size_t(1)) { }
Ptr(const Ptr& rhs) : p(rhs.p), refptr(rhs.refptr) { ++*refptr; }
Ptr& operator=(const Ptr& rhs) {
++*rhs.refptr;
if (--*refptr == 0) {
delete p;
delete refptr;
}
p = rhs.p;
refptr = rhs.refptr;
return *this;
}
~Ptr() {
if (--*refptr == 0) {
delete p;
delete refptr;
}
}
void make_unique() {
if (*refptr != 1) {
--*refptr;
p = p ? clone(p) : 0;
refptr = new std::size_t(1);
/* if (p)
p = clone( (&core::clone()) );
else
p = 0;
refptr = new std::size_t(1);
*/ }
}
operator bool() const { return p; }
T* operator->() const {
if (p) return p;
else throw std::runtime_error("unbound Ptr");
}
T& operator*() const {
if (p) return *p;
else throw std::runtime_error("unbound Ptr");
}
private :
T* p;
std::size_t* refptr;
};
/*
template <class T>
T* clone(T* (*fp)( ))
{
return (*fp)();
}*/
template <class T>
T* clone(const T* tp)
{
std::cout << "In ptr clone() " << std::endl;
return tp->clone();
}
#endif