Add in plugin manager template.
authorScott Cantor <cantor.2@osu.edu>
Tue, 2 May 2006 00:37:01 +0000 (00:37 +0000)
committerScott Cantor <cantor.2@osu.edu>
Tue, 2 May 2006 00:37:01 +0000 (00:37 +0000)
xmltooling/Lockable.h [moved from xmltooling/ILockable.h with 81% similarity]
xmltooling/Makefile.am
xmltooling/PluginManager.h [new file with mode: 0644]
xmltooling/XMLToolingConfig.cpp
xmltooling/XMLToolingConfig.h
xmltooling/exceptions.h
xmltooling/internal.h
xmltooling/xmltooling.vcproj

similarity index 81%
rename from xmltooling/ILockable.h
rename to xmltooling/Lockable.h
index 165399c..d7e2f26 100644 (file)
  */\r
 \r
 /**\r
- * @file ILockable.h\r
+ * @file Lockable.h\r
  * \r
  * Locking abstraction \r
  */\r
 \r
-#if !defined(__xmltooling_lockable_h__)\r
+#ifndef __xmltooling_lockable_h__\r
 #define __xmltooling_lockable_h__\r
 \r
 #include <xmltooling/base.h>\r
@@ -30,16 +30,16 @@ namespace xmltooling {
     /**\r
      * Abstract mixin interface for interfaces that support locking\r
      */\r
-    struct XMLTOOL_API ILockable\r
+    struct XMLTOOL_API Lockable\r
     {\r
-        virtual ~ILockable() {}\r
+        virtual ~Lockable() {}\r
         \r
         /**\r
          * Lock the associated object for exclusive access.\r
          * \r
          * @return a reference to the object being locked\r
          */\r
-        virtual ILockable& lock()=0;\r
+        virtual Lockable& lock()=0;\r
 \r
         /**\r
          * Unlock the associated object from exclusive access.\r
@@ -59,21 +59,21 @@ namespace xmltooling {
          * \r
          * @param lockee    Pointer to an object to lock and hold\r
          */\r
-        Locker(ILockable* lockee) : m_lockee(lockee->lock()) {}\r
+        Locker(Lockable* lockee) : m_lockee(lockee->lock()) {}\r
         \r
         /**\r
          * Locks an object and stores it for later release.\r
          * \r
          * @param lockee    Reference to an object to lock and hold\r
          */\r
-        Locker(ILockable& lockee) : m_lockee(lockee.lock()) {}\r
+        Locker(Lockable& lockee) : m_lockee(lockee.lock()) {}\r
 \r
         /**\r
          * Releases lock on held pointer, if any.\r
          */\r
         ~Locker() {m_lockee.unlock();}\r
     private:\r
-        ILockable& m_lockee;\r
+        Lockable& m_lockee;\r
     };\r
 \r
 };\r
index 315412f..b76066d 100644 (file)
@@ -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 (file)
index 0000000..d876cce
--- /dev/null
@@ -0,0 +1,95 @@
+/*\r
+ *  Copyright 2001-2006 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * @file PluginManager.h\r
+ * \r
+ * Plugin management template\r
+ */\r
+\r
+#ifndef __xmltooling_plugin_h__\r
+#define __xmltooling_plugin_h__\r
+\r
+#include <xmltooling/base.h>\r
+\r
+#include <map>\r
+#include <string>\r
+#include <xercesc/dom/DOM.hpp>\r
+\r
+using namespace xercesc;\r
+\r
+namespace xmltooling {\r
+\r
+    /**\r
+     * Template for management/access to plugins constructed based on a string type\r
+     * and arbitrary parameters.\r
+     * \r
+     * @param T         class of plugin to manage\r
+     * @param Params    parameters for plugin construction\r
+     */\r
+    template <class T, typename Params> class XMLTOOL_API PluginManager\r
+    {\r
+    public:\r
+        PluginManager() {}\r
+        ~PluginManager() {}\r
+\r
+        /** Factory function for plugin. */\r
+        typedef T* Factory(typename Params&);\r
+\r
+        /**\r
+         * Registers the factory for a given type.\r
+         * \r
+         * @param type      the name of the plugin type\r
+         * @param factory   the factory function for the plugin type\r
+         */\r
+        void registerFactory(const char* type, typename Factory* factory) {\r
+            if (type && factory)\r
+                m_map[type]=factory;\r
+        }\r
+\r
+        /**\r
+         * Unregisters the factory for a given type.\r
+         * \r
+         * @param type  the name of the plugin type\r
+         */\r
+        void deregisterFactory(const char* type) {\r
+            if (type) {\r
+                m_map.erase(type);\r
+            }\r
+        }\r
+\r
+        /**\r
+         * Builds a new instance of a plugin of a given type, configuring it\r
+         * with the supplied element, if any.\r
+         * \r
+         * @param type  the name of the plugin type\r
+         * @param p     parameters to configure plugin\r
+         * @return      the constructed plugin  \r
+         */\r
+        T* newPlugin(const char* type, typename Params& p) {\r
+            std::map<std::string, typename Factory*>::const_iterator i=m_map.find(type);\r
+            if (i==m_map.end())\r
+                throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type));\r
+            return i->second(p);\r
+        }\r
+        \r
+    private:\r
+        std::map<std::string, typename Factory*> m_map;\r
+    };\r
+\r
+};\r
+\r
+#endif /* __xmltooling_plugin_h__ */\r
index c3af1da..8e2e48f 100644 (file)
@@ -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;
index dacf961..e97bb89 100644 (file)
  * Library configuration \r
  */\r
 \r
-#if !defined(__xmltooling_config_h__)\r
+#ifndef __xmltooling_config_h__\r
 #define __xmltooling_config_h__\r
 \r
-#include <xmltooling/ILockable.h>\r
+#include <xmltooling/Lockable.h>\r
 \r
 namespace xmltooling {\r
 \r
@@ -34,7 +34,7 @@ namespace xmltooling {
      * obtain a global system lock, but the actual configuration itself is not\r
      * synchronized.\r
      */\r
-    class XMLTOOL_API XMLToolingConfig : public ILockable\r
+    class XMLTOOL_API XMLToolingConfig : public Lockable\r
     {\r
     MAKE_NONCOPYABLE(XMLToolingConfig);\r
     public:\r
index 990c012..f281470 100644 (file)
@@ -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);
 
 };
index ab48364..973684a 100644 (file)
@@ -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<void*> m_libhandles;
         void* m_lock;
-        //PlugManager m_plugMgr;
     };
     /// @endcond
 
index 82490a1..f6dc10a 100644 (file)
@@ -41,7 +41,7 @@
                                Name="VCCLCompilerTool"\r
                                Optimization="0"\r
                                AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(ProjectDir)&quot;"\r
-                               PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;XMLTOOLING_EXPORTS"\r
+                               PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL"\r
                                MinimalRebuild="true"\r
                                BasicRuntimeChecks="3"\r
                                RuntimeLibrary="3"\r
                        <Tool\r
                                Name="VCCLCompilerTool"\r
                                AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(ProjectDir)&quot;"\r
-                               PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;XMLTOOLING_EXPORTS"\r
+                               PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL"\r
                                RuntimeLibrary="2"\r
                                UsePrecompiledHeader="0"\r
                                WarningLevel="3"\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\ILockable.h"\r
+                               RelativePath=".\internal.h"\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\internal.h"\r
+                               RelativePath=".\Lockable.h"\r
                                >\r
                        </File>\r
                        <File\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\PluginManager.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\QName.h"\r
                                >\r
                        </File>\r