* Address the poor performance of the existing unique-name generation
As described in Issue 16849, the existing Tools::getUniqueName method
requires calling code to form a vector of existing names to be avoided.
This leads to poor performance both in the O(n) cost of building such a
vector and also getUniqueName's O(n) algorithm for actually generating
the unique name (where 'n' is the number of pre-existing names).
This has particularly noticeable cost in documents with large numbers
of DocumentObjects because generating both Names and Labels for each new
object incurs this cost. During an operation such as importing this
results in an O(n^2) time spent generating names.
The other major cost is in the saving of the temporary backup file,
which uses name generation for the "files" embedded in the Zip file.
Documents can easily need several such "files" for each object in the
document.
This update includes the following changes:
Create UniqueNameManager to keep a list of existing names organized in
a manner that eases unique-name generation. This class essentially acts
as a set of names, with the ability to add and remove names and check if
a name is already there, with the added ability to take a prototype name
and generate a unique form for it which is not already in the set.
Eliminate Tools::getUniqueName
Make DocumentObject naming use the new UniqueNameManager class
Make DocumentObject Label naming use the new UniqueNameManager class.
Labels are not always unique; unique labels are generated if the
settings at the time request it (and other conditions). Because of this
the Label management requires additionally keeping a map of counts
for labels which already exist more than once.
These collections are maintained via notifications of value changes on
the Label properties of the objects in the document.
Add Document::containsObject(DocumentObject*) for a definitive
test of an object being in a Document. This is needed because
DocumentObjects can be in a sort of limbo (e.g. when they are in the
Undo/Redo lists) where they have a parent linkage to the Document but
should not participate in Label collision checks.
Rename Document.getStandardObjectName to getStandardObjectLabel
to better represent what it does.
Use new UniqueNameManager for Writer internal filenames within the zip
file.
Eliminate unneeded Reader::FileNames collection. The file names
already exist in the FileList collection elements. The only existing
use for the FileNames collection was to determine if there were any
files at all, and with FileList and FileNames being parallel
vectors, they both had the same length so FileList could be used
for this test..
Use UniqueNameManager for document names and labels. This uses ad hoc
UniqueNameManager objects created on the spot on the assumption that
document creation is relatively rare and there are few documents, so
although the cost is O(n), n itself is small.
Use an ad hoc UniqueNameManager to name new DymanicProperty entries.
This is only done if a property of the proposed name already exists,
since such a check is more-or-less O(log(n)), almost never finds a
collision, and avoids the O(n) building of the UniqueNameManager.
If there is a collision an ad-hoc UniqueNameManager is built
and discarded after use.
The property management classes have a bit of a mess of methods
including several to populate various collection types with all
existing properties. Rather than introducing yet another such
collection-specific method to fill a UniqueNameManager, a
visitProperties method was added which calls a passed function for
each property. The existing code would be simpler if existing
fill-container methods all used this.
Ideally the PropertyContainer class would keep a central directory of
all properties ("static", Dynamic, and exposed by ExtensionContainer and
other derivations) and a permanent UniqueNameManager. However the
Property management is a bit of a mess making such a change a project
unto itself.
The unit tests for Tools:getUniqueName have been changed to test
UniqueNameManager.makeUniqueName instead.
This revealed a small regression insofar as passing a prototype name
like "xyz1234" to the old code would yield "xyz1235" whether or
not "xyz1234" already existed, while the new code will return the next
name above the currently-highest name on the "xyz" model, which could
be "xyz" or "xyz1".
* Correct wrong case on include path
* Implement suggested code changes
Also change the semantics of visitProperties to not have any short-circuit return
* Remove reference through undefined iterator
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix up some comments for DOxygen
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
233 lines
7.6 KiB
C++
233 lines
7.6 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.net> *
|
|
* *
|
|
* This file is part of the FreeCAD CAx development system. *
|
|
* *
|
|
* This library is free software; you can redistribute it and/or *
|
|
* modify it under the terms of the GNU Library General Public *
|
|
* License as published by the Free Software Foundation; either *
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
* *
|
|
* This library is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU Library General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Library General Public *
|
|
* License along with this library; see the file COPYING.LIB. If not, *
|
|
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
|
* Suite 330, Boston, MA 02111-1307, USA *
|
|
* *
|
|
***************************************************************************/
|
|
|
|
|
|
#include "PreCompiled.h"
|
|
|
|
#ifndef _PreComp_
|
|
#include <cassert>
|
|
#endif
|
|
|
|
#include <Base/PyObjectBase.h>
|
|
|
|
#include "Extension.h"
|
|
#include "ExtensionContainer.h"
|
|
#include "ExtensionPython.h"
|
|
#include <ExtensionPy.h>
|
|
|
|
|
|
/* We do not use a standard property macro for type initiation. The reason is that we have the first
|
|
* PropertyData in the extension chain, there is no parent property data.
|
|
*/
|
|
EXTENSION_TYPESYSTEM_SOURCE_P(App::Extension)
|
|
|
|
const App::PropertyData* App::Extension::extensionGetPropertyDataPtr()
|
|
{
|
|
return &propertyData;
|
|
}
|
|
|
|
const App::PropertyData& App::Extension::extensionGetPropertyData() const
|
|
{
|
|
return propertyData;
|
|
}
|
|
|
|
App::PropertyData App::Extension::propertyData;
|
|
|
|
void App::Extension::init()
|
|
{
|
|
assert(Extension::classTypeId == Base::Type::badType() && "don't init() twice!");
|
|
|
|
/* Set up entry in the type system. */
|
|
Extension::classTypeId =
|
|
Base::Type::createType(Base::Type::badType(), "App::Extension", Extension::create);
|
|
}
|
|
|
|
using namespace App;
|
|
|
|
Extension::~Extension()
|
|
{
|
|
if (!ExtensionPythonObject.is(Py::_None())) {
|
|
// Remark: The API of Py::Object has been changed to set whether the wrapper owns the passed
|
|
// Python object or not. In the constructor we forced the wrapper to own the object so we
|
|
// need not to dec'ref the Python object any more. But we must still invalidate the Python
|
|
// object because it need not to be destructed right now because the interpreter can own
|
|
// several references to it.
|
|
Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(ExtensionPythonObject.ptr());
|
|
// Call before decrementing the reference counter, otherwise a heap error can occur
|
|
obj->setInvalid();
|
|
}
|
|
}
|
|
|
|
void Extension::initExtensionType(Base::Type type)
|
|
{
|
|
m_extensionType = type;
|
|
if (m_extensionType.isBad()) {
|
|
throw Base::RuntimeError("Extension: Extension type not set");
|
|
}
|
|
}
|
|
|
|
void Extension::initExtension(ExtensionContainer* obj)
|
|
{
|
|
if (m_extensionType.isBad()) {
|
|
throw Base::RuntimeError("Extension: Extension type not set");
|
|
}
|
|
|
|
// all properties are initialised without PropertyContainer father. Now that we know it we can
|
|
// finally finish the property initialisation
|
|
std::vector<Property*> list;
|
|
extensionGetPropertyData().getPropertyList(this, list);
|
|
for (Property* prop : list) {
|
|
prop->setContainer(obj);
|
|
}
|
|
|
|
m_base = obj;
|
|
m_base->registerExtension(m_extensionType, this);
|
|
}
|
|
|
|
|
|
PyObject* Extension::getExtensionPyObject()
|
|
{
|
|
if (ExtensionPythonObject.is(Py::_None())) {
|
|
// ref counter is set to 1
|
|
auto grp = new ExtensionPy(this);
|
|
ExtensionPythonObject = Py::Object(grp, true);
|
|
}
|
|
return Py::new_reference_to(ExtensionPythonObject);
|
|
}
|
|
|
|
std::string Extension::name() const
|
|
{
|
|
if (m_extensionType.isBad()) {
|
|
throw Base::RuntimeError("Extension::name: Extension type not set");
|
|
}
|
|
|
|
std::string temp(m_extensionType.getName());
|
|
std::string::size_type pos = temp.find_last_of(':');
|
|
|
|
if (pos != std::string::npos) {
|
|
return temp.substr(pos + 1);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
Property* Extension::extensionGetPropertyByName(const char* name) const
|
|
{
|
|
return extensionGetPropertyData().getPropertyByName(this, name);
|
|
}
|
|
|
|
short int Extension::extensionGetPropertyType(const Property* prop) const
|
|
{
|
|
return extensionGetPropertyData().getType(this, prop);
|
|
}
|
|
|
|
short int Extension::extensionGetPropertyType(const char* name) const
|
|
{
|
|
return extensionGetPropertyData().getType(this, name);
|
|
}
|
|
|
|
const char* Extension::extensionGetPropertyName(const Property* prop) const
|
|
{
|
|
return extensionGetPropertyData().getName(this, prop);
|
|
}
|
|
|
|
const char* Extension::extensionGetPropertyGroup(const Property* prop) const
|
|
{
|
|
return extensionGetPropertyData().getGroup(this, prop);
|
|
}
|
|
|
|
const char* Extension::extensionGetPropertyGroup(const char* name) const
|
|
{
|
|
return extensionGetPropertyData().getGroup(this, name);
|
|
}
|
|
|
|
|
|
const char* Extension::extensionGetPropertyDocumentation(const Property* prop) const
|
|
{
|
|
return extensionGetPropertyData().getDocumentation(this, prop);
|
|
}
|
|
|
|
const char* Extension::extensionGetPropertyDocumentation(const char* name) const
|
|
{
|
|
return extensionGetPropertyData().getDocumentation(this, name);
|
|
}
|
|
|
|
void Extension::extensionGetPropertyList(std::vector<Property*>& List) const
|
|
{
|
|
|
|
extensionGetPropertyData().getPropertyList(this, List);
|
|
}
|
|
|
|
void Extension::extensionGetPropertyMap(std::map<std::string, Property*>& Map) const
|
|
{
|
|
extensionGetPropertyData().getPropertyMap(this, Map);
|
|
}
|
|
|
|
void Extension::extensionVisitProperties(std::function<void(Property*)> visitor) const
|
|
{
|
|
extensionGetPropertyData().visitProperties(this, visitor);
|
|
}
|
|
void Extension::initExtensionSubclass(Base::Type& toInit,
|
|
const char* ClassName,
|
|
const char* ParentName,
|
|
Base::Type::instantiationMethod method)
|
|
{
|
|
// don't init twice!
|
|
assert(toInit == Base::Type::badType());
|
|
// get the parent class
|
|
Base::Type parentType(Base::Type::fromName(ParentName));
|
|
// forgot init parent!
|
|
assert(parentType != Base::Type::badType());
|
|
|
|
// create the new type
|
|
toInit = Base::Type::createType(parentType, ClassName, method);
|
|
}
|
|
|
|
bool Extension::extensionHandleChangedPropertyName(Base::XMLReader& reader,
|
|
const char* TypeName,
|
|
const char* PropName)
|
|
{
|
|
(void)reader;
|
|
(void)TypeName;
|
|
(void)PropName;
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Extension::extensionHandleChangedPropertyType(Base::XMLReader& reader,
|
|
const char* TypeName,
|
|
Property* prop)
|
|
{
|
|
(void)reader;
|
|
(void)TypeName;
|
|
(void)prop;
|
|
|
|
return false;
|
|
}
|
|
|
|
namespace App
|
|
{
|
|
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::ExtensionPython, App::ExtensionPython::Inherited)
|
|
|
|
// explicit template instantiation
|
|
template class AppExport ExtensionPythonT<Extension>;
|
|
} // namespace App
|