Warning, file /pim/kitinerary/src/lib/mergeutil.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kitinerary_export.h"
0010 
0011 #include <QVariant>
0012 
0013 #include <functional>
0014 
0015 namespace KItinerary {
0016 class Flight;
0017 class Person;
0018 
0019 /** Utilities for merging reservations or elements of them. */
0020 class MergeUtil
0021 {
0022 public:
0023     /**
0024      *  Checks if two Reservation or Trip values refer to the same booking element.
0025      *
0026      *  This does not mean being exactly equal, but having matching identifying properties.
0027      *  What this means exactly depends on the data type:
0028      *  - Flights: booking reference, flight number and departure day match
0029      *  - Train trip: booking reference, train number and departure day match
0030      *  - Bus trip: booking ref and/or number and departure time match
0031      *  - Hotel booking: hotel name, booking reference and checkin day match
0032      *
0033      *  For all reservation types, the Reservation::underName property is
0034      *  checked and either needs to be equal or absent in one of the values.
0035      */
0036     KITINERARY_EXPORT static bool isSame(const QVariant &lhs, const QVariant &rhs);
0037 
0038     /**
0039      * Checks if two Person objects refer to the same person.
0040      *
0041      * Essentially a case-insensitive comparison of the name components.
0042      */
0043     KITINERARY_EXPORT static bool isSamePerson(const Person &lhs, const Person &rhs);
0044 
0045     /**
0046      *  Checks whether to elements refer to the same thing, just for different people.
0047      *  For example two reservations for the same trip or event, but with separate tickets
0048      *  for different attendees.
0049      *  This is useful for batching elements together.
0050      *  @since 5.23.41
0051      */
0052     KITINERARY_EXPORT static bool isSameIncidence(const QVariant &lhs, const QVariant &rhs);
0053 
0054     /**
0055      * Checks whether two transport reservation elements refer to the same departure.
0056      * This considers time, location and mode of transport.
0057      */
0058     static bool hasSameDeparture(const QVariant &lhs, const QVariant &rhs);
0059 
0060     /**
0061      * Checks whether two transport reservation elements refer to the same arrival.
0062      * This considers time, location and mode of transport.
0063      */
0064     static bool hasSameArrival(const QVariant &lhs, const QVariant &rhs);
0065 
0066     /**
0067      * Merge the two given objects.
0068      * This is the same as JsonLdDocument::apply in most cases, but if one side
0069      * can be determined to be "better", that one is preferred.
0070      */
0071     KITINERARY_EXPORT static QVariant merge(const QVariant &lhs, const QVariant &rhs);
0072 
0073     /** Register a comparator function for a custom type that will be used by isSame. */
0074     template <typename T>
0075     inline static void registerComparator(bool(*func)(const T&, const T&))
0076     {
0077         std::function<bool(const QVariant&, const QVariant &)> f([func](const QVariant &lhs, const QVariant &rhs) -> bool {
0078             return (*func)(lhs.value<T>(), rhs.value<T>());
0079         });
0080         registerComparator(qMetaTypeId<T>(), std::move(f));
0081     }
0082 
0083 private:
0084     KITINERARY_EXPORT static void registerComparator(int metaTypeId, std::function<bool(const QVariant&, const QVariant &)> &&func);
0085 };
0086 
0087 }
0088