DeviceDescriptor: eliminate obsolete NMEAOut kludge
[xcsoar.git] / src / Task / ProtectedTaskManager.cpp
blob88578672740106987fe5d2dd9060b1582d4fd5ae
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "ProtectedTaskManager.hpp"
25 #include "Serialiser.hpp"
26 #include "Deserialiser.hpp"
27 #include "XML/DataNodeXML.hpp"
28 #include "Task/TaskFile.hpp"
29 #include "LocalPath.hpp"
30 #include "Task/RoutePlannerGlue.hpp"
31 #include "Task/Ordered/Points/AATPoint.hpp"
32 #include "Engine/Task/Ordered/OrderedTask.hpp"
33 #include "Engine/Route/ReachResult.hpp"
35 #include <windef.h> // for MAX_PATH
37 ProtectedTaskManager::ProtectedTaskManager(TaskManager &_task_manager,
38 const TaskBehaviour &tb)
39 :Guard<TaskManager>(_task_manager),
40 task_behaviour(tb)
44 ProtectedTaskManager::~ProtectedTaskManager() {
45 UnprotectedLease lease(*this);
46 lease->SetIntersectionTest(NULL); // de-register
49 void
50 ProtectedTaskManager::SetGlidePolar(const GlidePolar &glide_polar)
52 ExclusiveLease lease(*this);
53 lease->SetGlidePolar(glide_polar);
56 const OrderedTaskBehaviour
57 ProtectedTaskManager::GetOrderedTaskBehaviour() const
59 Lease lease(*this);
60 return lease->GetOrderedTask().GetOrderedTaskBehaviour();
63 const Waypoint*
64 ProtectedTaskManager::GetActiveWaypoint() const
66 Lease lease(*this);
67 const TaskWaypoint *tp = lease->GetActiveTaskPoint();
68 if (tp)
69 return &tp->GetWaypoint();
71 return NULL;
74 bool
75 ProtectedTaskManager::TargetLock(const unsigned index, bool do_lock)
77 ExclusiveLease lease(*this);
78 return lease->TargetLock(index, do_lock);
81 void
82 ProtectedTaskManager::IncrementActiveTaskPoint(int offset)
84 ExclusiveLease lease(*this);
85 lease->IncrementActiveTaskPoint(offset);
88 void
89 ProtectedTaskManager::IncrementActiveTaskPointArm(int offset)
91 ExclusiveLease lease(*this);
92 TaskAdvance &advance = lease->SetTaskAdvance();
94 switch (advance.GetState()) {
95 case TaskAdvance::MANUAL:
96 case TaskAdvance::AUTO:
97 lease->IncrementActiveTaskPoint(offset);
98 break;
99 case TaskAdvance::START_DISARMED:
100 case TaskAdvance::TURN_DISARMED:
101 if (offset>0) {
102 advance.SetArmed(true);
103 } else {
104 lease->IncrementActiveTaskPoint(offset);
106 break;
107 case TaskAdvance::START_ARMED:
108 case TaskAdvance::TURN_ARMED:
109 if (offset>0) {
110 lease->IncrementActiveTaskPoint(offset);
111 } else {
112 advance.SetArmed(false);
114 break;
118 bool
119 ProtectedTaskManager::DoGoto(const Waypoint &wp)
121 ExclusiveLease lease(*this);
122 return lease->DoGoto(wp);
125 OrderedTask*
126 ProtectedTaskManager::TaskClone() const
128 Lease lease(*this);
129 return lease->Clone(task_behaviour);
132 bool
133 ProtectedTaskManager::TaskCommit(const OrderedTask& that)
135 ExclusiveLease lease(*this);
136 return lease->Commit(that);
139 bool
140 ProtectedTaskManager::TaskSave(const TCHAR* path, const OrderedTask& task)
142 DataNodeXML root(DataNodeXML::CreateRoot(_T("Task")));
143 Serialiser tser(root);
144 tser.Serialise(task);
146 return root.Save(path);
149 bool
150 ProtectedTaskManager::TaskSave(const TCHAR* path)
152 OrderedTask* task = TaskClone();
153 bool retval = TaskSave(path, *task);
154 delete task;
155 return retval;
158 const TCHAR ProtectedTaskManager::default_task_path[] = _T("Default.tsk");
161 OrderedTask*
162 ProtectedTaskManager::TaskCreateDefault(const Waypoints *waypoints,
163 TaskFactoryType factoryfail)
165 TCHAR path[MAX_PATH];
166 LocalPath(path, default_task_path);
167 OrderedTask *task = TaskFile::GetTask(path, task_behaviour, waypoints, 0);
168 if (!task) {
169 task = new OrderedTask(task_behaviour);
170 assert(task);
171 task->SetFactory(factoryfail);
173 return task;
176 bool
177 ProtectedTaskManager::TaskSaveDefault()
179 TCHAR path[MAX_PATH];
180 LocalPath(path, default_task_path);
181 return TaskSave(path);
184 void
185 ProtectedTaskManager::Reset()
187 ExclusiveLease lease(*this);
188 lease->Reset();
191 void
192 ProtectedTaskManager::SetRoutePlanner(const RoutePlannerGlue *_route) {
193 intersection_test.SetRoute(_route);
195 ExclusiveLease lease(*this);
196 lease->SetIntersectionTest(&intersection_test);
199 bool
200 ReachIntersectionTest::Intersects(const AGeoPoint& destination)
202 if (!route)
203 return false;
205 ReachResult result;
206 if (!route->FindPositiveArrival(destination, result))
207 return false;
209 // we use find_positive_arrival here instead of is_inside, because may use
210 // arrival height for sorting later
211 return result.terrain_valid == ReachResult::Validity::UNREACHABLE ||
212 (result.terrain_valid == ReachResult::Validity::VALID &&
213 result.terrain < destination.altitude);