Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ext/libcdb/: Even more DRYing ;)

  • Loading branch information...
commit 677d96cf75b2658cadc7f061374791caf6719b5c 1 parent e22a023
@blackwinter authored
View
14 ext/libcdb/ruby_cdb.h
@@ -7,14 +7,13 @@
#define RCDB_GET_FD(fptr) fileno((fptr)->f)
#endif
-#define RCDB_GET_STRUCT(what, _struct, obj, ptr) {\
+#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 \
@@ -45,6 +44,17 @@ rcdb_##what##er_alloc(VALUE klass) {\
rb_sys_fail(0);\
}
+#define RCDB_RAISE_ARGS(min, max) \
+ rb_raise(rb_eArgError,\
+ "wrong number of arguments (%d for " #min "-" #max ")", argc);
+
+#define RCDB_RETURN_ENUMERATOR(self, argc, argv, max) \
+ if (argc > max) {\
+ RCDB_RAISE_ARGS(0, max)\
+ }\
+\
+ RETURN_ENUMERATOR(self, argc, argv)
+
#define RCDB_DEFINE_INSPECT(what) \
static VALUE \
rcdb_##what##er_inspect(VALUE self) {\
View
27 ext/libcdb/ruby_cdb_reader.c
@@ -62,23 +62,13 @@ _rcdb_reader_iter_aset(VALUE pair, VALUE hash) {
/* Helper method */
static VALUE
_rcdb_reader_break_equal(VALUE val, VALUE ary) {
- if (RCDB_READER_EQUAL(val)) {
- rb_ary_store(ary, 0, Qtrue);
- rb_iter_break();
- }
-
- return Qnil;
+ RCDB_READER_BREAK_EQUAL(val, Qtrue)
}
/* Helper method */
static VALUE
_rcdb_reader_break_equal2(VALUE pair, VALUE ary) {
- if (RCDB_READER_EQUAL(rb_ary_entry(pair, 1))) {
- rb_ary_store(ary, 0, rb_ary_entry(pair, 0));
- rb_iter_break();
- }
-
- return Qnil;
+ RCDB_READER_BREAK_EQUAL(rb_ary_entry(pair, 1), rb_ary_entry(pair, 0))
}
/* Helper method */
@@ -157,12 +147,7 @@ rcdb_reader_each(int argc, VALUE *argv, VALUE self) {
unsigned cdbp;
VALUE key;
- if (argc > 1) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-1)", argc);
- }
-
- RETURN_ENUMERATOR(self, argc, argv);
-
+ RCDB_RETURN_ENUMERATOR(self, argc, argv, 1);
RCDB_READER_GET(self, cdb);
if (rb_scan_args(argc, argv, "01", &key) == 1 && !NIL_P(key)) {
@@ -203,11 +188,7 @@ rcdb_reader_each_dump(int argc, VALUE *argv, VALUE self) {
VALUE key, args = rb_ary_new3(1, self), ary = rb_ary_new();
VALUE (*block)(ANYARGS) = _rcdb_reader_yield_dump;
- if (argc > 1) {
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-1)", argc);
- }
-
- RETURN_ENUMERATOR(self, argc, argv);
+ RCDB_RETURN_ENUMERATOR(self, argc, argv, 1);
if (rb_scan_args(argc, argv, "01", &key) == 1 && !NIL_P(key)) {
rb_ary_push(ary, key);
View
9 ext/libcdb/ruby_cdb_reader.h
@@ -26,8 +26,13 @@ _rcdb_reader_call_##iter(VALUE args) {\
RCDB_READER_ITERATE1(method, block, arg1)\
return rb_ary_entry(arg, 0);
-#define RCDB_READER_EQUAL(val) \
- RTEST(rb_funcall((val), rb_intern("=="), 1, rb_ary_entry(ary, 1)))
+#define RCDB_READER_BREAK_EQUAL(val, ret) \
+ if (RTEST(rb_funcall(val, rb_intern("=="), 1, rb_ary_entry(ary, 1)))) {\
+ rb_ary_store(ary, 0, ret);\
+ rb_iter_break();\
+ }\
+\
+ return Qnil;
#define RCDB_READER_DEFINE_READ(what) \
static VALUE \
View
2  ext/libcdb/ruby_cdb_writer.c
@@ -154,7 +154,7 @@ rcdb_writer_put(int argc, VALUE *argv, VALUE self, enum cdb_put_mode mode) {
rcdb_writer_put_value(cdbm, argv[0], argv[1], mode);
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-2)", argc);
+ RCDB_RAISE_ARGS(1, 2)
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.