utils.h 3.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#pragma once

namespace WaypointManager {
namespace Utils {


template<class CoordinateType>
CoordinateType getCoordinate(const SimpleMissionItem* item){
    return item->coordinate();
}

template<>
QVariant getCoordinate<QVariant>(const SimpleMissionItem* item);


/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
bool extract(QmlObjectListModel &missionItems,
             ContainerType<CoordinateType> &coordinateList,
             std::size_t startIndex,
             std::size_t  endIndex)
{

    if (   startIndex < std::size_t(missionItems.count())
        && endIndex   < std::size_t(missionItems.count())
        && missionItems.count() > 0) {
        if (startIndex > endIndex) {
            if ( !extract(missionItems,
                          coordinateList,
                          startIndex,
                          std::size_t(missionItems.count()-1)) /*recursion*/)
                return false;
            if ( !extract(missionItems,
                          coordinateList,
                          0,
                          endIndex) /*recursion*/)
                return false;
        } else {
            for (std::size_t i = startIndex; i <= endIndex; ++i) {
                SimpleMissionItem *mItem = missionItems.value<SimpleMissionItem *>(int(i));

                if (mItem == nullptr) {
                    coordinateList.clear();
                    return false;
                }
                coordinateList.append(getCoordinate<CoordinateType>(mItem));
            }
        }
    } else
        return false;

    return true;
}

/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
bool extract(QmlObjectListModel &missionItems,
             ContainerType<CoordinateType> &coordinateList)
{

    return extract(missionItems,
                   coordinateList,
                   std::size_t(0),
                   std::size_t(missionItems.count())
                   );
}


/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
bool extract(ContainerType<CoordinateType>  &source,
             ContainerType<CoordinateType>  &destination,
             std::size_t startIndex,
             std::size_t  endIndex)
{

    if (   startIndex < std::size_t(source.size())
        && endIndex   < std::size_t(source.size())
        && source.size() > 0) {
        if (startIndex > endIndex) {
            if ( !extract(source,
                          destination,
                          startIndex,
                          std::size_t(source.size()-1)) /*recursion*/)
                return false;
            if ( !extract(source,
                          destination,
                          0,
                          endIndex) /*recursion*/)
                return false;
        } else {
            for (std::size_t i = startIndex; i <= endIndex; ++i) {
                destination.append(source[i]);
            }
        }
    } else
        return false;

    return true;
}

} // namespace WaypointManager
} // namespace WaypointManager