]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_dynamic_module.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_dynamic_module.h
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.h
28 //
29 // TODO: Add Linux support!
30 //
31 #pragma once
32
33 #include "vogl_core.h"
34
35 namespace vogl
36 {
37     typedef void *dynamic_module_handle_t;
38
39     dynamic_module_handle_t load_dynamic_module(const char *pModule_filename);
40     void unload_dynamic_module(dynamic_module_handle_t handle);
41     void *get_dynamic_module_proc_address(dynamic_module_handle_t handle, const char *pFunc_name);
42     void *lookup_dynamic_module_func(const char *pModule_filename, const char *pFunc_name);
43
44     class dynamic_module_cache
45     {
46     public:
47         dynamic_module_cache();
48         ~dynamic_module_cache();
49
50         void unload_all();
51
52         bool is_module_loaded(const char *pModule_filename);
53         dynamic_module_handle_t get_module(const char *pModule_filename);
54
55         static dynamic_module_cache &get_global()
56         {
57             static dynamic_module_cache s_module_cache;
58             return s_module_cache;
59         }
60
61     private:
62         struct loaded_module
63         {
64             dynamic_string m_filename;
65             dynamic_module_handle_t m_handle;
66         };
67
68         vogl::vector<loaded_module> m_modules;
69     };
70
71     template <typename dynamic_func_def_t>
72     class dynamic_function
73     {
74     public:
75         dynamic_function()
76             : m_pFunc(NULL)
77         {
78         }
79         dynamic_function(const char *pModule_filename, const char *pFunc_name)
80             : m_pFunc(NULL)
81         {
82             lookup(pModule_filename, pFunc_name);
83         }
84
85         inline void clear()
86         {
87             m_pFunc = NULL;
88         }
89
90         inline dynamic_func_def_t get_ptr() const
91         {
92             return m_pFunc;
93         }
94         inline operator dynamic_func_def_t() const
95         {
96             return m_pFunc;
97         }
98
99         inline void set(dynamic_func_def_t ptr)
100         {
101             m_pFunc = ptr;
102         }
103
104         inline operator bool() const
105         {
106             return m_pFunc != NULL;
107         }
108         inline bool operator!() const
109         {
110             return m_pFunc == NULL;
111         }
112
113         inline bool lookup(const char *pModule_filename, const char *pFunc_name)
114         {
115             if (!m_pFunc)
116                 m_pFunc = (dynamic_func_def_t)lookup_dynamic_module_func(pModule_filename, pFunc_name);
117             return m_pFunc != NULL;
118         }
119
120     private:
121         dynamic_func_def_t m_pFunc;
122     };
123
124 } // namespace vogl