From 5dd5c95e8384a2456f8e436f4ccf87a7190c35f1 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 10 Aug 2015 15:45:56 +1000 Subject: [PATCH] library: initial wrapper --- Makefile.am | 5 ++++ library.hpp | 28 +++++++++++++++++++++ library_posix.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ library_posix.hpp | 45 ++++++++++++++++++++++++++++++++++ library_win32.cpp | 45 ++++++++++++++++++++++++++++++++++ library_win32.hpp | 41 +++++++++++++++++++++++++++++++ 6 files changed, 226 insertions(+) create mode 100644 library.hpp create mode 100644 library_posix.cpp create mode 100644 library_posix.hpp create mode 100644 library_win32.cpp create mode 100644 library_win32.hpp diff --git a/Makefile.am b/Makefile.am index b87d33f0..599f12e1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -121,6 +121,7 @@ UTIL_FILES = \ json/schema.hpp \ json/tree.cpp \ json/tree.hpp \ + library.hpp \ log.cpp \ log.hpp \ log.ipp \ @@ -267,6 +268,8 @@ UTIL_FILES += \ backtrace_execinfo.cpp \ io_posix.cpp \ io_posix.hpp \ + library_posix.hpp \ + library_posix.cpp \ posix/dir.hpp \ posix/dir.cpp \ threads/barrier_posix.cpp \ @@ -282,6 +285,8 @@ if PLATFORM_WIN32 UTIL_FILES += \ backtrace_null.cpp \ io_win32.cpp \ + library_win32.hpp \ + library_win32.cpp \ threads/mutex_win32.cpp \ threads/mutex_win32.hpp \ threads/thread_win32.hpp \ diff --git a/library.hpp b/library.hpp new file mode 100644 index 00000000..53b051ad --- /dev/null +++ b/library.hpp @@ -0,0 +1,28 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#ifndef __UTIL_LIBRARY_HPP +#define __UTIL_LIBRARY_HPP + +#include "platform.hpp" + +#if defined(PLATFORM_LINUX) +#include "library_posix.hpp" +#elif defined(PLATFORM_WIN32) +#include "library_win32.hpp" +#endif + +#endif diff --git a/library_posix.cpp b/library_posix.cpp new file mode 100644 index 00000000..2a1ae1ad --- /dev/null +++ b/library_posix.cpp @@ -0,0 +1,62 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#include "library_posix.hpp" + +#include +#include + + +using util::detail::library_posix; + + +/////////////////////////////////////////////////////////////////////////////// +library_posix::library_posix (const std::string &path): + library_posix (path.c_str ()) +{ ; } + + +//----------------------------------------------------------------------------- +library_posix::library_posix (const char *path): + m_handle (dlopen (path, RTLD_NOW)) +{ + if (!m_handle) + throw std::runtime_error (dlerror ()); +} + + +//----------------------------------------------------------------------------- +library_posix::library_posix (library_posix &&rhs): + m_handle (nullptr) +{ + std::swap (m_handle, rhs.m_handle); +} + + +//----------------------------------------------------------------------------- +library_posix::~library_posix () +{ + if (m_handle) + dlclose (m_handle); +} + + +/////////////////////////////////////////////////////////////////////////////// +void* +library_posix::symbol (const char *name) +{ + return dlsym (m_handle, name); +} diff --git a/library_posix.hpp b/library_posix.hpp new file mode 100644 index 00000000..c9e4a8a2 --- /dev/null +++ b/library_posix.hpp @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#ifndef __UTIL_LIBRARY_POSIX_HPP +#define __UTIL_LIBRARY_POSIX_HPP + +#include + +namespace util { + namespace detail { + class library_posix { + public: + library_posix (const char *path); + library_posix (const std::string &path); + library_posix (library_posix&&); + ~library_posix (); + + library_posix (const library_posix&) = delete; + library_posix& operator= (const library_posix&) = delete; + + void* symbol (const char *name); + + private: + void *m_handle; + + }; + } + + typedef detail::library_posix library; +} + +#endif diff --git a/library_win32.cpp b/library_win32.cpp new file mode 100644 index 00000000..af5ce6a3 --- /dev/null +++ b/library_win32.cpp @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#include "library_win32.hpp" + +#include "except.hpp" + +using util::detail::win32::library; + + +/////////////////////////////////////////////////////////////////////////////// +library::library (const char *path): + m_handle (LoadLibraryA (path)) +{ + if (!m_handle) + win32_error::throw_code (); +} + + +//----------------------------------------------------------------------------- +library::~library () +{ + FreeLibrary (m_handle); +} + + +/////////////////////////////////////////////////////////////////////////////// +void* +library::symbol (const char *name) +{ + return reinterpret_cast (GetProcAddress (m_handle, name)); +} diff --git a/library_win32.hpp b/library_win32.hpp new file mode 100644 index 00000000..a3062fe0 --- /dev/null +++ b/library_win32.hpp @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#ifndef __UTIL_LIBRARY_WIN32_HPP +#define __UTIL_LIBRARY_WIN32_HPP + +#include +#include + +namespace util { + namespace detail { namespace win32 { + class library { + public: + library (const char *path); + library (const std::string &path); + ~library (); + + void* symbol (const char *name); + + private: + HMODULE m_handle; + }; + } } + + typedef detail::win32::library library; +} + +#endif