From: Scott Cantor Date: Tue, 2 May 2006 00:37:01 +0000 (+0000) Subject: Add in plugin manager template. X-Git-Tag: 1.0-alpha1~262 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-xmltooling.git;a=commitdiff_plain;h=26a08e6ab6631b2a5f91ac84ead591752434a9bc Add in plugin manager template. --- diff --git a/xmltooling/ILockable.h b/xmltooling/Lockable.h similarity index 81% rename from xmltooling/ILockable.h rename to xmltooling/Lockable.h index 165399c..d7e2f26 100644 --- a/xmltooling/ILockable.h +++ b/xmltooling/Lockable.h @@ -15,12 +15,12 @@ */ /** - * @file ILockable.h + * @file Lockable.h * * Locking abstraction */ -#if !defined(__xmltooling_lockable_h__) +#ifndef __xmltooling_lockable_h__ #define __xmltooling_lockable_h__ #include @@ -30,16 +30,16 @@ namespace xmltooling { /** * Abstract mixin interface for interfaces that support locking */ - struct XMLTOOL_API ILockable + struct XMLTOOL_API Lockable { - virtual ~ILockable() {} + virtual ~Lockable() {} /** * Lock the associated object for exclusive access. * * @return a reference to the object being locked */ - virtual ILockable& lock()=0; + virtual Lockable& lock()=0; /** * Unlock the associated object from exclusive access. @@ -59,21 +59,21 @@ namespace xmltooling { * * @param lockee Pointer to an object to lock and hold */ - Locker(ILockable* lockee) : m_lockee(lockee->lock()) {} + Locker(Lockable* lockee) : m_lockee(lockee->lock()) {} /** * Locks an object and stores it for later release. * * @param lockee Reference to an object to lock and hold */ - Locker(ILockable& lockee) : m_lockee(lockee.lock()) {} + Locker(Lockable& lockee) : m_lockee(lockee.lock()) {} /** * Releases lock on held pointer, if any. */ ~Locker() {m_lockee.unlock();} private: - ILockable& m_lockee; + Lockable& m_lockee; }; }; diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index 315412f..b76066d 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -27,8 +27,9 @@ libxmltoolinginclude_HEADERS = \ config_pub.h \ ElementProxy.h \ exceptions.h \ - ILockable.h \ + Lockable.h \ Namespace.h \ + PluginManager.h \ QName.h \ SimpleElement.h \ unicode.h \ diff --git a/xmltooling/PluginManager.h b/xmltooling/PluginManager.h new file mode 100644 index 0000000..d876cce --- /dev/null +++ b/xmltooling/PluginManager.h @@ -0,0 +1,95 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file PluginManager.h + * + * Plugin management template + */ + +#ifndef __xmltooling_plugin_h__ +#define __xmltooling_plugin_h__ + +#include + +#include +#include +#include + +using namespace xercesc; + +namespace xmltooling { + + /** + * Template for management/access to plugins constructed based on a string type + * and arbitrary parameters. + * + * @param T class of plugin to manage + * @param Params parameters for plugin construction + */ + template class XMLTOOL_API PluginManager + { + public: + PluginManager() {} + ~PluginManager() {} + + /** Factory function for plugin. */ + typedef T* Factory(typename Params&); + + /** + * Registers the factory for a given type. + * + * @param type the name of the plugin type + * @param factory the factory function for the plugin type + */ + void registerFactory(const char* type, typename Factory* factory) { + if (type && factory) + m_map[type]=factory; + } + + /** + * Unregisters the factory for a given type. + * + * @param type the name of the plugin type + */ + void deregisterFactory(const char* type) { + if (type) { + m_map.erase(type); + } + } + + /** + * Builds a new instance of a plugin of a given type, configuring it + * with the supplied element, if any. + * + * @param type the name of the plugin type + * @param p parameters to configure plugin + * @return the constructed plugin + */ + T* newPlugin(const char* type, typename Params& p) { + std::map::const_iterator i=m_map.find(type); + if (i==m_map.end()) + throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type)); + return i->second(p); + } + + private: + std::map m_map; + }; + +}; + +#endif /* __xmltooling_plugin_h__ */ diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index c3af1da..8e2e48f 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -68,6 +68,7 @@ DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling); DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling); DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling); DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling); +DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling); DECL_EXCEPTION_FACTORY(ValidationException,xmltooling); #ifndef XMLTOOLING_NO_XMLSEC @@ -280,7 +281,7 @@ void XMLToolingInternalConfig::term() Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete"); } -ILockable& XMLToolingInternalConfig::lock() +Lockable& XMLToolingInternalConfig::lock() { xercesc::XMLPlatformUtils::lockMutex(m_lock); return *this; diff --git a/xmltooling/XMLToolingConfig.h b/xmltooling/XMLToolingConfig.h index dacf961..e97bb89 100644 --- a/xmltooling/XMLToolingConfig.h +++ b/xmltooling/XMLToolingConfig.h @@ -20,10 +20,10 @@ * Library configuration */ -#if !defined(__xmltooling_config_h__) +#ifndef __xmltooling_config_h__ #define __xmltooling_config_h__ -#include +#include namespace xmltooling { @@ -34,7 +34,7 @@ namespace xmltooling { * obtain a global system lock, but the actual configuration itself is not * synchronized. */ - class XMLTOOL_API XMLToolingConfig : public ILockable + class XMLTOOL_API XMLToolingConfig : public Lockable { MAKE_NONCOPYABLE(XMLToolingConfig); public: diff --git a/xmltooling/exceptions.h b/xmltooling/exceptions.h index 990c012..f281470 100644 --- a/xmltooling/exceptions.h +++ b/xmltooling/exceptions.h @@ -347,6 +347,7 @@ namespace xmltooling { DECL_XMLTOOLING_EXCEPTION(UnmarshallingException,xmltooling,XMLToolingException,Exceptions during object unmarshalling); DECL_XMLTOOLING_EXCEPTION(UnknownElementException,xmltooling,XMLToolingException,Exceptions due to processing of unknown element content); DECL_XMLTOOLING_EXCEPTION(UnknownAttributeException,xmltooling,XMLToolingException,Exceptions due to processing of unknown attributes); + DECL_XMLTOOLING_EXCEPTION(UnknownExtensionException,xmltooling,XMLToolingException,Exceptions from use of an unrecognized extension/plugin); DECL_XMLTOOLING_EXCEPTION(ValidationException,xmltooling,XMLToolingException,Exceptions during object validation); }; diff --git a/xmltooling/internal.h b/xmltooling/internal.h index ab48364..973684a 100644 --- a/xmltooling/internal.h +++ b/xmltooling/internal.h @@ -26,6 +26,9 @@ # define _CRT_NONSTDC_NO_DEPRECATE 1 #endif +// Export public APIs. +#define XMLTOOLING_EXPORTS + // eventually we might be able to support autoconf via cygwin... #if defined (_MSC_VER) || defined(__BORLANDC__) # include "config_win32.h" @@ -63,7 +66,7 @@ namespace xmltooling { void term(); // global mutex available to library applications - xmltooling::ILockable& lock(); + Lockable& lock(); void unlock(); // configuration @@ -79,7 +82,6 @@ namespace xmltooling { private: std::vector m_libhandles; void* m_lock; - //PlugManager m_plugMgr; }; /// @endcond diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 82490a1..f6dc10a 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -41,7 +41,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories=""$(SolutionDir)";"$(ProjectDir)"" - PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;XMLTOOLING_EXPORTS" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL" MinimalRebuild="true" BasicRuntimeChecks="3" RuntimeLibrary="3" @@ -120,7 +120,7 @@ + +