swift
propertyindexref.cpp
Go to the documentation of this file.
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 
5 
7 
8 #include <QtGlobal>
9 
10 #include "misc/verify.h"
11 
12 namespace swift::misc
13 {
14  CPropertyIndexRef::CPropertyIndexRef(int index) : m_begin(nullptr), m_sizeOrIndex(index) {}
15 
16  CPropertyIndexRef::CPropertyIndexRef(const QVector<int> &indexes)
17  : m_begin(indexes.data()), m_sizeOrIndex(indexes.size())
18  {}
19 
21  {
22  SWIFT_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");
23  if (this->isEmpty() || !m_begin) { return -1; }
24  CPropertyIndexRef copy = *this;
25  ++copy.m_begin;
26  --copy.m_sizeOrIndex;
27  return copy;
28  }
29 
30  bool CPropertyIndexRef::isNested() const { return m_begin && m_sizeOrIndex > 1; }
31 
32  bool CPropertyIndexRef::isMyself() const { return this->isEmpty(); }
33 
34  bool CPropertyIndexRef::isEmpty() const { return m_begin ? m_sizeOrIndex < 1 : m_sizeOrIndex < 0; }
35 
37  {
38  Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index");
39  return m_begin ? *m_begin : m_sizeOrIndex;
40  }
41 
42  bool CPropertyIndexRef::startsWith(int index) const
43  {
44  if (this->isEmpty()) { return false; }
45  return this->frontToInt() == index;
46  }
47 
48  QString CPropertyIndexRef::toQString(bool i18n) const
49  {
50  Q_UNUSED(i18n);
51  QString s;
52  if (this->isEmpty()) { return s; }
53 
54  auto it = m_begin ? m_begin : &m_sizeOrIndex;
55  auto end = it + (m_begin ? m_sizeOrIndex : 1);
56  for (; it != end; ++it)
57  {
58  Q_ASSERT(*it >= static_cast<int>(CPropertyIndexRef::GlobalIndexCValueObject));
59  if (!s.isEmpty()) { s.append(";"); }
60  s.append(QString::number(*it));
61  }
62  return s;
63  }
64 } // namespace swift::misc
Non-owning reference to a CPropertyIndex with a subset of its features.
bool startsWith(int index) const
Starts with given index?
int frontToInt() const
Front to integer.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
bool isNested() const
Is nested index?
CPropertyIndexRef(int index)
Construct from a single index.
QString toQString(bool i18n=false) const
Cast as QString.
bool isMyself() const
Myself index, used with nesting.
Free functions in swift::misc.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition: lockfree.h:338
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26