Mercurial > code
view C++/modules/Dynlib/Dynlib.h @ 334:0b576ee64d45
* Create brand new hierarchy
* Rename DynLib to Dynlib
* Remove some warnings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 08 Mar 2015 14:26:33 +0100 |
parents | C++/DynLib.h@4e17193db141 |
children | d5ec1174b707 |
line wrap: on
line source
/* * DynLib.h -- portable shared library loader * * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _DYNLIB_H_ #define _DYNLIB_H_ #include <string> #if defined(_WIN32) # include <Windows.h> # define DYNLIB_EXPORT __declspec(dllexport) #else # define DYNLIB_EXPORT #endif /** * @class Dynlib * @brief Load a dynamic module * * This class is a portable wrapper to load shared libraries on * supported systems. */ class Dynlib { public: #if defined(_WIN32) using Handle = HMODULE; using Sym = FARPROC; #else using Handle = void *; using Sym = void *; #endif enum Policy { Immediately, //! load symbols immediately Lazy //! load symbols when needed }; private: Handle m_handle; void systemInit(); Handle systemLoad(const std::string &path, Policy policy) const; Sym systemSym(const std::string &name); void systemClose(); public: /** * Copy is forbidden. */ Dynlib(const Dynlib &) = delete; Dynlib &operator=(const Dynlib &) = delete; /** * Default constructor. */ Dynlib(); /** * Constructor to load a shared module. The path must * be absolute. * * @param path the absolute path * @param policy the policy to load * @throw std::runtime_error on error */ Dynlib(const std::string &path, Policy policy = Immediately); /** * Close the library automatically. */ ~Dynlib(); /** * Get a symbol from the library. * * @param name the symbol * @return the symbol * @throw std::runtime_error on error * @throw std::out_of_range if not found */ template <typename T> T sym(const std::string &name) { return reinterpret_cast<T>(systemSym(name)); } }; #endif // !_DYNLIB_H_