Extensions: Implement persistence
This commit is contained in:
@@ -1153,7 +1153,11 @@ void Document::writeObjects(const std::vector<App::DocumentObject*>& obj,
|
||||
|
||||
writer.incInd(); // indentation for 'Object name'
|
||||
for (it = obj.begin(); it != obj.end(); ++it) {
|
||||
writer.Stream() << writer.ind() << "<Object name=\"" << (*it)->getNameInDocument() << "\">" << endl;
|
||||
writer.Stream() << writer.ind() << "<Object name=\"" << (*it)->getNameInDocument() << "\"";
|
||||
if((*it)->hasExtensions())
|
||||
writer.Stream() << " Extensions=\"True\"";
|
||||
|
||||
writer.Stream() << ">" << endl;
|
||||
(*it)->Save(writer);
|
||||
writer.Stream() << writer.ind() << "</Object>" << endl;
|
||||
}
|
||||
|
||||
@@ -305,7 +305,7 @@ bool DocumentObject::isTouched() const
|
||||
void DocumentObject::Save (Base::Writer &writer) const
|
||||
{
|
||||
writer.ObjectName = this->getNameInDocument();
|
||||
App::PropertyContainer::Save(writer);
|
||||
App::ExtensionContainer::Save(writer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -331,6 +331,11 @@ std::string DynamicProperty::encodeAttribute(const std::string& str) const
|
||||
|
||||
void DynamicProperty::Save (Base::Writer &writer) const
|
||||
{
|
||||
//extenions must be saved first, as they need to be read and initialised before properties (as
|
||||
//they have their own properties which they need to handle on restore)
|
||||
if(this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
|
||||
static_cast<App::ExtensionContainer*>(this->pc)->saveExtensions(writer);
|
||||
|
||||
std::map<std::string,Property*> Map;
|
||||
getPropertyMap(Map);
|
||||
|
||||
@@ -389,6 +394,10 @@ void DynamicProperty::Save (Base::Writer &writer) const
|
||||
|
||||
void DynamicProperty::Restore(Base::XMLReader &reader)
|
||||
{
|
||||
//first all extensions must be initialised so that they can handle their properties
|
||||
if(this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
|
||||
static_cast<App::ExtensionContainer*>(this->pc)->restoreExtensions(reader);
|
||||
|
||||
reader.readElement("Properties");
|
||||
int Cnt = reader.getAttributeAsInteger("Count");
|
||||
|
||||
|
||||
@@ -108,12 +108,12 @@ PyObject* Extension::getExtensionPyObject(void) {
|
||||
std::string Extension::name() const {
|
||||
|
||||
if(m_extensionType.isBad())
|
||||
throw Base::Exception("Extension::setExtendedObject: Extension type not set");
|
||||
throw Base::Exception("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)
|
||||
if(pos != std::string::npos)
|
||||
return temp.substr(pos+1);
|
||||
else
|
||||
return std::string();
|
||||
|
||||
@@ -253,6 +253,12 @@ public:
|
||||
virtual const char* extensionGetPropertyDocumentation(const char *name) const;
|
||||
//@}
|
||||
|
||||
/** @name Persistance */
|
||||
//@{
|
||||
virtual void extensionSave(Base::Writer&) const {};
|
||||
virtual void extensionRestore(Base::XMLReader&) {};
|
||||
//@}
|
||||
|
||||
/** @name TypeHandling */
|
||||
//@{
|
||||
bool extensionIsDerivedFrom(const Base::Type type) const {return getExtensionTypeId().isDerivedFrom(type);}
|
||||
|
||||
@@ -83,11 +83,11 @@ bool ExtensionContainer::hasExtension(Base::Type t) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExtensionContainer::hasExtension(const char* name) const {
|
||||
bool ExtensionContainer::hasExtension(std::string name) const {
|
||||
|
||||
//and for types derived from it, as they can be cast to the extension
|
||||
for(auto entry : _extensions) {
|
||||
if(strcmp(entry.second->name().c_str(), name) == 0)
|
||||
if(entry.second->name() == name)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -110,11 +110,16 @@ Extension* ExtensionContainer::getExtension(Base::Type t) {
|
||||
return result->second;
|
||||
}
|
||||
|
||||
Extension* ExtensionContainer::getExtension(const char* name) {
|
||||
bool ExtensionContainer::hasExtensions() const {
|
||||
|
||||
return !_extensions.empty();
|
||||
}
|
||||
|
||||
Extension* ExtensionContainer::getExtension(std::string name) {
|
||||
|
||||
//and for types derived from it, as they can be cast to the extension
|
||||
for(auto entry : _extensions) {
|
||||
if(strcmp(entry.second->name().c_str(), name) == 0)
|
||||
if(entry.second->name() == name)
|
||||
return entry.second;
|
||||
}
|
||||
return nullptr;
|
||||
@@ -273,3 +278,135 @@ void ExtensionContainer::onChanged(const Property* prop) {
|
||||
|
||||
App::PropertyContainer::onChanged(prop);
|
||||
}
|
||||
|
||||
void ExtensionContainer::Save(Base::Writer& writer) const {
|
||||
|
||||
//Note: save extensions must be called first to ensure that the extension element is always the
|
||||
// very first inside the object element. That is needed as extension eleent works together with
|
||||
// an object attribute, and if annother element would be read first the object attributes would be
|
||||
// cleared.
|
||||
saveExtensions(writer);
|
||||
App::PropertyContainer::Save(writer);
|
||||
}
|
||||
|
||||
void ExtensionContainer::Restore(Base::XMLReader& reader) {
|
||||
|
||||
//restore dynamic extensions.
|
||||
//Note 1: The extension element must be read first, before all other object elements. That is
|
||||
// needed as the element works together with an object element attribute, which would be
|
||||
// cleared if annother attribute is read first
|
||||
//Note 2: This must happen before the py object of this container is used, as only in the
|
||||
// pyobject constructor the extension methods are added to the container.
|
||||
restoreExtensions(reader);
|
||||
App::PropertyContainer::Restore(reader);
|
||||
}
|
||||
|
||||
void ExtensionContainer::saveExtensions(Base::Writer& writer) const {
|
||||
|
||||
//we don't save anything if there are no dynamic extensions
|
||||
if(!hasExtensions())
|
||||
return;
|
||||
|
||||
//save dynamic extensions
|
||||
writer.incInd(); // indentation for 'Extensions'
|
||||
writer.Stream() << writer.ind() << "<Extensions Count=\"" << _extensions.size() << "\">" << std::endl;
|
||||
for(auto entry : _extensions) {
|
||||
|
||||
auto ext = entry.second;
|
||||
writer.incInd(); // indentation for 'Extension name'
|
||||
writer.Stream() << writer.ind() << "<Extension"
|
||||
<< " type=\"" << ext->getExtensionTypeId().getName() <<"\""
|
||||
<< " name=\"" << ext->name() << "\">" << std::endl;
|
||||
writer.incInd(); // indentation for the actual Extension
|
||||
try {
|
||||
// We must make sure to handle all exceptions accordingly so that
|
||||
// the project file doesn't get invalidated. In the error case this
|
||||
// means to proceed instead of aborting the write operation.
|
||||
ext->extensionSave(writer);
|
||||
}
|
||||
catch (const Base::Exception &e) {
|
||||
Base::Console().Error("%s\n", e.what());
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
Base::Console().Error("%s\n", e.what());
|
||||
}
|
||||
catch (const char* e) {
|
||||
Base::Console().Error("%s\n", e);
|
||||
}
|
||||
#ifndef FC_DEBUG
|
||||
catch (...) {
|
||||
Base::Console().Error("ExtensionContainer::Save: Unknown C++ exception thrown. Try to continue...\n");
|
||||
}
|
||||
#endif
|
||||
writer.decInd(); // indentation for the actual extension
|
||||
writer.Stream() << writer.ind() << "</Extension>" << std::endl;
|
||||
writer.decInd(); // indentation for 'Extension name'
|
||||
}
|
||||
writer.Stream() << writer.ind() << "</Extensions>" << std::endl;
|
||||
writer.decInd();
|
||||
}
|
||||
|
||||
void ExtensionContainer::restoreExtensions(Base::XMLReader& reader) {
|
||||
|
||||
//Dynamic extensions are optional (also because they are introduced late into the document format)
|
||||
//and hence it is possible that the element does not exist. As we cannot check for the existance of
|
||||
//an element a object attribute is set if extensions are available. Here we check that
|
||||
//attribute, and only if it exists the extensions element will be available.
|
||||
if(!reader.hasAttribute("Extensions"))
|
||||
return;
|
||||
|
||||
reader.readElement("Extensions");
|
||||
int Cnt = reader.getAttributeAsInteger("Count");
|
||||
|
||||
for (int i=0 ;i<Cnt ;i++) {
|
||||
reader.readElement("Extension");
|
||||
const char* Type = reader.getAttribute("type");
|
||||
const char* Name = reader.getAttribute("type");
|
||||
try {
|
||||
App::Extension* ext = getExtension(Name);
|
||||
if(!ext) {
|
||||
//get the extension type asked for
|
||||
Base::Type extension = Base::Type::fromName(Type);
|
||||
if (extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
|
||||
std::stringstream str;
|
||||
str << "No extension found of type '" << Type << "'" << std::ends;
|
||||
throw Base::Exception(str.str());
|
||||
}
|
||||
|
||||
//register the extension
|
||||
ext = static_cast<App::Extension*>(extension.createInstance());
|
||||
//check if this really is a python extension!
|
||||
if (!ext->isPythonExtension()) {
|
||||
delete ext;
|
||||
std::stringstream str;
|
||||
str << "Extension is not a python addable version: '" << Type << "'" << std::ends;
|
||||
throw Base::Exception(str.str());
|
||||
}
|
||||
|
||||
ext->initExtension(this);
|
||||
}
|
||||
if (ext && strcmp(ext->getExtensionTypeId().getName(), Type) == 0)
|
||||
ext->extensionRestore(reader);
|
||||
}
|
||||
catch (const Base::XMLParseException&) {
|
||||
throw; // re-throw
|
||||
}
|
||||
catch (const Base::Exception &e) {
|
||||
Base::Console().Error("%s\n", e.what());
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
Base::Console().Error("%s\n", e.what());
|
||||
}
|
||||
catch (const char* e) {
|
||||
Base::Console().Error("%s\n", e);
|
||||
}
|
||||
#ifndef FC_DEBUG
|
||||
catch (...) {
|
||||
Base::Console().Error("ExtensionContainer::Restore: Unknown C++ exception thrown");
|
||||
}
|
||||
#endif
|
||||
|
||||
reader.readEndElement("Extension");
|
||||
}
|
||||
reader.readEndElement("Extensions");
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include "PropertyPythonObject.h"
|
||||
#include "DynamicProperty.h"
|
||||
#include <CXX/Objects.hxx>
|
||||
#include <Base/Writer.h>
|
||||
#include <Base/Reader.h>
|
||||
|
||||
namespace App {
|
||||
|
||||
@@ -123,9 +125,10 @@ public:
|
||||
|
||||
void registerExtension(Base::Type extension, App::Extension* ext);
|
||||
bool hasExtension(Base::Type) const; //returns first of type (or derived from) and throws otherwise
|
||||
bool hasExtension(const char* name) const; //this version does not check derived classes
|
||||
bool hasExtension(std::string name) const; //this version does not check derived classes
|
||||
bool hasExtensions() const;
|
||||
App::Extension* getExtension(Base::Type); //returns first of type (or derived from) and throws otherwise
|
||||
App::Extension* getExtension(const char* name); //this version does not check derived classes
|
||||
App::Extension* getExtension(std::string name); //this version does not check derived classes
|
||||
|
||||
//returns first of type (or derived from) and throws otherwise
|
||||
template<typename ExtensionT>
|
||||
@@ -176,6 +179,14 @@ public:
|
||||
|
||||
virtual void onChanged(const Property*);
|
||||
|
||||
virtual void Save(Base::Writer& writer) const;
|
||||
virtual void Restore(Base::XMLReader& reader);
|
||||
|
||||
//those methods save/restore the dynamic extenions without handling properties, which is something
|
||||
//done by the default Save/Restore methods.
|
||||
void saveExtensions(Base::Writer& writer) const;
|
||||
void restoreExtensions(Base::XMLReader& reader);
|
||||
|
||||
private:
|
||||
//stored extensions
|
||||
std::map<Base::Type, App::Extension*> _extensions;
|
||||
|
||||
Reference in New Issue
Block a user