A
download SmartPtr.h
Language: C++
Copyright: (c) 1998,1999,2000
LOC: 403
Project Info
TableHockey3D(tablehockey)
Server: SourceForge
Type: cvs
...ey\CVSROOT\tablehockey\src\
   Application.cpp
   Application.h
   Camera.cpp
   Camera.h
   Cube.cpp
   Cube.h
   Data.cpp
   Data.h
   Disk.cpp
   Disk.h
   FloatingText.cpp
   FloatingText.h
   Floor.cpp
   Floor.h
   Input.cpp
   Input.h
   Light.cpp
   Light.h
   log.cpp
   log.h
   main.cpp
   mmgr.cpp
   mmgr.h
   Model.cpp
   Model.h
   nommgr.h
   Puck.cpp
   Puck.h
   Pyramid.cpp
   Pyramid.h
   scene.cpp
   scene.h
   SmartPtr.h
   Sound.cpp
   Sound.h
   TableHockey.dsp
   TableHockey.dsw
   TestScene.cpp
   TestScene.h
   text.cpp
   text.h
   TextHelp.cpp
   TextHelp.h
   texture.cpp
   texture.h
   TexturedCube.cpp
   TexturedCube.h
   timer.cpp
   timer.h
   View.cpp
   View.h

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#ifndef	_SMARTPTR_H_
#define	_SMARTPTR_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <assert.h>
#define ASSERT assert

#include <windows.h>

/*************************************************************************
 FILE :			SmartPtr.h
 Date :			20 December 1998

 Author :		Stefan Tchekanov  (stefant@iname.com)

 Description:	The template class SmartPtr performs Reference Counting
				Garbage Collection and/or Object Level Thread Synchronization.

 Classes :		SmartPtr		-	The SmartPtr class
				RefCountPtr		-	Reference Counting Garbage Collection
				SyncPtr			-	Synchronized access without Reference Counting Garbage Collection
				SyncRefCountPtr	-	Synchronized access with Reference Counting Garbage Collection
				SmartPtrBase	-	The base of all above classes. Used as a common base so
									an assignment between different pointers is able.
				
				For examples how to use these classes look at the end
				of this file.

				Implementation classes that SHOULD NEVER be used directly.
 					CRefCountRep
 					CSyncAccessRep
 					CSyncRefCountRep
 					CSyncAccess

Copyright notice:
	Written by Stefan Tchekanov (stefant@iname.com)
	Copyright(c) 1998,1999,2000

This code may be used in compiled form in any way you desire. This
file may be redistributed unmodified by any means PROVIDING it is 
not sold for profit without the authors written consent, and 
providing that this notice and the authors name is included. If 
the source code in this file is used in any commercial application 
then a simple email would be nice.

This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability if it causes any damage to your
computer.

*************************************************************************/
/* #    Revisions    # */

/*************************************************************************
  REVISION ON 7.January.2000 14:31:34  By Stefan Tchekanov
 
  Comments  : SmartPtr objects can point to objects of other types
			  not only to objects of their type.
 
 *************************************************************************/


/*************************************************************************
  REVISION ON 09 April 1999 11:24:38 By Stefan Tchekanov
 
  Comments  : SmartPtr objects now have size of a real pointer, Which
			  means that passing a SmartPtr as a function parameter is 
			  as efficient as passing an int or a pointer.
 
 *************************************************************************/


/////////////////////////////////////////////////////////////////////////////
//	If you wrap a non-class with the SmartPtr class, you will receive 
//	this warning. i.e.  int, short, etc...
//	It is a warning you may ignore.
#pragma warning( disable : 4284 )
//
//	This warning is generated when compiling in debug mode.
//	Debug symbols cannot be longer than 255 but when using templates
//	it is usual to have debug symbols longer tahn 255.
//	You may ignore this warning.
#pragma warning( disable : 4786 )

/////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////
//	Representation class just for reference counting
/////////////////////////////////////////////////////////////////////////////

template<class T>
class CRefCountRep {

//	Constructors and destructor
public:
	CRefCountRep( const T* ptr );
	~CRefCountRep();


//	Operations
public:
	long	incrRefCount();
	long	decrRefCount();

	T*		getPointer() const;
	T*		getRealPointer() const;

