]> git.jsancho.org Git - guile-irrlicht.git/commitdiff
Use macros for declaring wrapped types
authorJavier Sancho <jsf@jsancho.org>
Fri, 6 Mar 2020 09:02:58 +0000 (10:02 +0100)
committerJavier Sancho <jsf@jsancho.org>
Fri, 6 Mar 2020 09:02:58 +0000 (10:02 +0100)
src/IGUIEnvironment.cpp
src/IGUIEnvironment.h
src/ISceneManager.cpp
src/ISceneManager.h
src/IVideoDriver.cpp
src/IVideoDriver.h
src/IrrlichtDevice.cpp
src/IrrlichtDevice.h
src/util.h [new file with mode: 0644]

index 0f66f7210b7017803ad4c28ed40584aebc13f3ff..c0a2a06a5ccd5f55ce96c5a1fa9cb7b1be6bbdf9 100644 (file)
@@ -22,6 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #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);
 
 }
index 65fc694bc8ba63af8c8e39f4043c1d816e913416..2ce45e57664aa3dc5c01f5e20d330e83806609a6 100644 (file)
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
+#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
index 164af46047cb31ac7fefae56ebc6c44c4c8a0842..6b24b3dd955f0e65bbbfca3de38af5a5d65e814f 100644 (file)
@@ -22,6 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #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);
 
 }
index 561a8652c81eb5ff870199970b4e038a5e2bba0a..cdd85c96e82bbe0c0aaace388d65ac7b25e5c679 100644 (file)
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
+#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);
 
 }
 
index 6f19e9fe71369ad10d709b8927f86c5575205c90..a784437b693effd52cbbc49adde8924affff3d3e 100644 (file)
@@ -22,6 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #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);
 
 }
index eb91c02da93f132def5143d9bdfd8ea219b54f82..4e9f0326625c8715fd4f1f04fa340baceb75a0eb 100644 (file)
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
+#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);
 
 }
 
index aa492e15da58fa3b9e42f0eada092d8d8e13842b..026df21fa8c889118db96aee51462d323c07fb43 100644 (file)
@@ -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,
index 5d33bfd5bef08b3c482f51ccf370570703905834..09aa2cd6dabf9529e50c727b4ba43a86382e02c8 100644 (file)
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
+#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 (file)
index 0000000..6397b37
--- /dev/null
@@ -0,0 +1,62 @@
+/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
+
+   Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+
+   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
+   <http://www.gnu.org/licenses/>.
+*/
+
+
+#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);               \
+  }