1
/***************************************************************************
2
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
31
#include <Base/Tools.h>
32
#include <Base/Writer.h>
33
#include <CXX/Objects.hxx>
36
#include "ObjectIdentifier.h"
37
#include "PropertyContainer.h"
43
//**************************************************************************
44
//**************************************************************************
46
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
48
TYPESYSTEM_SOURCE_ABSTRACT(App::Property , Base::Persistence)
50
//**************************************************************************
51
// Construction/Destruction
53
static std::atomic<int64_t> _PropID;
55
// Here is the implementation! Description should take place in the header file!
61
Property::~Property() = default;
63
const char* Property::getName() const
65
return myName ? myName : "";
68
bool Property::hasName() const
70
return isValidName(myName);
73
bool Property::isValidName(const char* name)
75
return name && name[0] != '\0';
78
std::string Property::getFullName() const {
82
name = father->getFullName() + ".";
91
std::string Property::getFileName(const char* postfix, const char* prefix) const
93
std::ostringstream ss;
101
std::string name = getFullName();
102
auto pos = name.find('#');
103
if (pos == std::string::npos) {
107
ss << (name.c_str() + pos + 1);
116
short Property::getType() const
119
#define GET_PTYPE(_name) do {\
120
if(testStatus(App::Property::Prop##_name)) type|=Prop_##_name;\
125
GET_PTYPE(Transient);
126
GET_PTYPE(NoRecompute);
127
GET_PTYPE(NoPersist);
131
void Property::syncType(unsigned type) {
132
#define SYNC_PTYPE(_name) do{\
133
if(type & Prop_##_name) StatusBits.set((size_t)Prop##_name);\
135
SYNC_PTYPE(ReadOnly);
136
SYNC_PTYPE(Transient);
139
SYNC_PTYPE(NoRecompute);
140
SYNC_PTYPE(NoPersist);
143
const char* Property::getGroup() const
145
return father->getPropertyGroup(this);
148
const char* Property::getDocumentation() const
150
return father->getPropertyDocumentation(this);
153
void Property::setContainer(PropertyContainer *Father)
158
void Property::setPathValue(const ObjectIdentifier &path, const boost::any &value)
160
path.setValue(value);
163
const boost::any Property::getPathValue(const ObjectIdentifier &path) const
165
return path.getValue();
168
void Property::getPaths(std::vector<ObjectIdentifier> &paths) const
170
paths.emplace_back(getContainer(), getName());
173
ObjectIdentifier Property::canonicalPath(const ObjectIdentifier &p) const
180
* \brief The PropertyCleaner struct
181
* Make deleting dynamic property safer by postponing its destruction.
183
* Dynamic property can be removed at any time, even during triggering of
184
* onChanged() signal of the removing property. This patch introduced
185
* static function Property::destroy() to make it safer by queueing any
186
* removed property, and only deleting them when no onChanged() call is
189
struct PropertyCleaner {
190
explicit PropertyCleaner(Property *p)
193
++_PropCleanerCounter;
196
if(--_PropCleanerCounter)
199
while (!_RemovedProps.empty()) {
200
auto p = _RemovedProps.back();
201
_RemovedProps.pop_back();
209
_RemovedProps.push_back(prop);
211
static void add(Property *prop) {
212
_RemovedProps.push_back(prop);
217
static std::vector<Property*> _RemovedProps;
218
static int _PropCleanerCounter;
222
std::vector<Property*> PropertyCleaner::_RemovedProps;
223
int PropertyCleaner::_PropCleanerCounter = 0;
225
void Property::destroy(Property *p) {
227
// Is it necessary to nullify the container? May cause crash if any
228
// onChanged() caller assumes a non-null container.
230
// p->setContainer(0);
232
PropertyCleaner::add(p);
236
void Property::touch()
238
PropertyCleaner guard(this);
240
father->onEarlyChange(this);
241
father->onChanged(this);
243
StatusBits.set(Touched);
246
void Property::setReadOnly(bool readOnly)
248
this->setStatus(App::Property::ReadOnly, readOnly);
251
void Property::hasSetValue()
253
PropertyCleaner guard(this);
255
father->onChanged(this);
256
if(!testStatus(Busy)) {
257
Base::BitsetLocker<decltype(StatusBits)> guard(StatusBits,Busy);
258
signalChanged(*this);
261
StatusBits.set(Touched);
264
void Property::aboutToSetValue()
267
father->onBeforeChange(this);
270
void Property::verifyPath(const ObjectIdentifier &p) const
275
Property *Property::Copy() const
277
// have to be reimplemented by a subclass!
282
void Property::Paste(const Property& /*from*/)
284
// have to be reimplemented by a subclass!
288
void Property::setStatusValue(unsigned long status) {
290
static const unsigned long mask =
292
|(1<<PropNoRecompute)
302
status |= StatusBits.to_ulong() & mask;
303
unsigned long oldStatus = StatusBits.to_ulong();
304
StatusBits = decltype(StatusBits)(status);
307
static unsigned long _signalMask = (1<<ReadOnly) | (1<<Hidden);
308
if((status & _signalMask) != (oldStatus & _signalMask))
309
father->onPropertyStatusChanged(*this,oldStatus);
313
void Property::setStatus(Status pos, bool on) {
314
auto bits = StatusBits;
316
setStatusValue(bits.to_ulong());
319
bool Property::isSame(const Property &other) const {
322
if(other.getTypeId() != getTypeId() || getMemSize() != other.getMemSize())
325
Base::StringWriter writer,writer2;
328
return writer.getString() == writer2.getString();
331
//**************************************************************************
332
//**************************************************************************
334
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
336
void PropertyListsBase::_setPyObject(PyObject *value) {
337
std::vector<int> indices;
338
std::vector<PyObject *> vals;
341
if (PyDict_Check(value)) {
342
Py::Dict dict(value);
343
auto size = dict.size();
345
indices.reserve(size);
346
int listSize = getSize();
347
for(auto it=dict.begin();it!=dict.end();++it) {
348
const auto &item = *it;
349
PyObject *key = item.first.ptr();
350
if(!PyLong_Check(key))
351
throw Base::TypeError("expect key type to be integer");
352
long idx = PyLong_AsLong(key);
353
if(idx<-1 || idx>listSize)
354
throw Base::ValueError("index out of bound");
355
if(idx==-1 || idx==listSize) {
359
indices.push_back(idx);
360
vals.push_back(item.second.ptr());
363
if (PySequence_Check(value))
366
PyObject *iter = PyObject_GetIter(value);
368
Py::Object pyIter(iter,true);
369
pySeq = Py::asObject(PySequence_Fast(iter,""));
372
vals.push_back(value);
375
if(!pySeq.isNone()) {
376
Py::Sequence seq(pySeq);
377
vals.reserve(seq.size());
378
for(auto it=seq.begin();it!=seq.end();++it)
379
vals.push_back((*it).ptr());
382
setPyValues(vals,indices);
386
//**************************************************************************
387
//**************************************************************************
389
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
391
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLists , App::Property)