Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
CreationPolicies.h
Go to the documentation of this file.
1 #ifndef Protium_CreationPolicies_h_
2 #define Protium_CreationPolicies_h_
3 
4 #include <malloc.h>
5 
6 namespace Protium{
7  namespace Singleton{
8 
11  template <class T>
12  struct CreateNew{
14  static T* Create() { return new T; }
15 
17  static void Destroy(T* p) { delete p; }
18  };
19 
22  template< template<class> class Alloc>
23  struct CreateAlloc{
26  template <class T>
27  struct Allocator{
29  static Alloc<T> allocator;
31  static T* Create(){return new ( allocator.allocate(1) ) T;}
32 
34  static void Destroy(T* p){
35  p->~T();
36  allocator.deallocate(p,1);
37  }
38  };
39  };
40 
42  template <class T>
43  struct CreateMalloc{
44  static T* Create(){
45  void* p = malloc(sizeof(T));
46  if (!p) return NULL;
47  return new(p) T;
48  }
49 
50  static void Destroy(T* p){
51  p->~T();
52  free(p);
53  }
54  };
55 
57  template <class T>
58  struct CreateStatic{
59 
61  union MaxAlign
62  {
63  char t_[sizeof(T)];
64  short int shortInt_;
65  int int_;
66  long int longInt_;
67  float float_;
68  double double_;
69  long double longDouble_;
70  struct Test;
71  int Test::* pMember_;
72  int (Test::*pMemberFn_)(int);
73  };
74 
75  static T* Create(){
76  static MaxAlign staticMemory_;
77  return new(&staticMemory_) T;
78  }
79 
80  static void Destroy(T* p){
81  p->~T();
82  }
83  };
84  }
85 }
86 
87 
88 #endif //File Guardian
static Alloc< T > allocator
The allocator for all of these type classes.
static T * Create()
Creates instance.
Helper struct to create static instances of classes.
Used to determine max type size.
static void Destroy(T *p)
Destroys instance.
Helper struct to create instances with malloc, and deallocate with free.
static T * Create()
Creates instance.
static void Destroy(T *p)
Destroys instance.