| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <cassert> |
| 24 |
#include <array> |
| 25 |
#include <tuple> |
| 26 |
|
| 27 |
#include <atomic> |
| 28 |
#include <Base/Tools.h> |
| 29 |
#include <Base/Writer.h> |
| 30 |
#include <CXX/Objects.hxx> |
| 31 |
|
| 32 |
#include "Property.h" |
| 33 |
#include "ObjectIdentifier.h" |
| 34 |
#include "PropertyContainer.h" |
| 35 |
|
| 36 |
|
| 37 |
using namespace App; |
| 38 |
|
| 39 |
|
| 40 |
//************************************************************************** |
| 41 |
//************************************************************************** |
| 42 |
// Property |
| 43 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 44 |
|
| 45 |
TYPESYSTEM_SOURCE_ABSTRACT(App::Property, Base::Persistence) |
| 46 |
|
| 47 |
//************************************************************************** |
| 48 |
// Construction/Destruction |
| 49 |
|
| 50 |
static std::atomic<int64_t> _PropID; |
| 51 |
|
| 52 |
// Here is the implementation! Description should take place in the header file! |
| 53 |
Property::Property() |
| 54 |
: _id(++_PropID) |
| 55 |
{} |
| 56 |
|
| 57 |
Property::~Property() = default; |
| 58 |
|
| 59 |
const char* Property::getName() const |
| 60 |
{ |
| 61 |
return myName ? myName : ""; |
| 62 |
} |
| 63 |
|
| 64 |
bool Property::hasName() const |
| 65 |
{ |
| 66 |
return isValidName(myName); |
| 67 |
} |
| 68 |
|
| 69 |
bool Property::isValidName(const char* name) |
| 70 |
{ |
| 71 |
return !Base::Tools::isNullOrEmpty(name); |
| 72 |
} |
| 73 |
|
| 74 |
std::string Property::getFullName() const |
| 75 |
{ |
| 76 |
std::string name; |
| 77 |
if (myName) { |
| 78 |
if (father) { |
| 79 |
name = father->getFullName() + "."; |
| 80 |
} |
| 81 |
else { |
| 82 |
name = "?."; |
| 83 |
} |
| 84 |
name += myName; |
| 85 |
} |
| 86 |
else { |
| 87 |
return "?"; |
| 88 |
} |
| 89 |
return name; |
| 90 |
} |
| 91 |
|
| 92 |
std::string Property::getFileName(const char* postfix, const char* prefix) const |
| 93 |
{ |
| 94 |
std::ostringstream ss; |
| 95 |
if (prefix) { |
| 96 |
ss << prefix; |
| 97 |
} |
| 98 |
if (!myName) { |
| 99 |
ss << "Property"; |
| 100 |
} |
| 101 |
else { |
| 102 |
std::string name = getFullName(); |
| 103 |
auto pos = name.find('#'); |
| 104 |
if (pos == std::string::npos) { |
| 105 |
ss << name; |
| 106 |
} |
| 107 |
else { |
| 108 |
ss << (name.c_str() + pos + 1); |
| 109 |
} |
| 110 |
} |
| 111 |
if (postfix) { |
| 112 |
ss << postfix; |
| 113 |
} |
| 114 |
return ss.str(); |
| 115 |
} |
| 116 |
|
| 117 |
// clang-format off |
| 118 |
static constexpr auto mapProps = std::to_array<std::tuple<Property::Status, PropertyType>>({ |
| 119 |
{App::Property::PropReadOnly, Prop_ReadOnly}, |
| 120 |
{App::Property::PropHidden, Prop_Hidden}, |
| 121 |
{App::Property::PropOutput, Prop_Output}, |
| 122 |
{App::Property::PropTransient, Prop_Transient}, |
| 123 |
{App::Property::PropNoRecompute, Prop_NoRecompute}, |
| 124 |
{App::Property::PropNoPersist, Prop_NoPersist} |
| 125 |
}); |
| 126 |
// clang-format on |
| 127 |
|
| 128 |
short Property::getType() const |
| 129 |
{ |
| 130 |
short type = 0; |
| 131 |
for (const auto& [propertyStatus, propertyType] : mapProps) { |
| 132 |
if (testStatus(propertyStatus)) { |
| 133 |
type |= propertyType; |
| 134 |
} |
| 135 |
} |
| 136 |
return type; |
| 137 |
} |
| 138 |
|
| 139 |
void Property::syncType(unsigned type) |
| 140 |
{ |
| 141 |
for (const auto& [propertyStatus, propertyType] : mapProps) { |
| 142 |
if (type & propertyType) { |
| 143 |
StatusBits.set((size_t)propertyStatus); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
const char* Property::getGroup() const |
| 149 |
{ |
| 150 |
return father->getPropertyGroup(this); |
| 151 |
} |
| 152 |
|
| 153 |
const char* Property::getDocumentation() const |
| 154 |
{ |
| 155 |
return father->getPropertyDocumentation(this); |
| 156 |
} |
| 157 |
|
| 158 |
void Property::setContainer(PropertyContainer* father) |
| 159 |
{ |
| 160 |
this->father = father; |
| 161 |
} |
| 162 |
|
| 163 |
void Property::setPathValue(const ObjectIdentifier& path, const boost::any& value) |
| 164 |
{ |
| 165 |
path.setValue(value); |
| 166 |
} |
| 167 |
|
| 168 |
const boost::any Property::getPathValue(const ObjectIdentifier& path) const |
| 169 |
{ |
| 170 |
return path.getValue(); |
| 171 |
} |
| 172 |
|
| 173 |
void Property::getPaths(std::vector<ObjectIdentifier>& paths) const |
| 174 |
{ |
| 175 |
paths.emplace_back(getContainer(), getName()); |
| 176 |
} |
| 177 |
|
| 178 |
ObjectIdentifier Property::canonicalPath(const ObjectIdentifier& p) const |
| 179 |
{ |
| 180 |
return p; |
| 181 |
} |
| 182 |
|
| 183 |
namespace App |
| 184 |
{ |
| 185 |
/*! |
| 186 |
* \brief The PropertyCleaner struct |
| 187 |
* Make deleting dynamic property safer by postponing its destruction. |
| 188 |
* |
| 189 |
* Dynamic property can be removed at any time, even during triggering of |
| 190 |
* onChanged() signal of the removing property. This patch introduced |
| 191 |
* static function Property::destroy() to make it safer by queueing any |
| 192 |
* removed property, and only deleting them when no onChanged() call is |
| 193 |
* active. |
| 194 |
*/ |
| 195 |
struct PropertyCleaner |
| 196 |
{ |
| 197 |
explicit PropertyCleaner(Property* p) |
| 198 |
: prop(p) |
| 199 |
{ |
| 200 |
++_PropCleanerCounter; |
| 201 |
} |
| 202 |
~PropertyCleaner() |
| 203 |
{ |
| 204 |
if (--_PropCleanerCounter) { |
| 205 |
return; |
| 206 |
} |
| 207 |
bool found = false; |
| 208 |
while (!_RemovedProps.empty()) { |
| 209 |
auto p = _RemovedProps.back(); |
| 210 |
_RemovedProps.pop_back(); |
| 211 |
if (p != prop) { |
| 212 |
p->setContainer(nullptr); |
| 213 |
delete p; |
| 214 |
} |
| 215 |
else { |
| 216 |
found = true; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (found) { |
| 221 |
_RemovedProps.push_back(prop); |
| 222 |
} |
| 223 |
} |
| 224 |
static void add(Property* prop) |
| 225 |
{ |
| 226 |
_RemovedProps.push_back(prop); |
| 227 |
} |
| 228 |
|
| 229 |
Property* prop; |
| 230 |
|
| 231 |
static std::vector<Property*> _RemovedProps; |
| 232 |
static int _PropCleanerCounter; |
| 233 |
}; |
| 234 |
} // namespace App |
| 235 |
|
| 236 |
std::vector<Property*> PropertyCleaner::_RemovedProps; |
| 237 |
int PropertyCleaner::_PropCleanerCounter = 0; |
| 238 |
|
| 239 |
void Property::destroy(Property* p) |
| 240 |
{ |
| 241 |
if (p) { |
| 242 |
// Is it necessary to nullify the container? May cause crash if any |
| 243 |
// onChanged() caller assumes a non-null container. |
| 244 |
// |
| 245 |
// p->setContainer(0); |
| 246 |
|
| 247 |
PropertyCleaner::add(p); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
bool Property::enableNotify(bool enable) |
| 252 |
{ |
| 253 |
bool isNotify = isNotifyEnabled(); |
| 254 |
|
| 255 |
if (enable) { |
| 256 |
StatusBits.reset(DisableNotify); |
| 257 |
} |
| 258 |
else { |
| 259 |
StatusBits.set(DisableNotify); |
| 260 |
} |
| 261 |
return isNotify; |
| 262 |
} |
| 263 |
|
| 264 |
bool Property::isNotifyEnabled() const |
| 265 |
{ |
| 266 |
return !StatusBits.test(DisableNotify); |
| 267 |
} |
| 268 |
|
| 269 |
void Property::touch() |
| 270 |
{ |
| 271 |
PropertyCleaner guard(this); |
| 272 |
if (father && isNotifyEnabled()) { |
| 273 |
father->onEarlyChange(this); |
| 274 |
father->onChanged(this); |
| 275 |
} |
| 276 |
StatusBits.set(Touched); |
| 277 |
} |
| 278 |
|
| 279 |
void Property::setReadOnly(bool readOnly) |
| 280 |
{ |
| 281 |
this->setStatus(App::Property::ReadOnly, readOnly); |
| 282 |
} |
| 283 |
|
| 284 |
void Property::hasSetValue() |
| 285 |
{ |
| 286 |
PropertyCleaner guard(this); |
| 287 |
if (father) { |
| 288 |
if (isNotifyEnabled()) { |
| 289 |
father->onChanged(this); |
| 290 |
} |
| 291 |
if (!testStatus(Busy)) { |
| 292 |
Base::BitsetLocker<decltype(StatusBits)> guard(StatusBits, Busy); |
| 293 |
signalChanged(*this); |
| 294 |
} |
| 295 |
} |
| 296 |
StatusBits.set(Touched); |
| 297 |
} |
| 298 |
|
| 299 |
void Property::aboutToSetValue() |
| 300 |
{ |
| 301 |
if (father) { |
| 302 |
father->onBeforeChange(this); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void Property::verifyPath(const ObjectIdentifier& p) const |
| 307 |
{ |
| 308 |
p.verify(*this); |
| 309 |
} |
| 310 |
|
| 311 |
Property* Property::Copy() const |
| 312 |
{ |
| 313 |
// have to be reimplemented by a subclass! |
| 314 |
assert(0); |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
void Property::Paste(const Property& /*from*/) |
| 319 |
{ |
| 320 |
// have to be reimplemented by a subclass! |
| 321 |
assert(0); |
| 322 |
} |
| 323 |
|
| 324 |
void Property::setStatusValue(unsigned long status) |
| 325 |
{ |
| 326 |
// clang-format off |
| 327 |
static const unsigned long mask = |
| 328 |
(1<<PropDynamic) |
| 329 |
|(1<<PropNoRecompute) |
| 330 |
|(1<<PropReadOnly) |
| 331 |
|(1<<PropTransient) |
| 332 |
|(1<<PropOutput) |
| 333 |
|(1<<PropHidden) |
| 334 |
|(1<<PropNoPersist) |
| 335 |
|(1<<Busy); |
| 336 |
// clang-format on |
| 337 |
|
| 338 |
status &= ~mask; |
| 339 |
status |= StatusBits.to_ulong() & mask; |
| 340 |
unsigned long oldStatus = StatusBits.to_ulong(); |
| 341 |
StatusBits = decltype(StatusBits)(status); |
| 342 |
|
| 343 |
if (father) { |
| 344 |
static unsigned long _signalMask = (1 << ReadOnly) | (1 << Hidden); |
| 345 |
if ((status & _signalMask) != (oldStatus & _signalMask)) { |
| 346 |
father->onPropertyStatusChanged(*this, oldStatus); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
void Property::setStatus(Status pos, bool on) |
| 352 |
{ |
| 353 |
auto bits = StatusBits; |
| 354 |
bits.set(pos, on); |
| 355 |
setStatusValue(bits.to_ulong()); |
| 356 |
} |
| 357 |
|
| 358 |
bool Property::isSame(const Property& other) const |
| 359 |
{ |
| 360 |
if (&other == this) { |
| 361 |
return true; |
| 362 |
} |
| 363 |
if (other.getTypeId() != getTypeId() || getMemSize() != other.getMemSize()) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
Base::StringWriter writer, writer2; |
| 368 |
Save(writer); |
| 369 |
other.Save(writer2); |
| 370 |
return writer.getString() == writer2.getString(); |
| 371 |
} |
| 372 |
|
| 373 |
//************************************************************************** |
| 374 |
//************************************************************************** |
| 375 |
// PropertyListsBase |
| 376 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 377 |
|
| 378 |
void PropertyListsBase::_setPyObject(PyObject* value) |
| 379 |
{ |
| 380 |
std::vector<int> indices; |
| 381 |
std::vector<PyObject*> vals; |
| 382 |
Py::Object pySeq; |
| 383 |
|
| 384 |
if (PyDict_Check(value)) { |
| 385 |
Py::Dict dict(value); |
| 386 |
auto size = dict.size(); |
| 387 |
vals.reserve(size); |
| 388 |
indices.reserve(size); |
| 389 |
int listSize = getSize(); |
| 390 |
for (auto it = dict.begin(); it != dict.end(); ++it) { |
| 391 |
const auto& item = *it; |
| 392 |
PyObject* key = item.first.ptr(); |
| 393 |
if (!PyLong_Check(key)) { |
| 394 |
throw Base::TypeError("expect key type to be integer"); |
| 395 |
} |
| 396 |
long idx = PyLong_AsLong(key); |
| 397 |
if (idx < -1 || idx > listSize) { |
| 398 |
throw Base::ValueError("index out of bound"); |
| 399 |
} |
| 400 |
if (idx == -1 || idx == listSize) { |
| 401 |
idx = listSize; |
| 402 |
++listSize; |
| 403 |
} |
| 404 |
indices.push_back(idx); |
| 405 |
vals.push_back(item.second.ptr()); |
| 406 |
} |
| 407 |
} |
| 408 |
else { |
| 409 |
if (PySequence_Check(value)) { |
| 410 |
pySeq = value; |
| 411 |
} |
| 412 |
else { |
| 413 |
PyObject* iter = PyObject_GetIter(value); |
| 414 |
if (iter) { |
| 415 |
Py::Object pyIter(iter, true); |
| 416 |
pySeq = Py::asObject(PySequence_Fast(iter, "")); |
| 417 |
} |
| 418 |
else { |
| 419 |
PyErr_Clear(); |
| 420 |
vals.push_back(value); |
| 421 |
} |
| 422 |
} |
| 423 |
if (!pySeq.isNone()) { |
| 424 |
Py::Sequence seq(pySeq); |
| 425 |
vals.reserve(seq.size()); |
| 426 |
for (auto it = seq.begin(); it != seq.end(); ++it) { |
| 427 |
vals.push_back((*it).ptr()); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
setPyValues(vals, indices); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
//************************************************************************** |
| 436 |
//************************************************************************** |
| 437 |
// PropertyLists |
| 438 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 439 |
|
| 440 |
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLists, App::Property) |