[9122] Updated copyright notice for new year.
[getmangos.git] / src / framework / Policies / CreationPolicy.h
blobd4dc410ca60052e5e1f9236fdad70327f6d64a6c
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #ifndef MANGOS_CREATIONPOLICY_H
20 #define MANGOS_CREATIONPOLICY_H
22 #include <stdlib.h>
23 #include "Platform/Define.h"
25 namespace MaNGOS
27 /**
28 * OperatorNew policy creates an object on the heap using new.
30 template <class T>
31 class MANGOS_DLL_DECL OperatorNew
33 public:
34 static T* Create(void) { return (new T); }
35 static void Destroy(T *obj) { delete obj; }
38 /**
39 * LocalStaticCreation policy creates an object on the stack
40 * the first time call Create.
42 template <class T>
43 class MANGOS_DLL_DECL LocalStaticCreation
45 union MaxAlign
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);
58 public:
59 static T* Create(void)
61 static MaxAlign si_localStatic;
62 return new(&si_localStatic) T;
65 static void Destroy(T *obj) { obj->~T(); }
68 /**
69 * CreateUsingMalloc by pass the memory manger.
71 template<class T>
72 class MANGOS_DLL_DECL CreateUsingMalloc
74 public:
75 static T* Create()
77 void* p = ::malloc(sizeof(T));
78 if (!p) return 0;
79 return new(p) T;
82 static void Destroy(T* p)
84 p->~T();
85 ::free(p);
89 /**
90 * CreateOnCallBack creates the object base on the call back.
92 template<class T, class CALL_BACK>
93 class MANGOS_DLL_DECL CreateOnCallBack
95 public:
96 static T* Create()
98 return CALL_BACK::createCallBack();
101 static void Destroy(T *p)
103 CALL_BACK::destroyCallBack(p);
107 #endif