swift
propertyindex.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/propertyindex.h"
5 
6 #include <QStringList>
7 #include <QtGlobal>
8 
9 #include "misc/verify.h"
10 
11 namespace swift::misc
12 {
13  CPropertyIndex::CPropertyIndex(int singleProperty) : m_indexes { singleProperty }
14  {
15  Q_ASSERT(singleProperty >= static_cast<int>(CPropertyIndexRef::GlobalIndexCValueObject));
16  }
17 
18  CPropertyIndex::CPropertyIndex(std::initializer_list<int> il) : m_indexes(il) {}
19 
20  CPropertyIndex::CPropertyIndex(const QList<int> &indexes) : m_indexes(indexes) {}
21 
22  CPropertyIndex::CPropertyIndex(const QString &indexes) { this->parseFromString(indexes); }
23 
24  CPropertyIndex::operator CPropertyIndexRef() const { return CPropertyIndexRef(m_indexes); }
25 
27  {
28  SWIFT_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");
29  if (this->isEmpty()) { return CPropertyIndex(); }
30  CPropertyIndex copy = *this;
31  copy.m_indexes.pop_front();
32  return copy;
33  }
34 
35  bool CPropertyIndex::isNested() const { return m_indexes.size() > 1; }
36 
37  bool CPropertyIndex::isMyself() const { return m_indexes.isEmpty(); }
38 
39  bool CPropertyIndex::isEmpty() const { return m_indexes.isEmpty(); }
40 
41  QString CPropertyIndex::convertToQString(bool i18n) const
42  {
43  Q_UNUSED(i18n);
44  QString s;
45  for (const int i : m_indexes)
46  {
47  Q_ASSERT(i >= static_cast<int>(CPropertyIndexRef::GlobalIndexCValueObject));
48  if (!s.isEmpty()) { s.append(";"); }
49  s.append(QString::number(i));
50  }
51  return s;
52  }
53 
54  void CPropertyIndex::parseFromString(const QString &indexes)
55  {
56  m_indexes.clear();
57  if (indexes.isEmpty()) { return; }
58  for (const auto &index : QStringView { indexes }.split(';'))
59  {
60  if (index.isEmpty()) { continue; }
61  bool ok;
62  int i = index.toInt(&ok);
63  Q_ASSERT(ok);
64  Q_ASSERT(i >= static_cast<int>(CPropertyIndexRef::GlobalIndexCValueObject));
65  m_indexes.append(i);
66  }
67  }
68 
69  QJsonObject CPropertyIndex::toJson() const
70  {
71  QJsonObject json;
72  json.insert(QStringLiteral("indexes"), this->convertToQString());
73  return json;
74  }
75 
76  void CPropertyIndex::convertFromJson(const QJsonObject &json)
77  {
78  const QJsonValue value = json.value(QLatin1String("indexes"));
79  if (!value.isString()) { throw CJsonException("'indexes' missing or not a string"); }
80  this->parseFromString(value.toString());
81  }
82 
83  QList<int> CPropertyIndex::indexList() const { return m_indexes; }
84 
85  void CPropertyIndex::prepend(int newLeftIndex) { m_indexes.push_front(newLeftIndex); }
86 
87  bool CPropertyIndex::contains(int index) const { return m_indexes.contains(index); }
88 
90  {
91  Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index");
92  return m_indexes.front();
93  }
94 
95  bool CPropertyIndex::startsWith(int index) const
96  {
97  if (this->isEmpty()) { return false; }
98  return this->frontToInt() == index;
99  }
100 } // namespace swift::misc
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
Definition: jsonexception.h:24
QList< int > indexList() const
Index list.
CPropertyIndex()=default
Default constructor.
bool isEmpty() const
Empty?
void prepend(int newLeftIndex)
Shift existing indexes to right and insert given index at front.
bool isMyself() const
Myself index, used with nesting.
bool startsWith(int index) const
Starts with given index?
int frontToInt() const
Front to integer.
bool contains(int index) const
Contains index?
QJsonObject toJson() const
Cast to JSON object.
QString convertToQString(bool i18n=false) const
Cast as QString.
Q_REQUIRED_RESULT CPropertyIndex copyFrontRemoved() const
Copy with first element removed.
bool isNested() const
Is nested index?
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
void parseFromString(const QString &indexes)
Parse indexes from string.
Non-owning reference to a CPropertyIndex with a subset of its features.
Free functions in swift::misc.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26