QmlObjectListHelper.h 1.28 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
#ifndef QMLOBJECTLISTHELPER_H
#define QMLOBJECTLISTHELPER_H

#include "QmlObjectListModel.h"

#include <type_traits>

template <class PtrType>
inline bool contains(const QmlObjectListModel &in, const PtrType toFind) {

  static_assert(std::is_pointer<PtrType>::value, "PtrType must be a pointer.");

  typedef typename std::remove_pointer<PtrType>::type Type;

  for (int i = 0; i < in.count(); ++i) {
    const auto obj = qobject_cast<const Type *>(in[i]);
    Q_ASSERT(obj != nullptr);
    Q_ASSERT(toFind != nullptr);
    if (*obj == *toFind) {
      return true;
    }
  }
  return false;
}

template <class PtrType>
inline bool equals(const QmlObjectListModel &list,
                   const QmlObjectListModel &otherList) {

  static_assert(std::is_pointer<PtrType>::value, "PtrType must be a pointer.");

  typedef typename std::remove_pointer<PtrType>::type Type;

  if (list.count() == otherList.count()) {

    for (int i = 0; i < list.count(); ++i) {
      const auto obj = qobject_cast<const Type *>(list[i]);
      const auto otherObj = qobject_cast<const Type *>(otherList[i]);
      Q_ASSERT(obj != nullptr);
      Q_ASSERT(otherObj != nullptr);
      if (*obj != *otherObj) {
        return false;
      }
    }

    return true;
  } else {
    return false;
  }
}

#endif // QMLOBJECTLISTHELPER_H