plaintext/plugin_impl/library_impl.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <cassert>
00028 #include <string.h>
00029 #include "library_impl.h"
00030
00031 namespace text
00032 {
00033
00034
00035 static const std::string g_ext_table[] =
00036 {
00037 ".text",
00038 ".txt"
00039 };
00040 static const int EXT_TABLE_SIZE =
00041 sizeof(g_ext_table) / sizeof(g_ext_table[0]);
00042
00043
00044 utils::ObjectTable<PluginLibraryImpl>
00045 PluginLibraryImpl::g_instances_table;
00046
00047
00048 PluginLibraryImpl::PluginLibraryImpl()
00049 {
00050
00051 query_interface = query_interface_impl;
00052 release = release_impl;
00053 is_supported_document = is_supported_document_impl;
00054 create_document = create_document_impl;
00055
00056
00057 g_instances_table.add_interface<IPluginUnknown>(this);
00058 g_instances_table.add_interface<IPluginLibrary>(this);
00059 }
00060
00061 PluginLibraryImpl::~PluginLibraryImpl()
00062 {
00063 g_instances_table.remove(this);
00064 }
00065
00066 PluginStatus
00067 PluginLibraryImpl::query_interface_impl(IPluginUnknown *thiz,
00068 const UDSString *id,
00069 void **ptr)
00070 {
00071
00072
00073
00074 PluginLibraryImpl *instance = g_instances_table.get_object(thiz);
00075 if (g_instances_table.query_interface(instance, id->get_buffer(id), ptr))
00076 {
00077 return PLUGIN_OK;
00078 }
00079 return PLUGIN_FAIL;
00080 }
00081
00082 int PluginLibraryImpl::release_impl( IPluginUnknown *thiz )
00083 {
00084 PluginLibraryImpl *instance = g_instances_table.get_object(thiz);
00085 delete instance;
00086 return 0;
00087 }
00088
00089 PluginBool
00090 PluginLibraryImpl::is_supported_document_impl(IPluginUnknown *thiz,
00091 const UDSString *path)
00092 {
00093 assert(path);
00094
00095 if (path)
00096 {
00097 for(int i = 0; i < EXT_TABLE_SIZE; ++i)
00098 {
00099 const char * pos = strrchr(path->get_buffer(path), '.');
00100 assert(pos);
00101 if (pos)
00102 {
00103 if (strncmp(pos,
00104 g_ext_table[i].c_str(),
00105 g_ext_table[i].length()) == 0)
00106 {
00107 return PLUGIN_TRUE;
00108 }
00109 }
00110 }
00111 }
00112 return PLUGIN_FALSE;
00113 }
00114
00115 IPluginUnknown *
00116 PluginLibraryImpl::create_document_impl(IPluginUnknown * thiz)
00117 {
00118 PluginLibraryImpl *obj = g_instances_table.get_object(thiz);
00119
00120 if (obj)
00121 {
00122
00123 DocPtr doc = new PluginDocImpl;
00124
00125
00126 doc->release_signal.add_slot(obj,
00127 &PluginLibraryImpl::on_document_released);
00128
00129
00130 obj->documents.push_back(doc);
00131 return static_cast<IPluginUnknown *>(doc);
00132 }
00133 return 0;
00134 }
00135
00136 void PluginLibraryImpl::on_document_released(PluginDocImpl * doc)
00137 {
00138 DocumentsIter iter = std::find(documents.begin(), documents.end(), doc);
00139 if (iter != documents.end())
00140 {
00141 delete *iter;
00142 documents.erase(iter);
00143 }
00144 }
00145
00146 }
00147