	bool	isNull() const;


//	Implementation
private:
	T*		m_pRealPtr;
	long	m_counter;
};
/////////////////////////////////////////////////////////////////////////////

template<class T>
CRefCountRep<T>::CRefCountRep( const T* ptr )
 : m_pRealPtr( (T*)ptr ), m_counter( 0 ) {
}

template<class T>
CRefCountRep<T>::~CRefCountRep() {
	ASSERT( m_counter <= 0 );
	delete	m_pRealPtr;
}

template<class T>
long	CRefCountRep<T>::incrRefCount() {
	return	::InterlockedIncrement( &m_counter );
}

template<class T>
long	CRefCountRep<T>::decrRefCount() {
	return	::InterlockedDecrement( &m_counter );
}

template<class T>
T*	CRefCountRep<T>::getPointer() const {
	return	m_pRealPtr;
}

template<class T>
T*	CRefCountRep<T>::getRealPointer() const {
	return	m_pRealPtr;
}

template<class T>
bool	CRefCountRep<T>::isNull() const {
	return m_pRealPtr == NULL;
}
/////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////

template <class T>	class	CSyncAccess;

/////////////////////////////////////////////////////////////////////////////
//	Representation class just for thread synchronization
/////////////////////////////////////////////////////////////////////////////

template<class T>
class CSyncAccessRep {

//	Constructors and destructor
public:
	CSyncAccessRep( const T* ptr );
	~CSyncAccessRep();


typedef	CSyncAccess<T>	ACCESS;


//	Operations
public:
	long	incrRefCount();
	long	decrRefCount();
	
	ACCESS	getPointer() const;
	T*		getRealPointer() const;

	bool	isNull() const;

	void	acquireAccess();
	void	releaseAccess();


//	Implementation
protected:
	T*					m_pRealPtr;
	long				m_counter;
	CRITICAL_SECTION	m_CriticalSection;
};
/////////////////////////////////////////////////////////////////////////////

template<class T>
CSyncAccessRep<T>::CSyncAccessRep( const T* ptr )
 : m_pRealPtr( (T*)ptr ), m_counter( 0 ) {
	::InitializeCriticalSection( &m_CriticalSection );
}

template<class T>
CSyncAccessRep<T>::~CSyncAccessRep() {
	ASSERT( m_counter <= 0 );
	::DeleteCriticalSection( &m_CriticalSection );
}

template<class T>
long	CSyncAccessRep<T>::incrRefCount() {
	return	::InterlockedIncrement( &m_counter );
}

template<class T>
long	CSyncAccessRep<T>::decrRefCount() {
	return	::InterlockedDecrement( &m_counter );
}

template<class T>
CSyncAccessRep<T>::ACCESS	CSyncAccessRep<T>::getPointer() const {
	return	this;	//	Object of type ACCESS (CSyncAccess<T>) 
					//	will be automatically created on the stack
}

template<class T>
T*	CSyncAccessRep<T>::getRealPointer() const {
	return	m_pRealPtr;
}

template<class T>
bool	CSyncAccessRep<T>::isNull() const {
	return m_pRealPtr == NULL;
}

template<class T>
void	CSyncAccessRep<T>::acquireAccess() {
	::EnterCriticalSection( &m_CriticalSection );
}

template<class T>
void	CSyncAccessRep<T>::releaseAccess() {
	::LeaveCriticalSection( &m_CriticalSection );
}
/////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////
//	Representation class for Reference Counting AND Thread Synchronization
/////////////////////////////////////////////////////////////////////////////

template<class T>
class CSyncRefCountRep : public CSyncAccessRep<T> {

//	Constructors and destructor
public:
	CSyncRefCountRep( const T* ptr );
	~CSyncRefCountRep();
};
/////////////////////////////////////////////////////////////////////////////

template<class T>
CSyncRefCountRep<T>::CSyncRefCountRep( const T* ptr )
 : CSyncAccessRep<T>( ptr ) {
}

template<class T>
CSyncRefCountRep<T>::~CSyncRefCountRep() {
	ASSERT( m_counter <= 0 );
	delete	m_pRealPtr;		//	This is the only change needed to make in
							//	CSyncRefCountRep<T> class to collect the garbage and 
							//	in the same time to do Object Level Thread Synchronization
}
/////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////
//
//	The SyncAccess class. It is used as an intermediary to achieve
//	Object Level Thread Synchronization
//
/////////////////////////////////////////////////////////////////////////////

