plaintext/plugin_impl/library_impl.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: library_impl.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-plaintext.
00007  *
00008  * uds-plugin-plaintext is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-plaintext is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <cassert>
00028 #include <string.h>
00029 #include "library_impl.h"
00030 
00031 namespace text
00032 {
00033 
00034 /// @brief Internal file extension table.
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 // Plugin library object implementation.
00044 utils::ObjectTable<PluginLibraryImpl> 
00045 PluginLibraryImpl::g_instances_table;
00046 
00047 // Constructor
00048 PluginLibraryImpl::PluginLibraryImpl()
00049 {
00050     // Initialize all interface method pointers.
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     // record the instance.
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     // Two steps to query the interface.
00072     // 1. Retrieve object instance from the thiz pointer. 
00073     // 2. Retrieve desired interface from given object.
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         // Create a new document object.
00123         DocPtr doc = new PluginDocImpl;
00124 
00125         // Listen the release signal.
00126         doc->release_signal.add_slot(obj, 
00127             &PluginLibraryImpl::on_document_released);
00128 
00129         // Store the document.
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 }   // namespace text
00147 
Generated by  doxygen 1.6.2-20100208