1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef XPLRA_MULTITASKREQUEST_H
|
---|
30 | #define XPLRA_MULTITASKREQUEST_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "Request.h"
|
---|
34 |
|
---|
35 | #include <vector>
|
---|
36 |
|
---|
37 | #include <cstdlib>
|
---|
38 |
|
---|
39 | //------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | namespace xplra {
|
---|
42 |
|
---|
43 | //------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | class Task;
|
---|
46 |
|
---|
47 | //------------------------------------------------------------------------------
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * A request containing several tasks which it performs one-by-one
|
---|
51 | * when executing the request. It owns the tasks, which get deleted
|
---|
52 | * when the request object is deleted.
|
---|
53 | */
|
---|
54 | class MultiTaskRequest : public Request
|
---|
55 | {
|
---|
56 | private:
|
---|
57 | /**
|
---|
58 | * Type for the vector of the tasks.
|
---|
59 | */
|
---|
60 | typedef std::vector<Task*> tasks_t;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * The tasks.
|
---|
64 | */
|
---|
65 | tasks_t tasks;
|
---|
66 |
|
---|
67 | public:
|
---|
68 | /**
|
---|
69 | * Destroy the request by deleting the tasks.
|
---|
70 | */
|
---|
71 | virtual ~MultiTaskRequest();
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Add a task to the request.
|
---|
75 | */
|
---|
76 | void addTask(Task* task);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Get the number of tasks.
|
---|
80 | */
|
---|
81 | size_t getNumTasks() const;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get the task at the given index.
|
---|
85 | */
|
---|
86 | Task* getTask(size_t index) const;
|
---|
87 |
|
---|
88 | protected:
|
---|
89 | /**
|
---|
90 | * Perform the tasks one-by-one.
|
---|
91 | */
|
---|
92 | virtual void execute();
|
---|
93 | };
|
---|
94 |
|
---|
95 | //------------------------------------------------------------------------------
|
---|
96 | //------------------------------------------------------------------------------
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * A templated version of MultiTaskRequest, which accepts and returns
|
---|
100 | * tasks of a given type only.
|
---|
101 | */
|
---|
102 | template <class TaskClass>
|
---|
103 | class MultiTaskRequestTemplate : public MultiTaskRequest
|
---|
104 | {
|
---|
105 | public:
|
---|
106 | /**
|
---|
107 | * Add the given task.
|
---|
108 | */
|
---|
109 | void addTask(TaskClass* task);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Get the task at the given index.
|
---|
113 | */
|
---|
114 | TaskClass* getTask(size_t index) const;
|
---|
115 | };
|
---|
116 |
|
---|
117 | //------------------------------------------------------------------------------
|
---|
118 | // Inline definitions
|
---|
119 | //------------------------------------------------------------------------------
|
---|
120 |
|
---|
121 | inline void MultiTaskRequest::addTask(Task* task)
|
---|
122 | {
|
---|
123 | tasks.push_back(task);
|
---|
124 | }
|
---|
125 |
|
---|
126 | //------------------------------------------------------------------------------
|
---|
127 |
|
---|
128 | inline size_t MultiTaskRequest::getNumTasks() const
|
---|
129 | {
|
---|
130 | return tasks.size();
|
---|
131 | }
|
---|
132 |
|
---|
133 | //------------------------------------------------------------------------------
|
---|
134 |
|
---|
135 | inline Task* MultiTaskRequest::getTask(size_t index) const
|
---|
136 | {
|
---|
137 | return tasks[index];
|
---|
138 | }
|
---|
139 |
|
---|
140 | //------------------------------------------------------------------------------
|
---|
141 | // Template definitions
|
---|
142 | //------------------------------------------------------------------------------
|
---|
143 |
|
---|
144 | template <class TaskClass>
|
---|
145 | inline void MultiTaskRequestTemplate<TaskClass>::addTask(TaskClass* task)
|
---|
146 | {
|
---|
147 | MultiTaskRequest::addTask(task);
|
---|
148 | }
|
---|
149 |
|
---|
150 | //------------------------------------------------------------------------------
|
---|
151 |
|
---|
152 | template <class TaskClass>
|
---|
153 | inline TaskClass*
|
---|
154 | MultiTaskRequestTemplate<TaskClass>::getTask(size_t index) const
|
---|
155 | {
|
---|
156 | return static_cast<TaskClass*>(MultiTaskRequest::getTask(index));
|
---|
157 | }
|
---|
158 |
|
---|
159 | //------------------------------------------------------------------------------
|
---|
160 |
|
---|
161 | } /* namespace xplra */
|
---|
162 |
|
---|
163 | //------------------------------------------------------------------------------
|
---|
164 | #endif // XPLRA_MULTITASKREQUEST_H
|
---|
165 |
|
---|
166 | // Local Variables:
|
---|
167 | // mode: C++
|
---|
168 | // c-basic-offset: 4
|
---|
169 | // indent-tabs-mode: nil
|
---|
170 | // End:
|
---|