1 /* 2 Filter Base Class 3 4 Copyright (C) 2018 Robert Lipe, robertlipe+source@gpsbabel.org 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 #ifndef FILTER_H_INCLUDED_ 21 #define FILTER_H_INCLUDED_ 22 23 #include "defs.h" 24 25 // Filter have access to the global_waypoint_list, which formats really 26 // shouldn't have. 27 extern WaypointList* global_waypoint_list; 28 29 class Filter 30 { 31 public: 32 Filter() = default; 33 // Provide virtual public destructor to avoid undefined behavior when 34 // an object of derived class type is deleted through a pointer to 35 // its base class type. 36 // https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP52-CPP.+Do+not+delete+a+polymorphic+object+without+a+virtual+destructor 37 virtual ~Filter() = default; 38 // And that requires us to explicitly default or delete the move and copy operations. 39 // To prevent slicing we delete them. 40 // https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c21-if-you-define-or-delete-any-default-operation-define-or-delete-them-all. 41 Filter(const Filter&) = delete; 42 Filter& operator=(const Filter&) = delete; 43 Filter(Filter&&) = delete; 44 Filter& operator=(Filter&&) = delete; 45 46 virtual QVector<arglist_t>* get_args() = 0; 47 init()48 virtual void init() 49 { 50 /* Called before filter processing */ 51 52 /* optional. If not needed, delete and replace entry in vecs with NULL */ 53 54 /* This may be used to parse filter options, allocate memory, and do other 55 * housekeeping that should be done before filtering */ 56 } 57 58 virtual void process() = 0; /* this procedure must be present in vecs */ 59 //{ 60 // Here is how you register callbacks for all waypoints, routes, tracks. 61 // waypt_disp_all(waypt) 62 // route_disp_all(head, tail, rtept); 63 // track_disp_all(head, tail, trkpt); 64 //} 65 deinit()66 virtual void deinit() 67 { 68 /* called after filter processing */ 69 70 /* optional. If not needed, delete and replace entry in vecs with NULL */ 71 72 /* This should be used to clean up any memory allocations that are no longer 73 * needed after the filter terminates. */ 74 } 75 exit()76 virtual void exit() 77 { 78 /* called on program exit */ 79 80 /* optional. If not needed, delete and replace entry in vecs with NULL */ 81 82 /* You should not need this for simple filters, but it may be used to 83 * clean up memory allocations that must persist from one invocation of 84 * your filter to the next (for example, the stack in the stack filter.) 85 * Note that this member will be called even if your filter has not been 86 * used, so it *cannot* assume that _init or _process has been called 87 * previously. */ 88 } 89 90 protected: 91 template <class MyFilter> 92 class RteHdFunctor 93 { 94 public: 95 using RteHdCb = void (MyFilter::*)(const route_head*); RteHdFunctor(MyFilter * obj,RteHdCb cb)96 RteHdFunctor(MyFilter* obj, RteHdCb cb) : that(obj), _cb(cb) {} operator()97 void operator()(const route_head* rh) 98 { 99 ((that)->*(_cb))(rh); 100 } 101 102 private: 103 MyFilter* that; 104 RteHdCb _cb; 105 }; 106 107 template <class MyFilter> 108 class WayptFunctor 109 { 110 public: 111 using WayptCb = void (MyFilter::*)(const Waypoint*); WayptFunctor(MyFilter * obj,WayptCb cb)112 WayptFunctor(MyFilter* obj, WayptCb cb) : that(obj), _cb(cb) {} operator()113 void operator()(const Waypoint* wpt) 114 { 115 ((that)->*(_cb))(wpt); 116 } 117 118 private: 119 MyFilter* that; 120 WayptCb _cb; 121 }; 122 123 }; 124 #endif // FILTER_H_INCLUDED_ 125