| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************************************** |
| 4 |
* * |
| 5 |
* Copyright (c) 2022 Zheng, Lei (realthunder) <[email protected]> * |
| 6 |
* Copyright (c) 2023 FreeCAD Project Association * |
| 7 |
* * |
| 8 |
* This file is part of FreeCAD. * |
| 9 |
* * |
| 10 |
* FreeCAD is free software: you can redistribute it and/or modify it under the terms of the * |
| 11 |
* GNU Lesser General Public License as published by the Free Software Foundation, either * |
| 12 |
* version 2.1 of the License, or (at your option) any later version. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 16 |
* See the GNU Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with * |
| 19 |
* FreeCAD. If not, see <https://www.gnu.org/licenses/>. * |
| 20 |
* * |
| 21 |
**************************************************************************************************/ |
| 22 |
|
| 23 |
#ifndef APP_MAPPED_ELEMENT_H |
| 24 |
#define APP_MAPPED_ELEMENT_H |
| 25 |
|
| 26 |
#include "IndexedName.h" |
| 27 |
#include "MappedName.h" |
| 28 |
|
| 29 |
|
| 30 |
namespace App |
| 31 |
{ |
| 32 |
class DocumentObject; |
| 33 |
} |
| 34 |
|
| 35 |
namespace Data |
| 36 |
{ |
| 37 |
|
| 38 |
/// A MappedElement combines a MappedName and and IndexedName into a single entity and provides |
| 39 |
/// simple comparison operators for the combination (including operator< so that the entity can |
| 40 |
/// be sorted, or used in sorted containers). |
| 41 |
struct AppExport MappedElement |
| 42 |
{ |
| 43 |
IndexedName index; |
| 44 |
MappedName name; |
| 45 |
|
| 46 |
MappedElement() = default; |
| 47 |
|
| 48 |
MappedElement(const IndexedName& idx, MappedName n) |
| 49 |
: index(idx) |
| 50 |
, name(std::move(n)) |
| 51 |
{} |
| 52 |
|
| 53 |
MappedElement(MappedName n, const IndexedName& idx) |
| 54 |
: index(idx) |
| 55 |
, name(std::move(n)) |
| 56 |
{} |
| 57 |
|
| 58 |
~MappedElement() = default; |
| 59 |
|
| 60 |
MappedElement(const MappedElement& other) = default; |
| 61 |
|
| 62 |
MappedElement(MappedElement&& other) noexcept |
| 63 |
: index(other.index) |
| 64 |
, name(std::move(other.name)) |
| 65 |
{} |
| 66 |
|
| 67 |
MappedElement& operator=(MappedElement&& other) noexcept |
| 68 |
{ |
| 69 |
this->index = other.index; |
| 70 |
this->name = std::move(other.name); |
| 71 |
return *this; |
| 72 |
} |
| 73 |
|
| 74 |
MappedElement& operator=(const MappedElement& other) = default; |
| 75 |
|
| 76 |
bool operator==(const MappedElement& other) const |
| 77 |
{ |
| 78 |
return this->index == other.index && this->name == other.name; |
| 79 |
} |
| 80 |
|
| 81 |
bool operator!=(const MappedElement& other) const |
| 82 |
{ |
| 83 |
return this->index != other.index || this->name != other.name; |
| 84 |
} |
| 85 |
|
| 86 |
/// For sorting purposes, one MappedElement is considered "less" than another if its index |
| 87 |
/// compares less (which is first alphabetical, and then by numeric index). If the index of this |
| 88 |
/// MappedElement is the same, then the names are compared lexicographically. |
| 89 |
bool operator<(const MappedElement& other) const |
| 90 |
{ |
| 91 |
int res = this->index.compare(other.index); |
| 92 |
if (res < 0) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
if (res > 0) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
return this->name < other.name; |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
struct AppExport HistoryItem |
| 103 |
{ |
| 104 |
App::DocumentObject* obj; |
| 105 |
long tag; |
| 106 |
Data::MappedName element; |
| 107 |
Data::IndexedName index; |
| 108 |
std::vector<Data::MappedName> intermediates; |
| 109 |
HistoryItem(App::DocumentObject* obj, const Data::MappedName& name); |
| 110 |
}; |
| 111 |
|
| 112 |
struct AppExport ElementNameComparator |
| 113 |
{ |
| 114 |
/** Comparison function to make topo name more stable |
| 115 |
* |
| 116 |
* The sorting decomposes the name into either of the following two forms |
| 117 |
* '#' + hex_digits + tail |
| 118 |
* non_digits + digits + tail |
| 119 |
* |
| 120 |
* The non-digits part is compared lexically, while the digits part is |
| 121 |
* compared by its integer value. |
| 122 |
* |
| 123 |
* The reason for this is to prevent names with bigger digits (which usually means |
| 124 |
* they come later in history) from coming earlier when sorting. |
| 125 |
*/ |
| 126 |
bool operator()(const MappedName& leftName, const MappedName& rightName) const; |
| 127 |
}; |
| 128 |
|
| 129 |
} // namespace Data |
| 130 |
|
| 131 |
|
| 132 |
#endif // APP_MAPPED_ELEMENT_H |