|
From: <fbe...@us...> - 2014-09-22 08:52:36
|
Revision: 21147
http://sourceforge.net/p/sbml/code/21147
Author: fbergmann
Date: 2014-09-22 08:52:31 +0000 (Mon, 22 Sep 2014)
Log Message:
-----------
- store disabled packages so that they can be re-enabled, and their values persist
Modified Paths:
--------------
trunk/libsbml/src/sbml/SBase.cpp
trunk/libsbml/src/sbml/SBase.h
trunk/libsbml/src/sbml/extension/test/TestPackage.cpp
trunk/libsbml/src/sbml/extension/test/TestPackage.h
trunk/libsbml/src/sbml/extension/test/TestSBMLExtension.cpp
Modified: trunk/libsbml/src/sbml/SBase.cpp
===================================================================
--- trunk/libsbml/src/sbml/SBase.cpp 2014-09-22 07:00:16 UTC (rev 21146)
+++ trunk/libsbml/src/sbml/SBase.cpp 2014-09-22 08:52:31 UTC (rev 21147)
@@ -86,7 +86,7 @@
struct ClonePluginEntity : public unary_function<SBasePlugin*, SBasePlugin*>
{
SBasePlugin* operator() (SBasePlugin* sb) {
- if (!sb) return 0;
+ if (!sb) return NULL;
return sb->clone();
}
};
@@ -484,6 +484,7 @@
mHasBeenDeleted = true;
for_each( mPlugins.begin(), mPlugins.end(), DeletePluginEntity() );
+ deleteDisabledPlugins(false);
}
/*
@@ -3046,6 +3047,27 @@
return (int)mPlugins.size();
}
+unsigned int
+SBase::getNumDisabledPlugins() const
+{
+ return (int)mDisabledPlugins.size();
+}
+
+void
+SBase::deleteDisabledPlugins(bool recursive /*= true*/)
+{
+ for_each(mDisabledPlugins.begin(), mDisabledPlugins.end(), DeletePluginEntity());
+ mDisabledPlugins.clear();
+
+ if (recursive)
+ {
+ List* list = getAllElements();
+ for (unsigned int i = 0; i < list->getSize(); ++i)
+ ((SBase*)list->get(i))->deleteDisabledPlugins();
+ }
+
+}
+
int
SBase::disablePackage(const std::string& pkgURI, const std::string& prefix)
{
@@ -3168,27 +3190,51 @@
{
#if 0
cout << "[DEBUG] SBase::enablePackageInternal() (uri) " << pkgURI
- << " (prefix) " << pkgPrefix << " (element) " << getElementName() << endl;
+ << " (prefix) " << pkgPrefix << " (element) " << getElementName() << endl;
#endif
- mSBMLNamespaces->addNamespace(pkgURI,pkgPrefix);
+ mSBMLNamespaces->addNamespace(pkgURI, pkgPrefix);
}
//
- // enable the given package
+ // go through disabled plugins, and if we have one re-enable that one, rather
+ // than creating a new one
//
- const SBMLExtension* sbmlext = SBMLExtensionRegistry::getInstance().getExtensionInternal(pkgURI);
- if (sbmlext)
+ bool wasDisabled = false;
+ int numDisabledPlugins = (int)mDisabledPlugins.size();
+ for (int i = numDisabledPlugins - 1; i >= 0; --i)
{
- SBaseExtensionPoint extPoint(getPackageName(),getTypeCode());
- const SBasePluginCreatorBase* sbPluginCreator = sbmlext->getSBasePluginCreator(extPoint);
- if (sbPluginCreator)
+ SBasePlugin *current = mDisabledPlugins[i];
+ std::string uri = current->getURI();
+ if (pkgURI == uri)
{
- SBasePlugin* entity = sbPluginCreator->createPlugin(pkgURI,pkgPrefix,getNamespaces());
- entity->connectToParent(this);
- mPlugins.push_back(entity);
+ mDisabledPlugins.erase(mDisabledPlugins.begin() + i);
+ current->connectToParent(this);
+ mPlugins.push_back(current);
+ wasDisabled = true;
}
+ }
+
+ if (!wasDisabled)
+ {
+ //
+ // enable the given package
+ //
+ const SBMLExtension* sbmlext = SBMLExtensionRegistry::getInstance().getExtensionInternal(pkgURI);
+
+ if (sbmlext)
+ {
+ SBaseExtensionPoint extPoint(getPackageName(), getTypeCode());
+ const SBasePluginCreatorBase* sbPluginCreator = sbmlext->getSBasePluginCreator(extPoint);
+ if (sbPluginCreator)
+ {
+ SBasePlugin* entity = sbPluginCreator->createPlugin(pkgURI, pkgPrefix, getNamespaces());
+ entity->connectToParent(this);
+ mPlugins.push_back(entity);
+ }
+ }
+
}
/* check whether we are trying to reenable an unknown package
* that we previously disabled
@@ -3226,12 +3272,15 @@
//
// disable the given package
//
- for (size_t i=0; i < mPlugins.size(); i++)
+ int numPlugins = (int)mPlugins.size();
+ for (int i=numPlugins-1; i >= 0; --i)
{
- std::string uri = mPlugins[i]->getURI();
+ SBasePlugin *current = mPlugins[i];
+ std::string uri = current->getURI();
if (pkgURI == uri)
- {
+ {
mPlugins.erase( mPlugins.begin() + i );
+ mDisabledPlugins.push_back(current);
}
}
Modified: trunk/libsbml/src/sbml/SBase.h
===================================================================
--- trunk/libsbml/src/sbml/SBase.h 2014-09-22 07:00:16 UTC (rev 21146)
+++ trunk/libsbml/src/sbml/SBase.h 2014-09-22 08:52:31 UTC (rev 21147)
@@ -2672,6 +2672,28 @@
/**
+ * Returns the number of disabled plug-in objects (extenstion interfaces)
+ * for SBML Level 3 package extensions known.
+ *
+ * @copydetails doc_what_are_plugins
+ *
+ * @return the number of disabled plug-in objects (extension interfaces)
+ * of package extensions known by this instance of libSBML.
+ *
+ */
+ unsigned int getNumDisabledPlugins() const;
+
+ /**
+ * Deletes all information stored in disabled plugins.
+ *
+ * @param recursive if @c true, the disabled information will be deleted
+ * also from all child elements, otherwise only from this SBase element.
+ *
+ * @see getNumDisabledPlugins()
+ */
+ void deleteDisabledPlugins(bool recursive=true);
+
+ /**
* Enables or disables the given SBML Level 3 package on this object.
*
* This method enables the specified package on this object and other
@@ -3644,7 +3666,13 @@
//
std::vector<SBasePlugin*> mPlugins;
+ //
+ // In case an SBasePlugin is disabled, we still store it here in case it
+ // will be re-enabled later on.
+ //
+ std::vector<SBasePlugin*> mDisabledPlugins;
+
//
// namespace to which this SBase object belongs.
// This variable can be publicly accessible by getElementNamespace function.
Modified: trunk/libsbml/src/sbml/extension/test/TestPackage.cpp
===================================================================
--- trunk/libsbml/src/sbml/extension/test/TestPackage.cpp 2014-09-22 07:00:16 UTC (rev 21146)
+++ trunk/libsbml/src/sbml/extension/test/TestPackage.cpp 2014-09-22 08:52:31 UTC (rev 21147)
@@ -226,11 +226,13 @@
const std::string &prefix,
TestPkgNamespaces *groupsns)
: SBasePlugin(uri,prefix, groupsns)
+ , mValue()
{
}
TestModelPlugin::TestModelPlugin(const TestModelPlugin& orig)
: SBasePlugin(orig)
+ , mValue(orig.mValue)
{
}
@@ -241,6 +243,7 @@
{
if(&orig!=this)
{
+ this->mValue = orig.mValue;
this->SBasePlugin::operator =(orig);
}
return *this;
@@ -252,6 +255,18 @@
return new TestModelPlugin(*this);
}
+const std::string&
+TestModelPlugin::getValue() const
+{
+ return mValue;
+}
+void
+TestModelPlugin::setValue(const std::string& value)
+{
+ mValue = value;
+}
+
+
SBase*
TestModelPlugin::createObject(XMLInputStream& stream)
{
Modified: trunk/libsbml/src/sbml/extension/test/TestPackage.h
===================================================================
--- trunk/libsbml/src/sbml/extension/test/TestPackage.h 2014-09-22 07:00:16 UTC (rev 21146)
+++ trunk/libsbml/src/sbml/extension/test/TestPackage.h 2014-09-22 08:52:31 UTC (rev 21147)
@@ -95,23 +95,26 @@
class TestModelPlugin : public SBasePlugin
{
public:
+ TestModelPlugin(const std::string &uri, const std::string &prefix,
+ TestPkgNamespaces *groupsns);
+ TestModelPlugin(const TestModelPlugin& orig);
+ virtual ~TestModelPlugin();
+ TestModelPlugin& operator=(const TestModelPlugin& orig);
+ virtual TestModelPlugin* clone() const;
+ virtual SBase* createObject(XMLInputStream& stream);
+ virtual void writeElements(XMLOutputStream& stream) const;
+ virtual bool hasRequiredElements() const;
- TestModelPlugin (const std::string &uri, const std::string &prefix,
- TestPkgNamespaces *groupsns);
- TestModelPlugin(const TestModelPlugin& orig);
- virtual ~TestModelPlugin ();
- TestModelPlugin& operator=(const TestModelPlugin& orig);
- virtual TestModelPlugin* clone () const;
- virtual SBase* createObject (XMLInputStream& stream);
- virtual void writeElements (XMLOutputStream& stream) const;
- virtual bool hasRequiredElements() const ;
+ virtual void setSBMLDocument(SBMLDocument* d);
+ virtual void connectToParent(SBase *sbase);
- virtual void setSBMLDocument (SBMLDocument* d);
- virtual void connectToParent (SBase *sbase);
+ virtual void enablePackageInternal(const std::string& pkgURI,
+ const std::string& pkgPrefix, bool flag);
+ const std::string& getValue() const;
+ void setValue(const std::string& value);
- virtual void enablePackageInternal(const std::string& pkgURI,
- const std::string& pkgPrefix, bool flag);
-
+protected:
+ std::string mValue;
};
LIBSBML_CPP_NAMESPACE_END
#endif //__cplusplus
Modified: trunk/libsbml/src/sbml/extension/test/TestSBMLExtension.cpp
===================================================================
--- trunk/libsbml/src/sbml/extension/test/TestSBMLExtension.cpp 2014-09-22 07:00:16 UTC (rev 21146)
+++ trunk/libsbml/src/sbml/extension/test/TestSBMLExtension.cpp 2014-09-22 08:52:31 UTC (rev 21147)
@@ -136,7 +136,45 @@
}
END_TEST
+START_TEST(test_SBMLExtension_reenable)
+{
+ TestPkgNamespaces ns(3, 1, 1);
+ SBMLDocument doc(&ns);
+ Model* model = doc.createModel();
+ TestModelPlugin* mPlugin = dynamic_cast<TestModelPlugin*>( model->getPlugin("test") );
+ fail_unless(mPlugin != NULL);
+ mPlugin->setValue("foo");
+ fail_unless(mPlugin->getValue() == "foo");
+ fail_unless(doc.getNumDisabledPlugins() == 0);
+ // disable plugin
+ doc.disablePackage(TestExtension::getXmlnsL3V1V1(), "test");
+ fail_unless(doc.getNumDisabledPlugins() == 1);
+ mPlugin = dynamic_cast<TestModelPlugin*>(model->getPlugin("test"));
+ fail_unless(mPlugin == NULL);
+
+ // re-enable plugin
+ doc.enablePackage(TestExtension::getXmlnsL3V1V1(), "test", true);
+ fail_unless(doc.getNumDisabledPlugins() == 0);
+ mPlugin = dynamic_cast<TestModelPlugin*>(model->getPlugin("test"));
+ fail_unless(mPlugin != NULL);
+ fail_unless(mPlugin->getValue() == "foo");
+
+ // disable again
+ doc.disablePackage(TestExtension::getXmlnsL3V1V1(), "test");
+ fail_unless(doc.getNumDisabledPlugins() == 1);
+ doc.deleteDisabledPlugins();
+ fail_unless(doc.getNumDisabledPlugins() == 0);
+
+ // enable plugin
+ doc.enablePackage(TestExtension::getXmlnsL3V1V1(), "test", true);
+ mPlugin = dynamic_cast<TestModelPlugin*>(model->getPlugin("test"));
+ fail_unless(mPlugin != NULL);
+ fail_unless(mPlugin->getValue().empty());
+
+}
+END_TEST
+
Suite *
create_suite_SBMLExtension (void)
{
@@ -145,6 +183,7 @@
tcase_add_test( tcase, test_SBMLExtension );
tcase_add_test( tcase, test_SBMLExtension_c_api );
+ tcase_add_test( tcase, test_SBMLExtension_reenable );
suite_add_tcase(suite, tcase);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|