library: initial wrapper

This commit is contained in:
Danny Robson 2015-08-10 15:45:56 +10:00
parent 243716a121
commit 5dd5c95e83
6 changed files with 226 additions and 0 deletions

View File

@ -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 \

28
library.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#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

62
library_posix.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#include "library_posix.hpp"
#include <dlfcn.h>
#include <stdexcept>
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);
}

45
library_posix.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_LIBRARY_POSIX_HPP
#define __UTIL_LIBRARY_POSIX_HPP
#include <string>
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

45
library_win32.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#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<void*> (GetProcAddress (m_handle, name));
}

41
library_win32.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_LIBRARY_WIN32_HPP
#define __UTIL_LIBRARY_WIN32_HPP
#include <string>
#include <windows.h>
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