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
short Property::getType() const
94
#define GET_PTYPE(_name) do {\
95
if(testStatus(App::Property::Prop##_name)) type|=Prop_##_name;\
100
GET_PTYPE(Transient);
101
GET_PTYPE(NoRecompute);
102
GET_PTYPE(NoPersist);
106
void Property::syncType(unsigned type) {
107
#define SYNC_PTYPE(_name) do{\
108
if(type & Prop_##_name) StatusBits.set((size_t)Prop##_name);\
110
SYNC_PTYPE(ReadOnly);
111
SYNC_PTYPE(Transient);
114
SYNC_PTYPE(NoRecompute);
115
SYNC_PTYPE(NoPersist);
118
const char* Property::getGroup() const
120
return father->getPropertyGroup(this);
123
const char* Property::getDocumentation() const
125
return father->getPropertyDocumentation(this);
128
void Property::setContainer(PropertyContainer *Father)
133
void Property::setPathValue(const ObjectIdentifier &path, const boost::any &value)
135
path.setValue(value);
138
const boost::any Property::getPathValue(const ObjectIdentifier &path) const
140
return path.getValue();
143
void Property::getPaths(std::vector<ObjectIdentifier> &paths) const
145
paths.emplace_back(getContainer(), getName());
148
ObjectIdentifier Property::canonicalPath(const ObjectIdentifier &p) const
155
* \brief The PropertyCleaner struct
156
* Make deleting dynamic property safer by postponing its destruction.
158
* Dynamic property can be removed at any time, even during triggering of
159
* onChanged() signal of the removing property. This patch introduced
160
* static function Property::destroy() to make it safer by queueing any
161
* removed property, and only deleting them when no onChanged() call is
164
struct PropertyCleaner {
165
explicit PropertyCleaner(Property *p)
168
++_PropCleanerCounter;
171
if(--_PropCleanerCounter)
174
while (!_RemovedProps.empty()) {
175
auto p = _RemovedProps.back();
176
_RemovedProps.pop_back();
184
_RemovedProps.push_back(prop);
186
static void add(Property *prop) {
187
_RemovedProps.push_back(prop);
192
static std::vector<Property*> _RemovedProps;
193
static int _PropCleanerCounter;
197
std::vector<Property*> PropertyCleaner::_RemovedProps;
198
int PropertyCleaner::_PropCleanerCounter = 0;
200
void Property::destroy(Property *p) {
202
// Is it necessary to nullify the container? May cause crash if any
203
// onChanged() caller assumes a non-null container.
205
// p->setContainer(0);
207
PropertyCleaner::add(p);
211
void Property::touch()
213
PropertyCleaner guard(this);
215
father->onEarlyChange(this);
216
father->onChanged(this);
218
StatusBits.set(Touched);
221
void Property::setReadOnly(bool readOnly)
223
this->setStatus(App::Property::ReadOnly, readOnly);
226
void Property::hasSetValue()
228
PropertyCleaner guard(this);
230
father->onChanged(this);
231
if(!testStatus(Busy)) {
232
Base::BitsetLocker<decltype(StatusBits)> guard(StatusBits,Busy);
233
signalChanged(*this);
236
StatusBits.set(Touched);
239
void Property::aboutToSetValue()
242
father->onBeforeChange(this);
245
void Property::verifyPath(const ObjectIdentifier &p) const
250
Property *Property::Copy() const
252
// have to be reimplemented by a subclass!
257
void Property::Paste(const Property& /*from*/)
259
// have to be reimplemented by a subclass!
263
void Property::setStatusValue(unsigned long status) {
264
static const unsigned long mask =
266
|(1<<PropNoRecompute)
275
status |= StatusBits.to_ulong() & mask;
276
unsigned long oldStatus = StatusBits.to_ulong();
277
StatusBits = decltype(StatusBits)(status);
280
static unsigned long _signalMask = (1<<ReadOnly) | (1<<Hidden);
281
if((status & _signalMask) != (oldStatus & _signalMask))
282
father->onPropertyStatusChanged(*this,oldStatus);
286
void Property::setStatus(Status pos, bool on) {
287
auto bits = StatusBits;
289
setStatusValue(bits.to_ulong());
292
bool Property::isSame(const Property &other) const {
295
if(other.getTypeId() != getTypeId() || getMemSize() != other.getMemSize())
298
Base::StringWriter writer,writer2;
301
return writer.getString() == writer2.getString();
304
//**************************************************************************
305
//**************************************************************************
307
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
309
void PropertyListsBase::_setPyObject(PyObject *value) {
310
std::vector<int> indices;
311
std::vector<PyObject *> vals;
314
if (PyDict_Check(value)) {
315
Py::Dict dict(value);
316
auto size = dict.size();
318
indices.reserve(size);
319
int listSize = getSize();
320
for(auto it=dict.begin();it!=dict.end();++it) {
321
const auto &item = *it;
322
PyObject *key = item.first.ptr();
323
if(!PyLong_Check(key))
324
throw Base::TypeError("expect key type to be integer");
325
long idx = PyLong_AsLong(key);
326
if(idx<-1 || idx>listSize)
327
throw Base::ValueError("index out of bound");
328
if(idx==-1 || idx==listSize) {
332
indices.push_back(idx);
333
vals.push_back(item.second.ptr());
336
if (PySequence_Check(value))
339
PyObject *iter = PyObject_GetIter(value);
341
Py::Object pyIter(iter,true);
342
pySeq = Py::asObject(PySequence_Fast(iter,""));
345
vals.push_back(value);
348
if(!pySeq.isNone()) {
349
Py::Sequence seq(pySeq);
350
vals.reserve(seq.size());
351
for(auto it=seq.begin();it!=seq.end();++it)
352
vals.push_back((*it).ptr());
355
setPyValues(vals,indices);
359
//**************************************************************************
360
//**************************************************************************
362
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
364
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLists , App::Property)