From 03d4e13abc392abec3ef9cb6a97d83a5ae722ca8 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Fri, 6 Mar 2020 10:02:58 +0100 Subject: [PATCH] Use macros for declaring wrapped types --- src/IGUIEnvironment.cpp | 32 +++------------------ src/IGUIEnvironment.h | 12 ++------ src/ISceneManager.cpp | 32 +++------------------ src/ISceneManager.h | 11 ++------ src/IVideoDriver.cpp | 32 +++------------------ src/IVideoDriver.h | 11 ++------ src/IrrlichtDevice.cpp | 31 ++------------------- src/IrrlichtDevice.h | 11 ++------ src/util.h | 62 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 89 insertions(+), 145 deletions(-) create mode 100644 src/util.h diff --git a/src/IGUIEnvironment.cpp b/src/IGUIEnvironment.cpp index 0f66f72..c0a2a06 100644 --- a/src/IGUIEnvironment.cpp +++ b/src/IGUIEnvironment.cpp @@ -22,6 +22,7 @@ #include #include #include "IGUIEnvironment.h" +#include "util.h" extern "C" { @@ -31,33 +32,8 @@ extern "C" { init_gui_environment_type (); } - static SCM gui_environment_type; - - void - init_gui_environment_type (void) - { - SCM name, slots; - scm_t_struct_finalize finalizer; - - name = scm_from_utf8_symbol ("gui_environment"); - slots = scm_list_1 (scm_from_utf8_symbol ("data")); - finalizer = NULL; - - gui_environment_type = - scm_make_foreign_object_type (name, slots, finalizer); - } - - SCM - wrap_gui_environment (irr::gui::IGUIEnvironment* gui_environment) - { - return scm_make_foreign_object_1 (gui_environment_type, gui_environment); - } - - irr::gui::IGUIEnvironment* - unwrap_gui_environment (SCM gui_environment_obj) - { - scm_assert_foreign_object_type (gui_environment_type, gui_environment_obj); - return (irr::gui::IGUIEnvironment*)scm_foreign_object_ref (gui_environment_obj, 0); - } + DEFINE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, "gui-environment", + init_gui_environment_type, + wrap_gui_environment, unwrap_gui_environment); } diff --git a/src/IGUIEnvironment.h b/src/IGUIEnvironment.h index 65fc694..2ce45e5 100644 --- a/src/IGUIEnvironment.h +++ b/src/IGUIEnvironment.h @@ -24,21 +24,15 @@ #include #include +#include "util.h" extern "C" { void init_gui_environment (void); - void - init_gui_environment_type (void); - - SCM - wrap_gui_environment (irr::gui::IGUIEnvironment* gui_environment); - - irr::gui::IGUIEnvironment* - unwrap_gui_environment (SCM gui_environment_obj); - + DECLARE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, init_gui_environment_type, + wrap_gui_environment, unwrap_gui_environment); } #endif diff --git a/src/ISceneManager.cpp b/src/ISceneManager.cpp index 164af46..6b24b3d 100644 --- a/src/ISceneManager.cpp +++ b/src/ISceneManager.cpp @@ -22,6 +22,7 @@ #include #include #include "ISceneManager.h" +#include "util.h" extern "C" { @@ -31,33 +32,8 @@ extern "C" { init_scene_manager_type (); } - static SCM scene_manager_type; - - void - init_scene_manager_type (void) - { - SCM name, slots; - scm_t_struct_finalize finalizer; - - name = scm_from_utf8_symbol ("scene-manager"); - slots = scm_list_1 (scm_from_utf8_symbol ("data")); - finalizer = NULL; - - scene_manager_type = - scm_make_foreign_object_type (name, slots, finalizer); - } - - SCM - wrap_scene_manager (irr::scene::ISceneManager* scene_manager) - { - return scm_make_foreign_object_1 (scene_manager_type, scene_manager); - } - - irr::scene::ISceneManager* - unwrap_scene_manager (SCM scene_manager_obj) - { - scm_assert_foreign_object_type (scene_manager_type, scene_manager_obj); - return (irr::scene::ISceneManager*)scm_foreign_object_ref (scene_manager_obj, 0); - } + DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager", + init_scene_manager_type, + wrap_scene_manager, unwrap_scene_manager); } diff --git a/src/ISceneManager.h b/src/ISceneManager.h index 561a865..cdd85c9 100644 --- a/src/ISceneManager.h +++ b/src/ISceneManager.h @@ -24,20 +24,15 @@ #include #include +#include "util.h" extern "C" { void init_scene_manager (void); - void - init_scene_manager_type (void); - - SCM - wrap_scene_manager (irr::scene::ISceneManager* scene_manager); - - irr::scene::ISceneManager* - unwrap_scene_manager (SCM scene_manager_obj); + DECLARE_WRAPPED_TYPE (irr::scene::ISceneManager*, init_scene_manager_type, + wrap_scene_manager, unwrap_scene_manager); } diff --git a/src/IVideoDriver.cpp b/src/IVideoDriver.cpp index 6f19e9f..a784437 100644 --- a/src/IVideoDriver.cpp +++ b/src/IVideoDriver.cpp @@ -22,6 +22,7 @@ #include #include #include "IVideoDriver.h" +#include "util.h" extern "C" { @@ -31,33 +32,8 @@ extern "C" { init_video_driver_type (); } - static SCM video_driver_type; - - void - init_video_driver_type (void) - { - SCM name, slots; - scm_t_struct_finalize finalizer; - - name = scm_from_utf8_symbol ("video-driver"); - slots = scm_list_1 (scm_from_utf8_symbol ("data")); - finalizer = NULL; - - video_driver_type = - scm_make_foreign_object_type (name, slots, finalizer); - } - - SCM - wrap_video_driver (irr::video::IVideoDriver* driver) - { - return scm_make_foreign_object_1 (video_driver_type, driver); - } - - irr::video::IVideoDriver* - unwrap_video_driver (SCM driver_obj) - { - scm_assert_foreign_object_type (video_driver_type, driver_obj); - return (irr::video::IVideoDriver*)scm_foreign_object_ref (driver_obj, 0); - } + DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver", + init_video_driver_type, + wrap_video_driver, unwrap_video_driver); } diff --git a/src/IVideoDriver.h b/src/IVideoDriver.h index eb91c02..4e9f032 100644 --- a/src/IVideoDriver.h +++ b/src/IVideoDriver.h @@ -24,20 +24,15 @@ #include #include +#include "util.h" extern "C" { void init_video_driver (void); - void - init_video_driver_type (void); - - SCM - wrap_video_driver (irr::video::IVideoDriver* driver); - - irr::video::IVideoDriver* - unwrap_video_driver (SCM driver_obj); + DECLARE_WRAPPED_TYPE (irr::video::IVideoDriver*, init_video_driver_type, + wrap_video_driver, unwrap_video_driver); } diff --git a/src/IrrlichtDevice.cpp b/src/IrrlichtDevice.cpp index aa492e1..026df21 100644 --- a/src/IrrlichtDevice.cpp +++ b/src/IrrlichtDevice.cpp @@ -29,6 +29,7 @@ #include "IrrlichtDevice.h" #include "ISceneManager.h" #include "IVideoDriver.h" +#include "util.h" extern "C" { @@ -43,34 +44,8 @@ extern "C" { scm_c_define_gsubr ("set-window-caption!", 2, 0, 0, (scm_t_subr)irr_setWindowCaption); } - static SCM device_type; - - void - init_device_type (void) - { - SCM name, slots; - scm_t_struct_finalize finalizer; - - name = scm_from_utf8_symbol ("device"); - slots = scm_list_1 (scm_from_utf8_symbol ("data")); - finalizer = NULL; - - device_type = - scm_make_foreign_object_type (name, slots, finalizer); - } - - SCM - wrap_device (irr::IrrlichtDevice* device) - { - return scm_make_foreign_object_1 (device_type, device); - } - - irr::IrrlichtDevice* - unwrap_device (SCM device_obj) - { - scm_assert_foreign_object_type (device_type, device_obj); - return (irr::IrrlichtDevice*)scm_foreign_object_ref (device_obj, 0); - } + DEFINE_WRAPPED_TYPE (irr::IrrlichtDevice*, "device", + init_device_type, wrap_device, unwrap_device); SCM irr_createDevice (SCM deviceType, diff --git a/src/IrrlichtDevice.h b/src/IrrlichtDevice.h index 5d33bfd..09aa2cd 100644 --- a/src/IrrlichtDevice.h +++ b/src/IrrlichtDevice.h @@ -24,20 +24,15 @@ #include #include +#include "util.h" extern "C" { void init_device (void); - void - init_device_type (void); - - SCM - wrap_device (irr::IrrlichtDevice* device); - - irr::IrrlichtDevice* - unwrap_device (SCM device_obj); + DECLARE_WRAPPED_TYPE (irr::IrrlichtDevice*, init_device_type, + wrap_device, unwrap_device); SCM irr_createDevice (SCM deviceType, diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..6397b37 --- /dev/null +++ b/src/util.h @@ -0,0 +1,62 @@ +/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine + + Copyright (C) 2020 Javier Sancho + + This file is part of guile-irrlicht. + + guile-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + guile-irrlicht is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + + +#define DECLARE_WRAPPED_TYPE(TYPE, INIT, WRAP, UNWRAP) \ + void \ + INIT (void); \ + \ + SCM \ + WRAP (TYPE foreign_obj); \ + \ + TYPE \ + UNWRAP (SCM wrapped_obj); + + +#define DEFINE_WRAPPED_TYPE(TYPE, PRINT_NAME, INIT, WRAP, UNWRAP) \ + static SCM wrapped_type; \ + \ + void \ + INIT (void) \ + { \ + SCM name, slots; \ + scm_t_struct_finalize finalizer; \ + \ + name = scm_from_utf8_symbol (PRINT_NAME); \ + slots = scm_list_1 (scm_from_utf8_symbol ("data")); \ + finalizer = NULL; \ + \ + wrapped_type = \ + scm_make_foreign_object_type (name, slots, finalizer); \ + } \ + \ + SCM \ + WRAP (TYPE foreign_obj) \ + { \ + return scm_make_foreign_object_1 (wrapped_type, foreign_obj); \ + } \ + \ + TYPE \ + UNWRAP (SCM wrapped_obj) \ + { \ + scm_assert_foreign_object_type (wrapped_type, wrapped_obj); \ + return (TYPE)scm_foreign_object_ref (wrapped_obj, 0); \ + } -- 2.39.5