home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / plugins.h < prev    next >
C/C++ Source or Header  |  2008-11-07  |  9KB  |  281 lines

  1. #ifndef K3DSDK_PLUGINS_H
  2. #define K3DSDK_PLUGINS_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.     \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "plugins_detail.h"
  28. #include "types.h"
  29.  
  30. #include <set>
  31. #include <typeinfo>
  32.  
  33.  
  34. namespace k3d
  35. {
  36.  
  37. class iplugin_factory;
  38. class uuid;
  39.  
  40. namespace mime { class type; }
  41.  
  42. namespace plugin
  43. {
  44.  
  45. namespace factory
  46. {
  47.  
  48. /// Defines storage for a collection of plugin factories
  49. typedef std::set<iplugin_factory*> collection_t;
  50.  
  51. /// Returns the set of all available plugin factories.
  52. const collection_t lookup();
  53. /// Returns the plugin factory that implements a specific factory ID, or NULL.
  54. iplugin_factory* lookup(const uuid& ID);
  55. /// Returns the plugin factory that matches the given name, or NULL.  Note: returns NULL if more than one factory matches Name.
  56. iplugin_factory* lookup(const string_t& Name);
  57. /// Returns the set of plugin factories that match the given metadata name-value pair.
  58. const collection_t lookup(const string_t& MetadataName, const string_t& MetadataValue);
  59. /// Returns the set of plugin factories that implement a specific interface.
  60. const collection_t lookup(const std::type_info& Interface);
  61. /// Returns the set of plugin factories that implement a specific interface.
  62. template<typename interface_t>
  63. const collection_t lookup()
  64. {
  65.     return lookup(typeid(interface_t));
  66. }
  67. /// Returns the set of plugin factories that match a MIME type.
  68. const collection_t lookup(const mime::type& Type);
  69. /// Returns the set of plugin factories that implement a specific interface and match a MIME type.
  70. const collection_t lookup(const std::type_info& Interface, const mime::type& Type);
  71. /// Returns the set of plugin factories that implement a specific interface and match a MIME type.
  72. template<typename interface_t>
  73. const collection_t lookup(const mime::type& Type)
  74. {
  75.     return lookup(typeid(interface_t), Type);
  76. }
  77.  
  78. } // namespace factory
  79.  
  80. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  81. template<typename interface_t> interface_t* create(const string_t& FactoryName)
  82. {
  83.     if(iunknown* const unknown = detail::create_application_plugin(FactoryName))
  84.     {
  85.         if(interface_t* const result = dynamic_cast<interface_t*>(unknown))
  86.         {
  87.             return result;
  88.         }
  89.         else
  90.         {
  91.             log() << error << "Plugin doesn't implement interface: " << FactoryName << std::endl;
  92.         }
  93.  
  94.         delete unknown;
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
  100. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  101. template<typename interface_t> interface_t* create(iplugin_factory& Factory)
  102. {
  103.     if(iunknown* const unknown = detail::create_application_plugin(Factory))
  104.     {
  105.         if(interface_t* const result = dynamic_cast<interface_t*>(unknown))
  106.         {
  107.             return result;
  108.         }
  109.         else
  110.         {
  111.             log() << error << "Plugin doesn't implement interface: " << Factory.name() << std::endl;
  112.         }
  113.  
  114.         delete unknown;
  115.     }
  116.  
  117.     return 0;
  118. }
  119.  
  120. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  121. template<typename interface_t> interface_t* create(const uuid& FactoryID)
  122. {
  123.     if(iunknown* const unknown = detail::create_application_plugin(FactoryID))
  124.     {
  125.         if(interface_t* const result = dynamic_cast<interface_t*>(unknown))
  126.         {
  127.             return result;
  128.         }
  129.         else
  130.         {
  131.             log() << error << "Plugin doesn't implement interface: " << FactoryID << std::endl;
  132.         }
  133.  
  134.         delete unknown;
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
  140. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  141. inline iunknown* create(const string_t& FactoryName)
  142. {
  143.     return detail::create_application_plugin(FactoryName);
  144. }
  145.  
  146. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  147. inline iunknown* create(iplugin_factory& Factory)
  148. {
  149.     return detail::create_application_plugin(Factory);
  150. }
  151.  
  152. /// Creates an application plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  153. inline iunknown* create(const uuid& FactoryID)
  154. {
  155.     return detail::create_application_plugin(FactoryID);
  156. }
  157.  
  158. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  159. template<typename interface_t> interface_t* create(const string_t& FactoryName, idocument& Document, const string_t& Name = string_t())
  160. {
  161.     if(inode* const node = detail::create_document_plugin(FactoryName, Document, Name))
  162.     {
  163.         if(interface_t* const result = dynamic_cast<interface_t*>(node))
  164.         {
  165.             node->set_name(Name);
  166.             undoable_new(node, Document);
  167.             Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  168.  
  169.             return result;
  170.         }
  171.         else
  172.         {
  173.             log() << error << "Plugin doesn't implement interface: " << FactoryName << std::endl;
  174.         }
  175.  
  176.         delete node;
  177.     }
  178.  
  179.     return 0;
  180. }
  181.  
  182. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  183. template<typename interface_t> interface_t* create(iplugin_factory& Factory, idocument& Document, const string_t& Name = string_t())
  184. {
  185.     if(inode* const node = detail::create_document_plugin(Factory, Document, Name))
  186.     {
  187.         if(interface_t* const result = dynamic_cast<interface_t*>(node))
  188.         {
  189.             node->set_name(Name);
  190.             undoable_new(node, Document);
  191.             Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  192.  
  193.             return result;
  194.         }
  195.         else
  196.         {
  197.             log() << error << "Plugin doesn't implement interface: " << Factory.name() << std::endl;
  198.         }
  199.  
  200.         delete node;
  201.     }
  202.  
  203.     return 0;
  204. }
  205.  
  206. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  207. template<typename interface_t> interface_t* create(const uuid& FactoryID, idocument& Document, const string_t& Name = string_t())
  208. {
  209.     if(inode* const node = detail::create_document_plugin(FactoryID, Document, Name))
  210.     {
  211.         if(interface_t* const result = dynamic_cast<interface_t*>(node))
  212.         {
  213.             node->set_name(Name);
  214.             undoable_new(node, Document);
  215.             Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  216.  
  217.             return result;
  218.         }
  219.         else
  220.         {
  221.             log() << error << "Plugin doesn't implement interface: " << FactoryID << std::endl;
  222.         }
  223.  
  224.         delete node;
  225.     }
  226.  
  227.     return 0;
  228. }
  229.  
  230. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  231. inline iunknown* create(const string_t& FactoryName, idocument& Document, const string_t& Name = string_t())
  232. {
  233.     if(inode* const node = detail::create_document_plugin(FactoryName, Document, Name))
  234.     {
  235.         node->set_name(Name);
  236.         undoable_new(node, Document);
  237.         Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  238.  
  239.         return node;
  240.     }
  241.  
  242.     return 0;
  243. }
  244.  
  245. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  246. inline iunknown* create(iplugin_factory& Factory, idocument& Document, const string_t& Name = string_t())
  247. {
  248.     if(inode* const node = detail::create_document_plugin(Factory, Document, Name))
  249.     {
  250.         node->set_name(Name);
  251.         undoable_new(node, Document);
  252.         Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  253.  
  254.         return node;
  255.     }
  256.  
  257.     return 0;
  258. }
  259.  
  260. /// Creates a document plugin, returning the requested interface if implemented, otherwise NULL (cleans-up on failure)
  261. inline iunknown* create(const uuid& FactoryID, idocument& Document, const string_t& Name = string_t())
  262. {
  263.     if(inode* const node = detail::create_document_plugin(FactoryID, Document, Name))
  264.     {
  265.         node->set_name(Name);
  266.         undoable_new(node, Document);
  267.         Document.nodes().add_nodes(make_collection<inode_collection::nodes_t>(node));
  268.  
  269.         return node;
  270.     }
  271.  
  272.     return 0;
  273. }
  274.  
  275. } // namespace plugin
  276.  
  277. } // namespace k3d
  278.  
  279. #endif // !K3DSDK_PLUGINS_H
  280.  
  281.