source: trunk/src/core/library.cc @ 380

Last change on this file since 380 was 380, checked in by epyon, 10 years ago
  • oops, missed src : got rid of to_string and other std::string utilities (except slurp) string no longer in nv namespace
File size: 3.8 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4#include "nv/core/common.hh"
5#include "nv/core/library.hh"
6
7#if NV_PLATFORM == NV_WINDOWS
8#   define WIN32_LEAN_AND_MEAN
9#   include <windows.h>
10#   define NV_LIB_EXT ".dll"
11#   define NV_LIB_HANDLE HMODULE
12#   define NV_LIB_OPEN( name ) LoadLibraryEx( name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
13#   define NV_LIB_GET( handle, name ) GetProcAddress( handle, name )
14#   define NV_LIB_CLOSE( name ) !FreeLibrary( name )
15#elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
16#   include <dlfcn.h>
17#   define NV_LIB_EXT ".so"
18#   define NV_LIB_HANDLE void*
19#   define NV_LIB_OPEN( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL)
20#   define NV_LIB_GET( handle, name ) dlsym( handle, name )
21#   define NV_LIB_CLOSE( name ) dlclose( name )
22#elif NV_PLATFORM == NV_APPLE
23#   include "macUtils.h"
24#   include <dlfcn.h>
25#   define NV_LIB_EXT ".dylib"
26#   define NV_LIB_HANDLE CFBundleRef
27#   define NV_LIB_OPEN( name ) mac_loadExeBundle( name )
28#   define NV_LIB_GET( handle, name ) mac_getBundleSym( handle, name )
29#   define NV_LIB_CLOSE( name ) mac_unloadExeBundle( name )
30#endif
31
32#include "nv/core/logging.hh"
33
34using namespace nv;
35
36library::library()
37    : m_handle( nullptr ), m_name()
38{
39}
40
41void library::open( string_ref name )
42{
43        m_name.assign( name.data(), name.size() );
44        if ( !open() )
45        {
46                m_handle = nullptr;
47                NV_THROW( library_error, "Can't load library!", name.data() );
48        }
49}
50
51bool nv::library::try_open( string_ref name )
52{
53        m_name.assign( name.data(), name.size() );
54        if ( !open() )
55        {
56                m_handle = nullptr;
57                return false;
58        }
59        return true;
60}
61
62string_ref library::get_name() const
63{
64    return string_ref( m_name );
65}
66
67bool library::open( )
68{
69    if ( m_handle != NULL )
70    {
71        return true;
72    }
73    NV_LOG_NOTICE( "library : loading '", m_name, "'..." );
74
75        std::string name = m_name;
76        string_ref ext( NV_LIB_EXT );
77
78        if ( name.length() < ext.length() || name.substr( name.length() - ext.length(), ext.length() ) != ext )
79    {
80        name.append( ext.data(), ext.length() );
81    }
82
83    m_handle = (void*)NV_LIB_OPEN( name.c_str() );
84
85    if ( m_handle == NULL )
86    {
87                NV_LOG_NOTICE( "library : '", name, "' failed to open." );
88                return false;
89    }
90    NV_LOG_NOTICE( "library : '", name, "' loaded." );
91        return true;
92}
93
94void* library::get( string_ref symbol )
95{
96        void* result = (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.data() );
97    if ( !result )
98    {
99        NV_THROW( library_error, "Can't find symbol " + std::string(symbol.data(),symbol.size()) + "!", m_name );
100    }
101        return result;
102}
103
104void* nv::library::try_get( string_ref symbol )
105{
106        return (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.data() );
107}
108
109bool library::is_open() const
110{
111        return m_handle != nullptr;
112}
113
114void library::close()
115{
116    if ( NV_LIB_CLOSE( (NV_LIB_HANDLE)m_handle ) )
117    {
118        NV_LOG_ERROR( "library : can't close library '", m_name, "'!" );
119    }
120    m_handle = NULL;
121}
122
123library::~library()
124{
125    if ( m_handle != NULL )
126    {
127        close();
128    }
129}
130
131std::string library::get_error()
132{
133#if NV_PLATFORM == NV_WINDOWS
134    // We do hate WinAPI for code like this, don't we?
135    LPTSTR buffer = NULL;
136    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
137        NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buffer, 0, NULL );
138    std::string msg( (char*)buffer );
139    LocalFree( buffer );
140    return msg;
141#elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
142    return string(dlerror());
143#else
144    return string("");
145#endif
146}
Note: See TracBrowser for help on using the repository browser.