|
| 1 | +//*************************************************************************** |
| 2 | +// Copyright 2007-2026 Universidade do Porto - Faculdade de Engenharia * |
| 3 | +// Laboratório de Sistemas e Tecnologia Subaquática (LSTS) * |
| 4 | +//*************************************************************************** |
| 5 | +// This file is part of DUNE: Unified Navigation Environment. * |
| 6 | +// * |
| 7 | +// Commercial Licence Usage * |
| 8 | +// Licencees holding valid commercial DUNE licences may use this file in * |
| 9 | +// accordance with the commercial licence agreement provided with the * |
| 10 | +// Software or, alternatively, in accordance with the terms contained in a * |
| 11 | +// written agreement between you and Faculdade de Engenharia da * |
| 12 | +// Universidade do Porto. For licensing terms, conditions, and further * |
| 13 | +// information contact lsts@fe.up.pt. * |
| 14 | +// * |
| 15 | +// Modified European Union Public Licence - EUPL v.1.1 Usage * |
| 16 | +// Alternatively, this file may be used under the terms of the Modified * |
| 17 | +// EUPL, Version 1.1 only (the "Licence"), appearing in the file LICENCE.md * |
| 18 | +// included in the packaging of this file. You may not use this work * |
| 19 | +// except in compliance with the Licence. Unless required by applicable * |
| 20 | +// law or agreed to in writing, software distributed under the Licence is * |
| 21 | +// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF * |
| 22 | +// ANY KIND, either express or implied. See the Licence for the specific * |
| 23 | +// language governing permissions and limitations at * |
| 24 | +// https://github.com/LSTS/dune/blob/master/LICENCE.md and * |
| 25 | +// http://ec.europa.eu/idabc/eupl.html. * |
| 26 | +//*************************************************************************** |
| 27 | +// Author: Bernardo Gabriel * |
| 28 | +//*************************************************************************** |
| 29 | + |
| 30 | +#include "Factory.hpp" |
| 31 | + |
| 32 | +#include <DUNE/Utils/Utils.hpp> |
| 33 | +#include <DUNE/Streams/Terminal.hpp> |
| 34 | +#include <DUNE/IMC/Factory.hpp> |
| 35 | + |
| 36 | +#include "EntityStateChangeFilter.hpp" |
| 37 | +#include "LogBookEntryFilter.hpp" |
| 38 | + |
| 39 | +namespace DUNE |
| 40 | +{ |
| 41 | + namespace Tasks |
| 42 | + { |
| 43 | + typedef std::unique_ptr<CustomMessageFilter> (*Creator) (DUNE::Tasks::Task*, const std::string&); |
| 44 | + |
| 45 | + template <typename Type> |
| 46 | + static std::unique_ptr<CustomMessageFilter> |
| 47 | + create(DUNE::Tasks::Task* task, const std::string& spec) |
| 48 | + { |
| 49 | + return std::make_unique<Type>(task, spec); |
| 50 | + } |
| 51 | + |
| 52 | + static std::pair<uint32_t, std::string> pairs_id_abbrev[] = |
| 53 | + { |
| 54 | +#define CUSTOM_FILTER(msg_abbrev, filter_abbrev) \ |
| 55 | + std::pair<uint32_t, std::string>(DUNE::IMC::Factory::getIdFromAbbrev(#msg_abbrev), #filter_abbrev), |
| 56 | +#include <DUNE/Tasks/CustomMessageFilters/Factory.def> |
| 57 | + }; |
| 58 | + |
| 59 | + static std::pair<std::string, uint32_t> pairs_abbrev_id[] = |
| 60 | + { |
| 61 | +#define CUSTOM_FILTER(msg_abbrev, filter_abbrev) \ |
| 62 | + std::pair<std::string, uint32_t>(#filter_abbrev, DUNE::IMC::Factory::getIdFromAbbrev(#msg_abbrev)), |
| 63 | +#include <DUNE/Tasks/CustomMessageFilters/Factory.def> |
| 64 | + }; |
| 65 | + |
| 66 | + static std::pair<uint32_t, Creator> creator_pairs_id[] = |
| 67 | + { |
| 68 | +#define CUSTOM_FILTER(msg_abbrev, filter_abbrev) \ |
| 69 | + std::pair<uint32_t, Creator>(DUNE::IMC::Factory::getIdFromAbbrev(#msg_abbrev), &create<filter_abbrev>), |
| 70 | +#include <DUNE/Tasks/CustomMessageFilters/Factory.def> |
| 71 | + }; |
| 72 | + |
| 73 | + DUNE_DECLARE_STATIC_MAP(creators_by_id, uint32_t, Creator, creator_pairs_id); |
| 74 | + DUNE_DECLARE_STATIC_MAP(map_id_abbrev, uint32_t, std::string, pairs_id_abbrev); |
| 75 | + DUNE_DECLARE_STATIC_MAP(map_abbrev_id, std::string, uint32_t, pairs_abbrev_id); |
| 76 | + |
| 77 | + std::string |
| 78 | + CustomMessageFilterFactory::getName(uint32_t id) |
| 79 | + { |
| 80 | + auto it = map_id_abbrev.find(id); |
| 81 | + if (it != map_id_abbrev.end()) |
| 82 | + return it->second; |
| 83 | + |
| 84 | + return ""; |
| 85 | + } |
| 86 | + |
| 87 | + void |
| 88 | + CustomMessageFilterFactory::getNames(std::vector<std::string>& names) |
| 89 | + { |
| 90 | + names.clear(); |
| 91 | + for (auto it = map_id_abbrev.begin(); it != map_id_abbrev.end(); ++it) |
| 92 | + names.push_back(it->second); |
| 93 | + } |
| 94 | + |
| 95 | + void |
| 96 | + CustomMessageFilterFactory::getNames(std::string& names) |
| 97 | + { |
| 98 | + std::vector<std::string> names_vec; |
| 99 | + getNames(names_vec); |
| 100 | + names = Utils::String::join(names_vec.begin(), names_vec.end(), ","); |
| 101 | + } |
| 102 | + |
| 103 | + uint32_t |
| 104 | + CustomMessageFilterFactory::getId(const std::string& name) |
| 105 | + { |
| 106 | + auto it = map_abbrev_id.find(name); |
| 107 | + if (it != map_abbrev_id.end()) |
| 108 | + return it->second; |
| 109 | + |
| 110 | + return 0; |
| 111 | + } |
| 112 | + |
| 113 | + std::unique_ptr<CustomMessageFilter> |
| 114 | + CustomMessageFilterFactory::produce(DUNE::Tasks::Task* task, const std::string& name, const std::string& spec) |
| 115 | + { |
| 116 | + auto it = map_abbrev_id.find(name); |
| 117 | + if (it != map_abbrev_id.end()) |
| 118 | + return produce(task, it->second, spec); |
| 119 | + |
| 120 | + return nullptr; |
| 121 | + } |
| 122 | + |
| 123 | + std::unique_ptr<CustomMessageFilter> |
| 124 | + CustomMessageFilterFactory::produce(DUNE::Tasks::Task* task, uint32_t id, const std::string& spec) |
| 125 | + { |
| 126 | + if (creators_by_id[id]) |
| 127 | + return creators_by_id[id](task, spec); |
| 128 | + |
| 129 | + DUNE_DBG("Custom Filter Factory", "unknown custom filter " << id); |
| 130 | + return nullptr; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments