diff options
author | Chad Versace <chad.versace@linux.intel.com> | 2012-05-31 21:57:40 -0700 |
---|---|---|
committer | Chad Versace <chad.versace@linux.intel.com> | 2012-06-03 16:29:43 -0700 |
commit | 602fc904b0b15d4841c7a40a27d50351e9147296 (patch) | |
tree | 1a6379a2302dc156462b2d4ffb778dee1c350b9b /src/waffle/core/wcore_platform.h | |
parent | 50b0bca22427bf2890ac07cee2b61cfa4fcd8240 (diff) | |
download | waffle-602fc904b0b15d4841c7a40a27d50351e9147296.tar.gz |
all: Replace tagged unions with a more traditional object model
This rewrites the bulk of Waffle's code.
When I first began writing Waffle, I wanted to experiment with
a non-traditional object model that used tagged unions. Very soon I began
to abhor the "innovative" decision. This patch replaces the tagged-union
model with a more traditional object model (as found in the Linux kernel
[1], Google's NaCl, libdrm, and many other places) that uses vtables and
embedded structures.
[1] Neil Brown. LWN, 2011 June 1. Object-oriented design patterns in the kernel.
(Part 1: http://lwn.net/Articles/444910/).
(Part 2: http://lwn.net/Articles/446317/).
As an example of the new object model, below is an outline of how
waffle_window_swap_buffers() is now implemeneted.
// file: waffle_window.c
bool
waffle_window_swap_buffers(struct waffle_window *self)
{
struct wcore_window *wc_self = wcore_window(self); // safe cast
// Check preconditions ...
return wc_self->vtbl->swap_buffers(wc_self);
}
// file: wcore_window.h
struct wcore_window_vtbl {
bool
(*swap_buffers)(struct wcore_window *self);
// More member functions ...
};
struct wcore_window {
const struct wcore_window_vtbl *vtbl;
struct waffle_window {} wfl;
// More members ...
};
// file: glx_window.h
struct glx_window {
struct wcore_window wcore;
// More members ...
};
// file: glx_window.c
static bool
glx_window_swap_buffers(struct wcore_window *wc_self)
{
struct glx_window *self = glx_window(wc_self); // safe cast
// Call glXSwapBuffers ...
return true;
}
static const struct wcore_window_vtbl glx_window_wcore_vtbl = {
.swap_buffers = glx_window_swap_buffers,
// More members ...
};
Signed-off-by: Chad Versace <chad.versace@linux.intel.com>
Diffstat (limited to 'src/waffle/core/wcore_platform.h')
-rw-r--r-- | src/waffle/core/wcore_platform.h | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/src/waffle/core/wcore_platform.h b/src/waffle/core/wcore_platform.h index dbb9237..587b280 100644 --- a/src/waffle/core/wcore_platform.h +++ b/src/waffle/core/wcore_platform.h @@ -23,37 +23,85 @@ // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -/// @defgroup wcore_platform wcore_platform -/// @ingroup wcore -/// -/// @brief Abstract native platform. -/// @{ - -/// @file - #pragma once +#include <assert.h> #include <stdbool.h> #include <stdint.h> -struct native_dispatch; -union native_platform; +struct wcore_config; +struct wcore_config_attrs; +struct wcore_context; +struct wcore_display; +struct wcore_platform; +struct wcore_window; -struct wcore_platform { - /// @brief Each instance has a unique id. - uint64_t id; +struct wcore_platform_vtbl { + bool + (*destroy)(struct wcore_platform *self); + + struct wcore_display* + (*connect_to_display)( + struct wcore_platform *self, + const char *name); + + struct wcore_config* + (*choose_config)( + struct wcore_platform *self, + struct wcore_display *dpy, + const struct wcore_config_attrs *attrs); + + struct wcore_context* + (*create_context)( + struct wcore_platform *self, + struct wcore_config *config, + struct wcore_context *share_ctx); - /// @brief One of WAFFLE_PLATFORM_*. Denotes type of `native`. - int native_tag; + struct wcore_window* + (*create_window)( + struct wcore_platform *self, + struct wcore_config *config, + int width, + int height); - union native_platform *native; - const struct native_dispatch *dispatch; + bool + (*make_current)( + struct wcore_platform *self, + struct wcore_display *dpy, + struct wcore_window *window, + struct wcore_context *ctx); + + void* + (*get_proc_address)( + struct wcore_platform *self, + const char *proc); + + bool + (*dl_can_open)( + struct wcore_platform *self, + int32_t waffle_dl); + + void* + (*dl_sym)( + struct wcore_platform *self, + int32_t waffle_dl, + const char *symbol); }; -struct wcore_platform* -wcore_platform_create(int platform); +struct wcore_platform { + const struct wcore_platform_vtbl *vtbl; +}; -bool -wcore_platform_destroy(struct wcore_platform *self); +static inline bool +wcore_platform_init(struct wcore_platform *self) +{ + assert(self); + return true; +} -/// @} +static inline bool +wcore_platform_teardown(struct wcore_platform *self) +{ + assert(self); + return true; +} |