-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.concept_model_idiom.h
81 lines (71 loc) · 1.62 KB
/
3.concept_model_idiom.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Tianyi Zhang on 10/24/20.
//
#ifndef DESIGN_PATTERN_3_CONCEPT_MODEL_IDIOM_H
#define DESIGN_PATTERN_3_CONCEPT_MODEL_IDIOM_H
#include <iostream>
#include <memory>
#include <vector>
//refer to https://gracicot.github.io/conceptmodel/2017/09/13/concept-model-part1.html
class PrintTask{
public:
void process(){
std::cout<<"Print task"<<std::endl;
}
};
class GrabTask{
public:
void process(){
std::cout<<"Grab task"<<std::endl;
}
};
//actually just an adapter to convert PrintTask and GrabTask to Task
class Task{
public:
template<typename T>
Task(T&& task){
self = std::unique_ptr<TaskConcept>(new TaskModel<T>(std::forward<T>(task)));
}
void process(){
this->self->process();
}
private:
class TaskConcept{
public:
virtual void process() = 0;
virtual ~TaskConcept(){}
};
template<typename T>
class TaskModel: public TaskConcept{
public:
TaskModel(T&& task) : task(task)
{}
void process() override{
task.process();
}
T task;
};
std::unique_ptr<TaskConcept> self;
};
class TaskQueue{
public:
template<typename T>
void push(T&& task){
this->taskQueue.push_back(task);
}
//following is ambiguous
/*
void push(Task&& task){
this->taskQueue.push_back(std::move(task));
}
void push(Task task){
this->taskQueue.push_back(std::move(task));
}*/
void run(){
for(auto& task:taskQueue)
task.process();
}
private:
std::vector<Task> taskQueue;
};
#endif //DESIGN_PATTERN_3_CONCEPT_MODEL_IDIOM_H