]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_dynamic_module.cpp
Initial vogl checkin
[vogl] / src / voglcore / vogl_dynamic_module.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_dynamic_module.cpp
28 //
29 // TODO: Add Linux support!
30 //
31 #include "vogl_core.h"
32 #include "vogl_dynamic_module.h"
33
34 #ifdef VOGL_USE_WIN32_API
35 #include "vogl_winhdr.h"
36 #endif
37
38 namespace vogl
39 {
40     dynamic_module_cache g_dynamic_module_cache;
41
42     dynamic_module_handle_t load_dynamic_module(const char *pFilename)
43     {
44 #if defined(VOGL_ANSI_CPLUSPLUS)
45         VOGL_NOTE_UNUSED(pFilename);
46         return NULL;
47 #elif defined(VOGL_USE_WIN32_API)
48         return (dynamic_module_handle_t)LoadLibraryA(pFilename);
49 #else
50         VOGL_NOTE_UNUSED(pFilename);
51         printf("Implement me: load_dynamic_module()\n");
52         return NULL;
53 #endif
54     }
55
56     void unload_dynamic_module(dynamic_module_handle_t handle)
57     {
58 #if defined(VOGL_ANSI_CPLUSPLUS)
59         VOGL_NOTE_UNUSED(handle);
60 #elif defined(VOGL_USE_WIN32_API)
61         if (handle)
62         {
63             FreeLibrary((HMODULE)handle);
64         }
65 #else
66         VOGL_NOTE_UNUSED(handle);
67         printf("Implement me: unload_dynamic_module()\n");
68 #endif
69     }
70
71     void *get_dynamic_module_proc_address(dynamic_module_handle_t handle, const char *pFunc_name)
72     {
73         if (!handle)
74             return NULL;
75 #if defined(VOGL_ANSI_CPLUSPLUS)
76         VOGL_NOTE_UNUSED(handle);
77         VOGL_NOTE_UNUSED(pFunc_name);
78         return NULL;
79 #elif defined(VOGL_USE_WIN32_API)
80         VOGL_NOTE_UNUSED(handle);
81         return GetProcAddress((HMODULE)handle, pFunc_name);
82 #else
83         VOGL_NOTE_UNUSED(handle);
84         VOGL_NOTE_UNUSED(pFunc_name);
85         printf("Implement me: get_dynamic_module_proc_address()\n");
86         return NULL;
87 #endif
88     }
89
90     dynamic_module_cache::dynamic_module_cache()
91     {
92     }
93
94     dynamic_module_cache::~dynamic_module_cache()
95     {
96     }
97
98     void dynamic_module_cache::unload_all()
99     {
100         for (uint i = 0; i < m_modules.size(); i++)
101             unload_dynamic_module(m_modules[i].m_handle);
102         m_modules.clear();
103     }
104
105     bool dynamic_module_cache::is_module_loaded(const char *pFilename)
106     {
107         for (uint i = 0; i < m_modules.size(); i++)
108             if (m_modules[i].m_filename == pFilename)
109                 return true;
110         return false;
111     }
112
113     dynamic_module_handle_t dynamic_module_cache::get_module(const char *pFilename)
114     {
115         for (uint i = 0; i < m_modules.size(); i++)
116             if (m_modules[i].m_filename == pFilename)
117                 return m_modules[i].m_handle;
118
119         dynamic_module_handle_t handle = load_dynamic_module(pFilename);
120         if (!handle)
121             return NULL;
122
123         loaded_module *pModule = m_modules.enlarge(1);
124         pModule->m_filename = pFilename;
125         pModule->m_handle = handle;
126         return handle;
127     }
128
129     void *lookup_dynamic_module_func(const char *pModule_filename, const char *pFunc_name)
130     {
131         dynamic_module_handle_t mod_handle = dynamic_module_cache::get_global().get_module(pModule_filename);
132         if (!mod_handle)
133             return NULL;
134
135         return get_dynamic_module_proc_address(mod_handle, pFunc_name);
136     }
137
138 } // namespace vogl