template <class T>
class	CSyncAccess {

//	Internally used data type
private:
	typedef		CSyncAccessRep<T>	REP;


//	Constructors and destructors
public:
	//	Copy constructor
	CSyncAccess( const CSyncAccess& that );
	CSyncAccess( const REP* rep );
	~CSyncAccess();


//	Operators
public:
	T*	operator -> ();


//	Implementation
private:
	REP*	m_rep;
	bool	m_acquired;
};
/////////////////////////////////////////////////////////////////////////////

template <class T>
CSyncAccess<T>::CSyncAccess( const REP* rep ) 
 : m_rep( (REP*)rep ), m_acquired( false ) {
}
template <class T>
CSyncAccess<T>::CSyncAccess( const  CSyncAccess<REP>& that )
 : m_rep( that.m_rep ), m_acquired( false ) {
}

template <class T>
CSyncAccess<T>::~CSyncAccess() {
	if( m_acquired ) {
		m_rep->releaseAccess();
	}
}

template <class T>
T*	CSyncAccess<T>::operator -> () {
//	This is checked by SmartPtr<T>::operator -> () too
	ASSERT( (m_rep != NULL) && (! m_rep->isNull()) );
	if( ! m_acquired ) {
		m_rep->acquireAccess();
		m_acquired = true;
	}
	return	m_rep->getRealPointer();
}
/////////////////////////////////////////////////////////////////////////////





/////////////////////////////////////////////////////////////////////////////
//	The SmartPtr class
/////////////////////////////////////////////////////////////////////////////

class	SmartPtrBase {
public:
	SmartPtrBase() : m_rep( NULL )
	{};

	void*	m_rep;
};
/////////////////////////////////////////////////////////////////////////////

template<class T, class REP, class ACCESS = T*>
class SmartPtr :	public SmartPtrBase {

//	Constructors and destructor
public:
	//	Default constructor and destructor
	SmartPtr();
	~SmartPtr();

	//	Copy constructor
	SmartPtr( const SmartPtr& ptr );

	//	Other constructors
	SmartPtr( const T* ptr );
	SmartPtr( const SmartPtrBase& ptr );


//	Assignment Operators
public:
	SmartPtr& operator = ( const SmartPtr& ptr );
	SmartPtr& operator = ( const T* ptr );
	SmartPtr& operator = ( const SmartPtrBase& ptr );


//	Operators
public:
	ACCESS	operator -> ();
	T&		operator * ();

	//	Casting operator
	operator T* ();

