File: SmartPtr.h

package info (click to toggle)
dibbler 0.7.3-1.3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,148 kB
  • ctags: 8,720
  • sloc: cpp: 54,863; sh: 9,389; ansic: 8,659; yacc: 2,570; makefile: 1,061; lex: 842; perl: 49; xml: 6
file content (168 lines) | stat: -rw-r--r-- 3,248 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * $Id: SmartPtr.h,v 1.7 2007-01-02 01:39:01 thomson Exp $
 *
 * $Log: SmartPtr.h,v $
 * Revision 1.7  2007-01-02 01:39:01  thomson
 * Code cleanup.
 *
 * Revision 1.6  2006-12-25 20:47:01  thomson
 * Some memory leaks fixes, valgrind info added.
 *
 * Revision 1.5  2006-07-03 18:01:51  thomson
 * SPtr define added.
 *
 * Revision 1.4  2004/06/04 19:47:06  thomson
 * Various fixes.
 *
 * Revision 1.3  2004/06/04 16:55:27  thomson
 * *** empty log message ***
 *
 * Revision 1.2  2004/03/29 22:06:49  thomson
 * 0.1.1 version
 *
 *
 * Released under GNU GPL v2 licence
 *
 */

#ifndef SMARTPTR_H
#define SMARTPTR_H

#include <iostream>

#define SPtr SmartPtr

class Ptr {
public:
    //constructor used in case of NULL SmartPtr
    Ptr() {
	ptr=NULL;
	refcount=1;
    }
    //Constructor used in case of non NULL SmartPtr
    Ptr(void* sth) {
	ptr=sth;
	refcount=1;
    }
    
    ~Ptr() {
	//if(ptr) delete ptr;
    }
    int refcount; //refrence counter
    void * ptr;	  //pointer to the real object
};

template <class T>
class SmartPtr
{

	//Don't use this class alone, it's used only in casting 
	//one smartpointer to another smartpointer 
	//e.g.
	//SmartPtr<a> a(new a()); SmartPtr<b> b(new(b)); a=b;

public:
    SmartPtr();
    SmartPtr(T* something);
	SmartPtr(Ptr *voidptr) { 
        if(voidptr) 
        {
            this->ptr=voidptr; 
            this->ptr->refcount++;
        }
        else
            this->ptr=new Ptr();
    }
    SmartPtr(const SmartPtr & ref);
	SmartPtr(int onlyNull);
	SmartPtr& operator=(const SmartPtr& old);

	operator Ptr*() {
	    if (this->ptr->ptr) 
		return this->ptr;
	    else
		return (Ptr*)NULL;
	}

    int refCount();
    ~SmartPtr();
    T& operator*() const;
    T* operator->() const;

 private:
    Ptr * ptr;
};

template <class T> SmartPtr<T>::SmartPtr() {
    ptr = new Ptr();
}

template <class T> int SmartPtr<T>::refCount() {
    if (this->ptr)
	return this->ptr->refcount;
    return 0;
}

template <class T>
SmartPtr<T>::SmartPtr(T* something) {
    ptr = new Ptr(something);
}

template <class T>
SmartPtr<T>::SmartPtr(const SmartPtr& old) {
	old.ptr->refcount++;
	this->ptr = old.ptr;
    this->ptr->refcount=old.ptr->refcount;
}

template <class T>
SmartPtr<T>::~SmartPtr() {
    if (!(--(ptr->refcount))) {
	delete (T*)(ptr->ptr);
	delete ptr;
    }
}

template <class T>
T& SmartPtr<T>::operator*() const {
    return *((T*)(ptr->ptr)); //it can return NULL
}

template <class T>
T* SmartPtr<T>::operator->() const {
    if (!ptr) {
	return 0;
    }
    return (T*)(ptr->ptr); //it can return NULL
}

//It's is called in eg. instrusction: return NULL;
//and SmartPtr is returned in function
template <class T>
SmartPtr<T>::SmartPtr(int )
{
	ptr=new Ptr(); //this->ptr->ptr is NULL
}

template <class T>
SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr& old) {
	if (this==&old)
		return *this;
	if (this->ptr) 
		if(!(--this->ptr->refcount))
		{
		    delete (T*)(this->ptr->ptr);
		    delete this->ptr;
		    this->ptr=NULL;
		}
		this->ptr=old.ptr;
		old.ptr->refcount++;
		//    cout << "operator=" << endl;
		return *this;
}
#endif