source: xplra/src/plugin/src/xplra/MultiTaskRequest.h@ 42:719f590e4878

Last change on this file since 42:719f590e4878 was 13:42fd631176b7, checked in by István Váradi <ivaradi@…>, 11 years ago

Reorganized code to be able to handle the client library as a normal libtool library

File size: 4.9 KB
Line 
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
41namespace xplra {
42
43//------------------------------------------------------------------------------
44
45class 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 */
54class MultiTaskRequest : public Request
55{
56private:
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
67public:
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
88protected:
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 */
102template <class TaskClass>
103class MultiTaskRequestTemplate : public MultiTaskRequest
104{
105public:
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
121inline void MultiTaskRequest::addTask(Task* task)
122{
123 tasks.push_back(task);
124}
125
126//------------------------------------------------------------------------------
127
128inline size_t MultiTaskRequest::getNumTasks() const
129{
130 return tasks.size();
131}
132
133//------------------------------------------------------------------------------
134
135inline Task* MultiTaskRequest::getTask(size_t index) const
136{
137 return tasks[index];
138}
139
140//------------------------------------------------------------------------------
141// Template definitions
142//------------------------------------------------------------------------------
143
144template <class TaskClass>
145inline void MultiTaskRequestTemplate<TaskClass>::addTask(TaskClass* task)
146{
147 MultiTaskRequest::addTask(task);
148}
149
150//------------------------------------------------------------------------------
151
152template <class TaskClass>
153inline TaskClass*
154MultiTaskRequestTemplate<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:
Note: See TracBrowser for help on using the repository browser.