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_CREATIONPOLICY_H |
---|
22 | #define TRINITY_CREATIONPOLICY_H |
---|
23 | |
---|
24 | #include <stdlib.h> |
---|
25 | #include "Platform/Define.h" |
---|
26 | |
---|
27 | namespace Trinity |
---|
28 | { |
---|
29 | /** |
---|
30 | * OperatorNew policy creates an object on the heap using new. |
---|
31 | */ |
---|
32 | template <class T> |
---|
33 | class TRINITY_DLL_DECL OperatorNew |
---|
34 | { |
---|
35 | public: |
---|
36 | static T* Create(void) { return (new T); } |
---|
37 | static void Destroy(T *obj) { delete obj; } |
---|
38 | }; |
---|
39 | |
---|
40 | /** |
---|
41 | * LocalStaticCreation policy creates an object on the stack |
---|
42 | * the first time call Create. |
---|
43 | */ |
---|
44 | template <class T> |
---|
45 | class TRINITY_DLL_DECL LocalStaticCreation |
---|
46 | { |
---|
47 | union MaxAlign |
---|
48 | { |
---|
49 | char t_[sizeof(T)]; |
---|
50 | short int shortInt_; |
---|
51 | int int_; |
---|
52 | long int longInt_; |
---|
53 | float float_; |
---|
54 | double double_; |
---|
55 | long double longDouble_; |
---|
56 | struct Test; |
---|
57 | int Test::* pMember_; |
---|
58 | int (Test::*pMemberFn_)(int); |
---|
59 | }; |
---|
60 | public: |
---|
61 | static T* Create(void) |
---|
62 | { |
---|
63 | static MaxAlign si_localStatic; |
---|
64 | return new(&si_localStatic) T; |
---|
65 | } |
---|
66 | |
---|
67 | static void Destroy(T *obj) { obj->~T(); } |
---|
68 | }; |
---|
69 | |
---|
70 | /** |
---|
71 | * CreateUsingMalloc by pass the memory manger. |
---|
72 | */ |
---|
73 | template<class T> |
---|
74 | class TRINITY_DLL_DECL CreateUsingMalloc |
---|
75 | { |
---|
76 | public: |
---|
77 | static T* Create() |
---|
78 | { |
---|
79 | void* p = ::malloc(sizeof(T)); |
---|
80 | if (!p) return 0; |
---|
81 | return new(p) T; |
---|
82 | } |
---|
83 | |
---|
84 | static void Destroy(T* p) |
---|
85 | { |
---|
86 | p->~T(); |
---|
87 | ::free(p); |
---|
88 | } |
---|
89 | }; |
---|
90 | |
---|
91 | /** |
---|
92 | * CreateOnCallBack creates the object base on the call back. |
---|
93 | */ |
---|
94 | template<class T, class CALL_BACK> |
---|
95 | class TRINITY_DLL_DECL CreateOnCallBack |
---|
96 | { |
---|
97 | public: |
---|
98 | static T* Create() |
---|
99 | { |
---|
100 | return CALL_BACK::createCallBack(); |
---|
101 | } |
---|
102 | |
---|
103 | static void Destroy(T *p) |
---|
104 | { |
---|
105 | CALL_BACK::destroyCallBack(p); |
---|
106 | } |
---|
107 | }; |
---|
108 | } |
---|
109 | #endif |
---|