1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_REFERENCEIMPL_H |
---|
22 | #define TRINITY_REFERENCEIMPL_H |
---|
23 | |
---|
24 | #include "Reference.h" |
---|
25 | |
---|
26 | template |
---|
27 | < |
---|
28 | typename T, |
---|
29 | class THREADING_MODEL |
---|
30 | > |
---|
31 | Referencer<T, THREADING_MODEL>::Referencer(T *ref) |
---|
32 | : i_holder(NULL) |
---|
33 | { |
---|
34 | if( ref != NULL ) |
---|
35 | { |
---|
36 | i_holder = new ReferenceeHolder(ref); |
---|
37 | ++i_holder->i_referenceCount; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | template |
---|
42 | < |
---|
43 | typename T, |
---|
44 | class THREADING_MODEL |
---|
45 | > |
---|
46 | Referencer<T, THREADING_MODEL>::~Referencer() |
---|
47 | { |
---|
48 | if( i_holder != NULL ) |
---|
49 | deReference(i_holder); |
---|
50 | i_holder = NULL; |
---|
51 | } |
---|
52 | |
---|
53 | template |
---|
54 | < |
---|
55 | typename T, |
---|
56 | class THREADING_MODEL |
---|
57 | > |
---|
58 | Referencer<T, THREADING_MODEL>& |
---|
59 | Referencer<T, THREADING_MODEL>::operator=(const Referencer<T, THREADING_MODEL> &obj) |
---|
60 | { |
---|
61 | if( i_holder != NULL ) |
---|
62 | deReference(i_holder); |
---|
63 | if( obj.i_holder != NULL ) |
---|
64 | addReference(obj.i_holder); |
---|
65 | i_holder = obj.i_holder; |
---|
66 | return *this; |
---|
67 | } |
---|
68 | |
---|
69 | template |
---|
70 | < |
---|
71 | typename T, |
---|
72 | class THREADING_MODEL |
---|
73 | > |
---|
74 | Referencer<T, THREADING_MODEL>& |
---|
75 | Referencer<T, THREADING_MODEL>::operator=(T *ref) |
---|
76 | { |
---|
77 | if( i_holder != NULL ) |
---|
78 | deReference(i_holder); |
---|
79 | i_holder = NULL; |
---|
80 | if( ref != NULL ) |
---|
81 | { |
---|
82 | i_holder = new ReferenceeHolder(ref); |
---|
83 | ++i_holder->i_referenceCount; |
---|
84 | } |
---|
85 | |
---|
86 | return *this; |
---|
87 | } |
---|
88 | |
---|
89 | template |
---|
90 | < |
---|
91 | typename T, |
---|
92 | class THREADING_MODEL |
---|
93 | > |
---|
94 | void |
---|
95 | Referencer<T, THREADING_MODEL>::deReference(ReferenceHolder<T, THREADING_MODEL> *holder) |
---|
96 | { |
---|
97 | assert( holder != NULL && holder->i_referenceCount > 0); |
---|
98 | bool delete_object = false; |
---|
99 | |
---|
100 | { |
---|
101 | // The guard is within the scope due to the guard |
---|
102 | // must release earlier than expected. |
---|
103 | Lock guard(*holder); |
---|
104 | Guard(&guard); |
---|
105 | |
---|
106 | --holder->i_referenceCount; |
---|
107 | if( holder->i_referenceCount == 0 ) |
---|
108 | delete_object = true; |
---|
109 | } |
---|
110 | |
---|
111 | if( delete_object ) |
---|
112 | { |
---|
113 | delete holder->i_referencee; |
---|
114 | delete holder; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | template |
---|
119 | < |
---|
120 | typename T, |
---|
121 | class THREADING_MODEL |
---|
122 | > |
---|
123 | void |
---|
124 | Referencer<T, THREADING_MODEL>::addReference(ReferenceHolder<T, THREADING_MODEL> *holder) |
---|
125 | { |
---|
126 | assert( i_holder != NULL ); |
---|
127 | Lock guard(*holder); |
---|
128 | Guard(&guard); |
---|
129 | |
---|
130 | ++holder->i_referenceCount; |
---|
131 | } |
---|
132 | #endif |
---|