-
Notifications
You must be signed in to change notification settings - Fork 0
/
Policy Based Design.h
64 lines (49 loc) · 1.08 KB
/
Policy Based Design.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <iostream>
namespace policy_bs_design {
class Widget {};
template<typename T>
class OPNewCreator {
public:
static T* create() {
std::cout << "OPNewCreate::Create" << "\n";
return new T;
}
};
template<typename T>
class MallocCreator {
public:
static T* create() {
std::cout << "MallocCreator::Create" << "\n";
void* rawbuffer = std::malloc(sizeof(T));
if (!rawbuffer) {
return nullptr;
}
return new (rawbuffer) T;
}
};
template<typename T>
class PrototypeBasedCreator {
static T* ptr_prototype_obj = nullptr;
public:
PrototypeBasedCreator(T* ptr_proto = nullptr) :ptr_prototype_obj(ptr_proto) {}
static T* getPrototype() {
return ptr_prototype_obj;
}
static void setPrototype(T* ptr_proto) {
ptr_prototype_obj = ptr_proto;
}
static T* create() {
std::cout << "PrototypeBasedCreator::Create" << "\n";
ptr_prototype_obj->clone();
}
};
template< class Policy>
class WidgetManager :Policy {
public:
void doSomething() {
std::cout << "WidgetManager::doSomething()\n";
this->create();
}
};
}