Permalink
Browse files

ext/libcdb/: Avoid code duplication like the plague.

Probably went a bit overboard, though ;)
  • Loading branch information...
1 parent 391bdf5 commit a32da2d784e048abb5ff5007e3ba7e7bca9a3751 @blackwinter committed Jan 20, 2012
Showing with 153 additions and 228 deletions.
  1. +53 −2 ext/libcdb/ruby_cdb.h
  2. +57 −163 ext/libcdb/ruby_cdb_reader.c
  3. +35 −15 ext/libcdb/ruby_cdb_reader.h
  4. +7 −40 ext/libcdb/ruby_cdb_writer.c
  5. +1 −8 ext/libcdb/ruby_cdb_writer.h
View
@@ -2,11 +2,62 @@
#define __RUBY_CDB_H__
#ifdef HAVE_RUBY_IO_H
-#define GetFileFD(fptr) (fptr)->fd
+#define RCDB_GET_FD(fptr) (fptr)->fd
#else
-#define GetFileFD(fptr) fileno((fptr)->f)
+#define RCDB_GET_FD(fptr) fileno((fptr)->f)
#endif
+#define RCDB_GET_STRUCT(what, _struct, obj, ptr) {\
+ if (RTEST(rcdb_##what##er_closed_p(obj))) {\
+ rb_raise(rb_eIOError, "closed stream");\
+ }\
+ else {\
+ Data_Get_Struct((obj), struct _struct, (ptr));\
+ }\
+}
+
+#define RCDB_DEFINE_ALLOC(what, _struct) \
+static void \
+rcdb_##what##er_free(void *ptr) {\
+ free(ptr);\
+}\
+\
+static VALUE \
+rcdb_##what##er_alloc(VALUE klass) {\
+ struct _struct *ptr = ALLOC_N(struct _struct, 1);\
+ return Data_Wrap_Struct(klass, NULL, rcdb_##what##er_free, ptr);\
+}
+
+#define RCDB_INITIALIZE(what, WHAT, _struct, init) \
+ struct _struct *ptr = NULL;\
+ rb_io_t *fptr;\
+\
+ Check_Type(io, T_FILE);\
+ GetOpenFile(io, fptr);\
+\
+ rb_io_check_##what##able(fptr);\
+ rb_iv_set(self, "@io", io);\
+ rb_iv_set(self, "closed", Qfalse);\
+\
+ RCDB_##WHAT##ER_GET(self, ptr);\
+\
+ if (cdb_##init(ptr, RCDB_GET_FD(fptr)) == -1) {\
+ rb_sys_fail(0);\
+ }
+
+#define RCDB_DEFINE_INSPECT(what) \
+static VALUE \
+rcdb_##what##er_inspect(VALUE self) {\
+ VALUE str = rb_call_super(0, NULL);\
+\
+ if (RTEST(rcdb_##what##er_closed_p(self))) {\
+ rb_funcall(str,\
+ rb_intern("insert"), 2, INT2FIX(-2), rb_str_new2(" (closed)"));\
+ }\
+\
+ return str;\
+}
+
VALUE cCDB;
void rcdb_init_cdb(void);
Oops, something went wrong.

0 comments on commit a32da2d

Please sign in to comment.