1
2//
3// This source file is part of appleseed.
4// Visit http://appleseedhq.net/ for additional information and resources.
5//
6// This software is released under the MIT license.
7//
8// Copyright (c) 2014-2017 Esteban Tovagliari, The appleseedhq Organization
9//
10// Permission is hereby granted, free of charge, to any person obtaining a copy
11// of this software and associated documentation files (the "Software"), to deal
12// in the Software without restriction, including without limitation the rights
13// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14// copies of the Software, and to permit persons to whom the Software is
15// furnished to do so, subject to the following conditions:
16//
17// The above copyright notice and this permission notice shall be included in
18// all copies or substantial portions of the Software.
19//
20// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26// THE SOFTWARE.
27//
28
29// Interface header.
30#include "sharedlibrary.h"
31
32// Standard headers.
33#include <string>
34
35// Platform headers.
36#ifndef _WIN32
37#include <dlfcn.h>
38#endif
39
40using namespace std;
41
42namespace foundation
43{
44
45//
46// ExceptionCannotLoadSharedLib class implementation.
47//
48
49ExceptionCannotLoadSharedLib::ExceptionCannotLoadSharedLib(
50 const char* path,
51 const char* error_msg)
52{
53 string err("Cannot load shared library ");
54 err += path;
55 err += ". Error = ";
56 err += error_msg;
57 set_what(err.c_str());
58}
59
60
61//
62// ExceptionSharedLibCannotGetSymbol class implementation.
63//
64
65ExceptionSharedLibCannotGetSymbol::ExceptionSharedLibCannotGetSymbol(
66 const char* symbol_name,
67 const char* error_msg)
68{
69 string err("Cannot get symbol ");
70 err += symbol_name;
71 err += ". Error = ";
72 err += error_msg;
73 set_what(err.c_str());
74}
75
76
77//
78// SharedLibrary class implementation.
79//
80
81namespace
82{
83
84string get_last_error_message()
85{
86#ifdef _WIN32
87 return get_windows_last_error_message();
88#else
89 return dlerror();
90#endif
91}
92
93}
94
95SharedLibrary::SharedLibrary(const char* path)
96{
97#ifdef _WIN32
98 m_handle = LoadLibraryA(path);
99#else
100 m_handle = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
101#endif
102
103 if (m_handle == 0)
104 {
105 throw ExceptionCannotLoadSharedLib(
106 path,
107 get_last_error_message().c_str());
108 }
109}
110
111SharedLibrary::~SharedLibrary()
112{
113#ifdef _WIN32
114 FreeLibrary(m_handle);
115#else
116 dlclose(m_handle);
117#endif
118}
119
120void* SharedLibrary::get_symbol(const char* name, const bool no_throw) const
121{
122#ifdef _WIN32
123 void* symbol = GetProcAddress(m_handle, name);
124#else
125 void* symbol = dlsym(m_handle, name);
126#endif
127
128 if (symbol == 0 && !no_throw)
129 {
130 throw ExceptionSharedLibCannotGetSymbol(
131 name,
132 get_last_error_message().c_str());
133 }
134
135 return symbol;
136}
137
138} // namespace foundation
139