	//	Comparison Operators
	bool	operator == ( const SmartPtrBase& ptr );
	bool	operator == ( const T* ptr );
	bool	operator != ( const SmartPtrBase& ptr );
	bool	operator != ( const T* ptr );


//	Attributes
public:
	bool	IsNull() const;
	long	GetRefCount() const;
	REP*	GetRepPtr() const;


//	Helper methods
protected:
	void	CopyFrom( const SmartPtrBase& ptr );
	void	CopyFrom( const T* ptr );


//	Implementation
private:
	void	IncrRefCount();
	void	DecrRefCount();
};
/////////////////////////////////////////////////////////////////////////////
		
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::SmartPtr()
{
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::~SmartPtr()
{
	DecrRefCount();
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::SmartPtr( const SmartPtr& ptr )
{
	CopyFrom( ptr );
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::SmartPtr( const T* ptr )
{
	CopyFrom( ptr );
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::SmartPtr( const SmartPtrBase& ptr )
{
	CopyFrom( ptr );
}

template<class T, class REP, class ACCESS>
void	SmartPtr<T,REP,ACCESS>::CopyFrom( const SmartPtrBase& ptr )
{
	if( m_rep != ptr.m_rep ) {
		DecrRefCount();
		m_rep = ptr.m_rep;
		IncrRefCount();
	}
}
template<class T, class REP, class ACCESS>
void	SmartPtr<T,REP,ACCESS>::CopyFrom( const T* ptr )
{
	DecrRefCount();
	m_rep = (ptr != NULL) ? new REP( ptr ) : NULL;
	IncrRefCount();
}

template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>& SmartPtr<T,REP,ACCESS>::operator = ( const SmartPtr& ptr ) {
	CopyFrom( ptr );
	return	*this;
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>& SmartPtr<T,REP,ACCESS>::operator = ( const T* ptr ) {
	CopyFrom( ptr );
	return	*this;
}
template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>& SmartPtr<T,REP,ACCESS>::operator = ( const SmartPtrBase& ptr ) {
	CopyFrom( ptr );
	return	*this;
}

template<class T, class REP, class ACCESS>
ACCESS	SmartPtr<T,REP,ACCESS>::operator -> () {
	ASSERT( ! IsNull() );
	return GetRepPtr()->getPointer();
}

template<class T, class REP, class ACCESS>
T& SmartPtr<T,REP,ACCESS>::operator * () {
	ASSERT( ! IsNull() );
	return *(GetRepPtr()->getRealPointer());
}

template<class T, class REP, class ACCESS>
SmartPtr<T,REP,ACCESS>::operator T* () {
	return	( IsNull() ) ? NULL : GetRepPtr()->getRealPointer();
}

template<class T, class REP, class ACCESS>
bool	SmartPtr<T,REP,ACCESS>::operator == ( const SmartPtrBase& ptr ) {
	return	m_rep == ptr.m_rep;
}
template<class T, class REP, class ACCESS>
bool	SmartPtr<T,REP,ACCESS>::operator == ( const T* ptr ) {
	if( ! IsNull() ) {
		return	GetRepPtr()->getRealPointer() == ptr;
	}
	return	ptr == NULL;
}
template<class T, class REP, class ACCESS>
bool SmartPtr<T,REP,ACCESS>::operator != ( const SmartPtrBase& ptr ) {
	return	m_rep != ptr.m_rep;
}
template<class T, class REP, class ACCESS>
bool SmartPtr<T,REP,ACCESS>::operator != ( const T* ptr ) {
	return	! (operator ==( ptr ));
}

template<class T, class REP, class ACCESS>
bool	SmartPtr<T,REP,ACCESS>::IsNull() const {
	return	m_rep == NULL;
}

template<class T, class REP, class ACCESS>
long	SmartPtr<T,REP,ACCESS>::GetRefCount() const {
	ASSERT( ! IsNull() );
	return	GetRepPtr()->m_counter;
}

template<class T, class REP, class ACCESS>
REP*	SmartPtr<T,REP,ACCESS>::GetRepPtr() const {
	return	(REP*)m_rep;
}

template<class T, class REP, class ACCESS>
void	SmartPtr<T,REP,ACCESS>::IncrRefCount() {
	if( ! IsNull() ) {
		GetRepPtr()->incrRefCount();
	}
}
template<class T, class REP, class ACCESS>
void	SmartPtr<T,REP,ACCESS>::DecrRefCount() {
	if( ! IsNull() ) {
		if( GetRepPtr()->decrRefCount() <= 0 ) {
			REP*	rep = (REP*)m_rep;
			delete	rep;
		}
		m_rep = NULL;
	}
}
/////////////////////////////////////////////////////////////////////////////





/////////////////////////////////////////////////////////////////////////////
//
//	Helper classes for easier use of the SmartPtr class
//
/////////////////////////////////////////////////////////////////////////////

template<class T, class REP = CRefCountRep<T>, class ACCESS = T*>
class	RefCountPtr : public SmartPtr<T, REP, ACCESS> {

//	Constructors and destructor
public:
	//	Default constructor and destructor
	RefCountPtr()	{};
	~RefCountPtr()	{};

	//	Copy constructor
	RefCountPtr( const RefCountPtr& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

	RefCountPtr( const SmartPtrBase& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};
	RefCountPtr( const T* ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

//	Assignment Operators
public:
	RefCountPtr& operator = ( const RefCountPtr& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	RefCountPtr& operator = ( const T* ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	RefCountPtr& operator = ( const SmartPtrBase& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
};
/////////////////////////////////////////////////////////////////////////////

template<class T, class REP = CSyncAccessRep<T>, class ACCESS = CSyncAccess<T> >
class	SyncPtr : public SmartPtr<T, REP, ACCESS> {

//	Constructors and destructor
public:
	//	Default constructor and destructor
	SyncPtr()	{};
	~SyncPtr()	{};

	//	Copy constructor
	SyncPtr( const SyncPtr& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

	SyncPtr( const SmartPtrBase& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};
	SyncPtr( const T* ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

//	Assignment Operators
public:
	SyncPtr& operator = ( const SyncPtr& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	SyncPtr& operator = ( const T* ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	SyncPtr& operator = ( const SmartPtrBase& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
};
/////////////////////////////////////////////////////////////////////////////

template<class T, class REP = CSyncRefCountRep<T>, class ACCESS = CSyncAccess<T> >
class	SyncRefCountPtr : public SmartPtr<T, REP, ACCESS> {

//	Constructors and destructor
public:
	//	Default constructor and destructor
	SyncRefCountPtr()	{};
	~SyncRefCountPtr()	{};

	//	Copy constructor
	SyncRefCountPtr( const SyncRefCountPtr& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

	SyncRefCountPtr( const SmartPtrBase& ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};
	SyncRefCountPtr( const T* ptr )
		: SmartPtr<T,REP,ACCESS>( ptr )
	{};

//	Assignment Operators
public:
	SyncRefCountPtr& operator = ( const SyncRefCountPtr& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	SyncRefCountPtr& operator = ( const T* ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
	SyncRefCountPtr& operator = ( const SmartPtrBase& ptr ) {
		CopyFrom( ptr );
		return	*this;
	}
};
/////////////////////////////////////////////////////////////////////////////






/////////////////////////////////////////////////////////////////////////////
//
//	Examples on how to use these classes.
//
//	There are 3 cases:
//	1. Reference Counting
//	2. Object Level Thread Synchronization
//	3. Object Level Thread Synchronization AND Reference Counting
//
/*
Let's have a class CSomething


1.	Reference Counting on CSomething
------------------------------------

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	RefCountPtr<CSomething>	LPSOMETHING;

	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1 == NULL ) {
	....
	}

	p2->do();
	p1 = NULL;

}	//	Here the object pointed by p2 WILL BE destroyed automatically
/////////////////////////////////////////////////////////////////////////////



2. Object Level Thread Synchronization for objects of CSomething
---------------------------------------------------------

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	SyncPtr<CSomething>	LPSOMETHING;
	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1.IsNULL() ) {
	....
	}
	StartThread( p1 );

	p2->do();	//	Synchronized with the other thread
	p1 = NULL;

}	//	Here the object pointed by p2 will NOT be destroyed automatically

void	ThreadFunc( LPSOMETHING p ) {
	p->do();	//	Synchronized with the other thread
}//	Here the object pointed by p will NOT be destroyed automatically
/////////////////////////////////////////////////////////////////////////////



3. Object Level Thread Synchronization and Reference Counting
	for objects of CSomething
---------------------------------------------------------

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	SyncRefCountPtr<CSomething>	LPSOMETHING;
	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1.IsNULL() ) {
	....
	}
	StartThread( p1 );

	p2->do();	//	Synchronized with the other thread
	p1 = NULL;

}	//	Here the object pointed by p2 WILL BE destroyed automatically
	//	if p in ThreadFunc has already released the object

void	ThreadFunc( LPSOMETHING p ) {
	p->do();	//	Synchronized with the other thread
}//	Here the object pointed by p WILL BE destroyed automatically
 //	if p2 in TestFunc has already released the object
/////////////////////////////////////////////////////////////////////////////


WARNING:	Don't use a code like this

	CSomething*	ptr = new CSomething();	//	A real pointer
	LPSOMETHING	p1 = ptr;
	LPSOMETHING	p2 = ptr;	//	This will lead to 2 (two) 
							//	representation objects.
							//	So you will not have nor proper 
							//	grabage collection nor proper
							//	thread synchronization
	if( p1 != p2 ) {		//	The comparison will say these
		ASSERT( FALSE );	//	point to different objects
	}						//	even you think it is the same

/////////////////////////////////////////////////////////////////////////////

sizeof( CRITICAL_SECTION ) 24
sizeof( CRefCountRep ) 8
sizeof( CSyncAccessRep ) 32
sizeof( CSyncRefCountRep ) 32
sizeof( CSyncAccess ) 8
sizeof( SmartPtr ) 4
sizeof( CSomething* ) 4
sizeof( int ) 4

*/

#endif		//	_SMARTPTR_H_

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us