Files
create/src/App/DynamicProperty.h
Kevin Martin 5696c9add3 Address the poor performance of the existing unique-name generation (#17944)
* 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>
2024-12-13 10:54:46 -06:00

229 lines
8.3 KiB
C++

/***************************************************************************
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.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 *
* *
***************************************************************************/
#ifndef APP_DYNAMICPROPERTY_H
#define APP_DYNAMICPROPERTY_H
#include <map>
#include <string>
#include <vector>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <FCGlobal.h>
namespace Base
{
class Writer;
class XMLReader;
class XMLWriter;
} // namespace Base
namespace App
{
class Property;
class PropertyContainer;
namespace bmi = boost::multi_index;
struct CStringHasher
{
inline std::size_t operator()(const char* s) const
{
if (!s) {
return 0;
}
return boost::hash_range(s, s + std::strlen(s));
}
inline bool operator()(const char* a, const char* b) const
{
if (!a) {
return !b;
}
if (!b) {
return false;
}
return std::strcmp(a, b) == 0;
}
};
/** This class implements an interface to add properties at run-time to an object
* derived from PropertyContainer. The additional properties are made persistent.
* @author Werner Mayer
*/
class AppExport DynamicProperty
{
public:
DynamicProperty();
virtual ~DynamicProperty();
/** @name Access properties */
//@{
/// Get all properties of the class (including parent)
void getPropertyList(std::vector<Property*>& List) const;
/// get all properties with their names
void getPropertyNamedList(std::vector<std::pair<const char*, Property*>>& List) const;
/// See PropertyContainer::visitProperties for semantics
void visitProperties(std::function<void(Property*)> visitor) const;
/// Get all properties of the class (including parent)
void getPropertyMap(std::map<std::string, Property*>& Map) const;
/// Find a dynamic property by its name
Property* getDynamicPropertyByName(const char* name) const;
/*!
Add a dynamic property of the type @a type and with the name @a name.
@a Group gives the grouping name which appears in the property editor and
@a doc shows the tooltip there.
With @a attr, @a ro and @a hidden the behaviour of the property can be controlled.
@a attr is an OR'ed value of the PropertyType enumeration.
If no special attribute should be set Prop_None can be set (or leave the default of 0).
For convenience the attributes for 'Read-Only' and 'Hidden' can also be controlled with
the values @a ro or @a hidden. This means,
@code
addDynamicProperty(..., ..., "Base","blah", Prop_ReadOnly | Prop_Hidden);
@endcode
is equivalent to
@code
addDynamicProperty(..., ..., "Base","blah", Prop_None, true, true);
@endcode
*/
Property* addDynamicProperty(PropertyContainer& pc,
const char* type,
const char* name = nullptr,
const char* group = nullptr,
const char* doc = nullptr,
short attr = 0,
bool ro = false,
bool hidden = false);
/** Add a pre-existing property
*
* The property is not treated as dynamic, and will not trigger signal.
*
* @return Return false if there is a property exist with the same name.
*/
bool addProperty(Property* prop);
/*!
Removes a dynamic property by name. Returns true if the property is part of the container,
otherwise false is returned.
*/
bool removeDynamicProperty(const char* name);
/// Remove pre-existing property, which will not be deleted.
bool removeProperty(const Property* prop);
/// Get a list of all dynamic properties.
std::vector<std::string> getDynamicPropertyNames() const;
/// Get the name of a property
const char* getPropertyName(const Property* prop) const;
//@}
/** @name Property attributes */
//@{
/// Get the attributes of a property
short getPropertyType(const Property* prop) const;
/// Get the attributes of a named property
short getPropertyType(const char* name) const;
/// Get the group name of a property
const char* getPropertyGroup(const Property* prop) const;
/// Get the group name of a named property
const char* getPropertyGroup(const char* name) const;
/// Get the documentation of a property
const char* getPropertyDocumentation(const Property* prop) const;
/// Get the documentation of a named property
const char* getPropertyDocumentation(const char* name) const;
//@}
/// Remove all properties
void clear();
/// Get property count
size_t size() const
{
return props.size();
}
void save(const Property* prop, Base::Writer& writer) const;
Property* restore(PropertyContainer& pc,
const char* PropName,
const char* TypeName,
Base::XMLReader& reader);
struct PropData
{
Property* property;
std::string name;
const char* pName;
mutable std::string group;
mutable std::string doc;
short attr;
bool readonly;
bool hidden;
PropData(Property* prop = nullptr,
std::string&& n = std::string(),
const char* pn = nullptr,
const char* g = nullptr,
const char* d = nullptr,
short a = 0,
bool ro = false,
bool h = false)
: property(prop)
, name(std::move(n))
, pName(pn)
, group(g ? g : "")
, doc(d ? d : "")
, attr(a)
, readonly(ro)
, hidden(h)
{}
const char* getName() const
{
return pName ? pName : name.c_str();
}
};
PropData getDynamicPropertyData(const Property* prop) const;
bool changeDynamicProperty(const Property* prop, const char* group, const char* doc);
private:
std::string getUniquePropertyName(PropertyContainer& pc, const char* Name) const;
private:
bmi::multi_index_container<
PropData,
bmi::indexed_by<
bmi::hashed_unique<bmi::const_mem_fun<PropData, const char*, &PropData::getName>,
CStringHasher,
CStringHasher>,
bmi::hashed_unique<bmi::member<PropData, Property*, &PropData::property>>>>
props;
};
} // namespace App
#endif // APP_DYNAMICPROPERTY_H