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_REFERENCE_H |
---|
22 | #define TRINITY_REFERENCE_H |
---|
23 | |
---|
24 | /** |
---|
25 | * Referencer<T> |
---|
26 | * Referencer is an object that holds a reference holder that hold a reference |
---|
27 | * counted object. When an object's reference count drop to zero, it removes |
---|
28 | * the object. This is a non intrusive mechanism and any object at any point |
---|
29 | * in time can be referenced. When and object is reference counted, do not |
---|
30 | * pass the object directly to other methods but rather, pass its |
---|
31 | * reference around. Objects can be reference counted in both single threaded |
---|
32 | * model and multi-threaded model |
---|
33 | */ |
---|
34 | |
---|
35 | #include <stdexcept> |
---|
36 | #include "Platform/Define.h" |
---|
37 | #include "Policies/ThreadingModel.h" |
---|
38 | #include "ReferenceHolder.h" |
---|
39 | |
---|
40 | template |
---|
41 | < |
---|
42 | typename T, |
---|
43 | class THREADING_MODEL = Trinity::SingleThreaded<T> |
---|
44 | > |
---|
45 | class TRINITY_DLL_DECL Referencer |
---|
46 | { |
---|
47 | typedef typename THREADING_MODEL::Lock Lock; |
---|
48 | typedef ReferenceHolder<T, THREADING_MODEL> ReferenceeHolder; |
---|
49 | public: |
---|
50 | |
---|
51 | /// Constructs a referencer. |
---|
52 | Referencer(T *ref = NULL); |
---|
53 | |
---|
54 | /// Copy constructor |
---|
55 | Referencer(const Referencer &obj) : i_holder(NULL) { *this = obj; } |
---|
56 | |
---|
57 | /// Destructor |
---|
58 | ~Referencer(); |
---|
59 | |
---|
60 | /// Referencee accessor |
---|
61 | T* referencee(void) { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
62 | const T* referencee(void) const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
63 | |
---|
64 | //T& referencee(void){ return _referencee(); } |
---|
65 | //const T& referencee(void) const { return const_cast<Referencer *>(this)->_referencee(); } |
---|
66 | operator T&(void) { return _referencee(); } |
---|
67 | operator const T&(void) const { return *const_cast<Referencer *>(this)->_referencee(); } |
---|
68 | |
---|
69 | /// cast operators |
---|
70 | T* operator*() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
71 | T const * operator*() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
72 | |
---|
73 | /// overload operators |
---|
74 | T* operator->() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
75 | const T * operator->() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } |
---|
76 | |
---|
77 | /// operator = |
---|
78 | Referencer& operator=(const Referencer &obj); |
---|
79 | Referencer& operator=(T *); |
---|
80 | |
---|
81 | /// returns true if i_referencee is null |
---|
82 | bool isNull(void) const { return i_holder == NULL; } |
---|
83 | |
---|
84 | private: |
---|
85 | |
---|
86 | T& _referencee(void) |
---|
87 | { |
---|
88 | if( i_holder == NULL ) |
---|
89 | throw std::runtime_error("Invalid access to null pointer"); |
---|
90 | return *i_holder->i_referencee; |
---|
91 | } |
---|
92 | |
---|
93 | void deReference(ReferenceeHolder *); |
---|
94 | void addReference(ReferenceeHolder *); |
---|
95 | |
---|
96 | // private data |
---|
97 | ReferenceeHolder *i_holder; |
---|
98 | }; |
---|
99 | #endif |
---|