📄 _pion_plugin_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>libpion: /Users/mdickey/code/libpion-export/src/PionPlugin.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul></div><h1>/Users/mdickey/code/libpion-export/src/PionPlugin.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// -----------------------------------------------------------------</span><a name="l00002"></a>00002 <span class="comment">// libpion: a C++ framework for building lightweight HTTP interfaces</span><a name="l00003"></a>00003 <span class="comment">// -----------------------------------------------------------------</span><a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007 Atomic Labs, Inc. (http://www.atomiclabs.com)</span><a name="l00005"></a>00005 <span class="comment">//</span><a name="l00006"></a>00006 <span class="comment">// Distributed under the Boost Software License, Version 1.0.</span><a name="l00007"></a>00007 <span class="comment">// See accompanying file COPYING or copy at http://www.boost.org/LICENSE_1_0.txt</span><a name="l00008"></a>00008 <span class="comment">//</span><a name="l00009"></a>00009 <a name="l00010"></a>00010 <span class="preprocessor">#include <libpion/PionConfig.hpp></span><a name="l00011"></a>00011 <span class="preprocessor">#include <libpion/PionPlugin.hpp></span><a name="l00012"></a>00012 <span class="preprocessor">#include <boost/filesystem/operations.hpp></span><a name="l00013"></a>00013 <a name="l00014"></a>00014 <span class="preprocessor">#ifdef PION_WIN32</span><a name="l00015"></a>00015 <span class="preprocessor"></span><span class="preprocessor"> #include <windows.h></span><a name="l00016"></a>00016 <span class="preprocessor">#else</span><a name="l00017"></a>00017 <span class="preprocessor"></span><span class="preprocessor"> #include <dlfcn.h></span><a name="l00018"></a>00018 <span class="preprocessor">#endif</span><a name="l00019"></a>00019 <span class="preprocessor"></span><a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="keyword">namespace </span>pion { <span class="comment">// begin namespace pion</span><a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="comment">// static members of PionEngine</span><a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="keyword">const</span> std::string PionPlugin::PION_PLUGIN_CREATE(<span class="stringliteral">"pion_create_"</span>);<a name="l00026"></a>00026 <span class="keyword">const</span> std::string PionPlugin::PION_PLUGIN_DESTROY(<span class="stringliteral">"pion_destroy_"</span>);<a name="l00027"></a>00027 <span class="preprocessor">#ifdef PION_WIN32</span><a name="l00028"></a>00028 <span class="preprocessor"></span> <span class="keyword">const</span> std::string PionPlugin::PION_PLUGIN_EXTENSION(<span class="stringliteral">".dll"</span>);<a name="l00029"></a>00029 <span class="preprocessor">#else</span><a name="l00030"></a>00030 <span class="preprocessor"></span> <span class="keyword">const</span> std::string PionPlugin::PION_PLUGIN_EXTENSION(<span class="stringliteral">".so"</span>);<a name="l00031"></a>00031 <span class="preprocessor">#endif</span><a name="l00032"></a>00032 <span class="preprocessor"></span><span class="keyword">const</span> std::string PionPlugin::PION_CONFIG_EXTENSION(<span class="stringliteral">".conf"</span>);<a name="l00033"></a>00033 std::vector<std::string> PionPlugin::m_plugin_dirs;<a name="l00034"></a>00034 PionPlugin::PluginMap PionPlugin::m_plugin_map;<a name="l00035"></a>00035 boost::mutex PionPlugin::m_plugin_mutex;<a name="l00036"></a>00036 <a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="comment">// PionEngine member functions</span><a name="l00039"></a>00039 <a name="l00040"></a><a class="code" href="classpion_1_1_pion_plugin.html#b84026edef087fb963e6a828d4b8f730">00040</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_pion_plugin.html#b84026edef087fb963e6a828d4b8f730">PionPlugin::checkCygwinPath</a>(boost::filesystem::path& final_path,<a name="l00041"></a>00041 <span class="keyword">const</span> std::string& start_path)<a name="l00042"></a>00042 {<a name="l00043"></a>00043 <span class="preprocessor">#if defined(PION_WIN32) && defined(PION_CYGWIN_DIRECTORY)</span><a name="l00044"></a>00044 <span class="preprocessor"></span> <span class="comment">// try prepending PION_CYGWIN_DIRECTORY if not complete</span><a name="l00045"></a>00045 <span class="keywordflow">if</span> (! final_path.is_complete() && final_path.has_root_directory()) {<a name="l00046"></a>00046 final_path = boost::filesystem::path(std::string(PION_CYGWIN_DIRECTORY) + start_path);<a name="l00047"></a>00047 }<a name="l00048"></a>00048 <span class="preprocessor">#endif</span><a name="l00049"></a>00049 <span class="preprocessor"></span>}<a name="l00050"></a>00050 <a name="l00051"></a><a class="code" href="classpion_1_1_pion_plugin.html#8b8215145cd47f8f87d87cad24bbc488">00051</a> <span class="keywordtype">void</span> PionPlugin::addPluginDirectory(<span class="keyword">const</span> std::string& dir)<a name="l00052"></a>00052 {<a name="l00053"></a>00053 <span class="preprocessor">#ifdef PION_WIN32</span><a name="l00054"></a>00054 <span class="preprocessor"></span> <span class="comment">// work around bug in boost::filesystem on Windows -> do not plugin directories</span><a name="l00055"></a>00055 <span class="comment">// basically, if you create a path object on Windows, then convert it to</span><a name="l00056"></a>00056 <span class="comment">// directory_string() or file_string(), then try to construct</span><a name="l00057"></a>00057 <span class="comment">// a new path object based on the string, it throws an exception (ugh!!!)</span><a name="l00058"></a>00058 boost::mutex::scoped_lock plugin_lock(m_plugin_mutex);<a name="l00059"></a>00059 m_plugin_dirs.push_back(dir);<a name="l00060"></a>00060 <span class="preprocessor">#else</span><a name="l00061"></a>00061 <span class="preprocessor"></span> boost::filesystem::path plugin_path(dir);<a name="l00062"></a>00062 <a class="code" href="classpion_1_1_pion_plugin.html#b84026edef087fb963e6a828d4b8f730">checkCygwinPath</a>(plugin_path, dir);<a name="l00063"></a>00063 <span class="keywordflow">if</span> (! boost::filesystem::exists(plugin_path) )<a name="l00064"></a>00064 <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_pion_plugin_1_1_directory_not_found_exception.html" title="exception thrown if the plug-in directory does not exist">DirectoryNotFoundException</a>(dir);<a name="l00065"></a>00065 boost::mutex::scoped_lock plugin_lock(m_plugin_mutex);<a name="l00066"></a>00066 m_plugin_dirs.push_back(plugin_path.directory_string());<a name="l00067"></a>00067 <span class="preprocessor">#endif</span><a name="l00068"></a>00068 <span class="preprocessor"></span>}<a name="l00069"></a>00069 <a name="l00070"></a><a class="code" href="classpion_1_1_pion_plugin.html#e3edad5eff79a95d95e622ef4bbe3fc0">00070</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_pion_plugin.html#e3edad5eff79a95d95e622ef4bbe3fc0" title="clears all directories from the plug-in search path">PionPlugin::resetPluginDirectories</a>(<span class="keywordtype">void</span>)<a name="l00071"></a>00071 {<a name="l00072"></a>00072 boost::mutex::scoped_lock plugin_lock(m_plugin_mutex);<a name="l00073"></a>00073 m_plugin_dirs.clear();<a name="l00074"></a>00074 }<a name="l00075"></a>00075 <a name="l00076"></a><a class="code" href="classpion_1_1_pion_plugin.html#998d9f6ff965e708ad466ed3afac3b68">00076</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_pion_plugin.html#998d9f6ff965e708ad466ed3afac3b68">PionPlugin::open</a>(<span class="keyword">const</span> std::string& plugin_file)<a name="l00077"></a>00077 {<a name="l00078"></a>00078 <a class="code" href="classpion_1_1_pion_plugin.html#1a4d45d0870f435e2b64242062db8e2d" title="releases the plug-in's shared library symbols">releaseData</a>(); <span class="comment">// make sure we're not already pointing to something</span><a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="comment">// use a temporary object first since openPlugin() may throw</span><a name="l00081"></a>00081 <a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html">PionPluginData</a> plugin_data(<a class="code" href="classpion_1_1_pion_plugin.html#693a3b35e4b8086ef33c11cffdab3491" title="returns the name of the plugin that is currently open">getPluginName</a>(plugin_file));<a name="l00082"></a>00082 <a name="l00083"></a>00083 <span class="comment">// check to see if we already have a matching shared library</span><a name="l00084"></a>00084 boost::mutex::scoped_lock plugin_lock(m_plugin_mutex);<a name="l00085"></a>00085 PluginMap::iterator itr = m_plugin_map.find(plugin_data.<a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#5be82540cf819b32bf8d7d185c11a87c" title="the name of the plugin (must be unique per process)">m_plugin_name</a>);<a name="l00086"></a>00086 <span class="keywordflow">if</span> (itr == m_plugin_map.end()) {<a name="l00087"></a>00087 <span class="comment">// no plug-ins found with the same name</span><a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="comment">// open up the shared library using our temporary data object</span><a name="l00090"></a>00090 openPlugin(plugin_file, plugin_data); <span class="comment">// may throw</span><a name="l00091"></a>00091 <a name="l00092"></a>00092 <span class="comment">// all is good -> insert it into the plug-in map</span><a name="l00093"></a>00093 m_plugin_data = <span class="keyword">new</span> <a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html">PionPluginData</a>(plugin_data);<a name="l00094"></a>00094 m_plugin_map.insert( std::make_pair(m_plugin_data-><a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#5be82540cf819b32bf8d7d185c11a87c" title="the name of the plugin (must be unique per process)">m_plugin_name</a>,<a name="l00095"></a>00095 m_plugin_data) );<a name="l00096"></a>00096 } <span class="keywordflow">else</span> {<a name="l00097"></a>00097 <span class="comment">// found an existing plug-in with the same name</span><a name="l00098"></a>00098 m_plugin_data = itr->second;<a name="l00099"></a>00099 }<a name="l00100"></a>00100 <a name="l00101"></a>00101 <span class="comment">// increment the number of references</span><a name="l00102"></a>00102 ++ m_plugin_data-><a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#8bf1caa36074d1af037f6c8732a2e002" title="number of references to this class">m_references</a>;<a name="l00103"></a>00103 }<a name="l00104"></a>00104 <a name="l00105"></a><a class="code" href="classpion_1_1_pion_plugin.html#1a4d45d0870f435e2b64242062db8e2d">00105</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_pion_plugin.html#1a4d45d0870f435e2b64242062db8e2d" title="releases the plug-in's shared library symbols">PionPlugin::releaseData</a>(<span class="keywordtype">void</span>)<a name="l00106"></a>00106 {<a name="l00107"></a>00107 <span class="keywordflow">if</span> (m_plugin_data != NULL) {<a name="l00108"></a>00108 boost::mutex::scoped_lock plugin_lock(m_plugin_mutex);<a name="l00109"></a>00109 <span class="comment">// double-check after locking mutex</span><a name="l00110"></a>00110 <span class="keywordflow">if</span> (m_plugin_data != NULL && --m_plugin_data-><a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#8bf1caa36074d1af037f6c8732a2e002" title="number of references to this class">m_references</a> == 0) {<a name="l00111"></a>00111 <a name="l00112"></a>00112 <span class="comment">// The handling of dynamic libraries on Windows is EXTREMELY buggy, so if</span><a name="l00113"></a>00113 <span class="comment">// we are running on Windows, never release the shared libraries</span><a name="l00114"></a>00114 <span class="preprocessor">#ifndef PION_WIN32</span><a name="l00115"></a>00115 <span class="preprocessor"></span> <span class="comment">// no more references to the plug-in library</span><a name="l00116"></a>00116 <a name="l00117"></a>00117 <span class="comment">// release the shared object</span><a name="l00118"></a>00118 closeDynamicLibrary(m_plugin_data-><a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#fb02cbdc46d52d18367cb536a27faed4" title="symbol library loaded from a shared object file">m_lib_handle</a>);<a name="l00119"></a>00119 <a name="l00120"></a>00120 <span class="comment">// remove it from the plug-in map</span><a name="l00121"></a>00121 PluginMap::iterator itr = m_plugin_map.find(m_plugin_data-><a class="code" href="structpion_1_1_pion_plugin_1_1_pion_plugin_data.html#5be82540cf819b32bf8d7d185c11a87c" title="the name of the plugin (must be unique per process)">m_plugin_name</a>);<a name="l00122"></a>00122 <span class="comment">// check itr just to be safe (it SHOULD always find a match)</span><a name="l00123"></a>00123 <span class="keywordflow">if</span> (itr != m_plugin_map.end())<a name="l00124"></a>00124 m_plugin_map.erase(itr);<a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="comment">// release the heap object</span><a name="l00127"></a>00127 <span class="keyword">delete</span> m_plugin_data;<a name="l00128"></a>00128 <span class="preprocessor">#endif</span><a name="l00129"></a>00129 <span class="preprocessor"></span> <a name="l00130"></a>00130 }<a name="l00131"></a>00131 m_plugin_data = NULL;<a name="l00132"></a>00132 }<a name="l00133"></a>00133 }<a name="l00134"></a>00134 <a name="l00135"></a><a class="code" href="classpion_1_1_pion_plugin.html#9a76dc0c0fdf0ca4b19f719c122ae424">00135</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_pion_plugin.html#9a76dc0c0fdf0ca4b19f719c122ae424" title="grabs a reference to another plug-in's shared library symbols">PionPlugin::grabData</a>(<span class="keyword">const</span> <a class="code" href="classpion_1_1_pion_plugin.html">PionPlugin</a>& p)<a name="l00136"></a>00136 {<a name="l00137"></a>00137 <a class="code" href="classpion_1_1_pion_plugin.html#1a4d45d0870f435e2b64242062db8e2d" title="releases the plug-in's shared library symbols">releaseData</a>(); <span class="comment">// make sure we're not already pointing to something</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -