root/trunk/src/framework/Policies/CreationPolicy.h @ 4

Revision 2, 2.8 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#ifndef MANGOS_CREATIONPOLICY_H
20#define MANGOS_CREATIONPOLICY_H
21
22#include <stdlib.h>
23#include "Platform/Define.h"
24
25namespace MaNGOS
26{
27    /**
28     * OperatorNew policy creates an object on the heap using new.
29     */
30    template <class T>
31        class MANGOS_DLL_DECL OperatorNew
32    {
33        public:
34            static T* Create(void) { return (new T); }
35            static void Destroy(T *obj) { delete obj; }
36    };
37
38    /**
39     * LocalStaticCreation policy creates an object on the stack
40     * the first time call Create.
41     */
42    template <class T>
43        class MANGOS_DLL_DECL LocalStaticCreation
44    {
45        union MaxAlign
46        {
47            char t_[sizeof(T)];
48            short int shortInt_;
49            int int_;
50            long int longInt_;
51            float float_;
52            double double_;
53            long double longDouble_;
54            struct Test;
55            int Test::* pMember_;
56            int (Test::*pMemberFn_)(int);
57        };
58        public:
59            static T* Create(void)
60            {
61                static MaxAlign si_localStatic;
62                return new(&si_localStatic) T;
63            }
64
65            static void Destroy(T *obj) { obj->~T(); }
66    };
67
68    /**
69     * CreateUsingMalloc by pass the memory manger.
70     */
71    template<class T>
72        class MANGOS_DLL_DECL CreateUsingMalloc
73    {
74        public:
75            static T* Create()
76            {
77                void* p = ::malloc(sizeof(T));
78                if (!p) return 0;
79                return new(p) T;
80            }
81
82            static void Destroy(T* p)
83            {
84                p->~T();
85                ::free(p);
86            }
87    };
88
89    /**
90     * CreateOnCallBack creates the object base on the call back.
91     */
92    template<class T, class CALL_BACK>
93        class MANGOS_DLL_DECL CreateOnCallBack
94    {
95        public:
96            static T* Create()
97            {
98                return CALL_BACK::createCallBack();
99            }
100
101            static void Destroy(T *p)
102            {
103                CALL_BACK::destroyCallBack(p);
104            }
105    };
106}
107#endif
Note: See TracBrowser for help on using the browser.