summaryrefslogtreecommitdiffhomepage
path: root/src/string.c
diff options
context:
space:
mode:
authorMitchell Blank Jr <[email protected]>2012-05-20 09:43:14 -0700
committerMitchell Blank Jr <[email protected]>2012-05-20 09:43:14 -0700
commit9e336b00e5934dbe583edfd1db6370fa4e78745d (patch)
tree08448056bc440124baab552e716c383f5e3f5ef7 /src/string.c
parentec8f97d2a6facab56ff78cc62ebf1928d06bed4a (diff)
downloadmruby-9e336b00e5934dbe583edfd1db6370fa4e78745d.tar.gz
mruby-9e336b00e5934dbe583edfd1db6370fa4e78745d.zip
More C++ compilability work: mrb_obj_alloc void* conversions
One of the biggest set of changes needed to make C++ compile, is that you can't autoconvert "void*" to a different pointer type without a cast (you can of course, convert pointers *to* "void*"!) For the first part, convert the users of "mrb_obj_alloc". Since it has to return something, make it RBasic* (that's what mrb_obj_alloc() is operating on anyway). This way, even in C you'll get a warning if you don't cast it. For places where there are a lot of similar calls to mrb_obj_alloc(), this can be easily hidden through a macro. I did this in string.c: #define mrb_obj_alloc_string(mrb) ((struct RString *) mrb_obj_alloc((mrb), MRB_TT_STRING, (mrb)->string_class)) I also updated the mrb_object() macro to also return a RBasic* -- my previous commit changed that from "void*" -> "RObject*", but I figure it should be consistent with mrb_obj_alloc()
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/string.c b/src/string.c
index 6e72f7267..b3ef156eb 100644
--- a/src/string.c
+++ b/src/string.c
@@ -291,12 +291,14 @@ mrb_str_capacity(mrb_value str)
}
#endif //INCLUDE_ENCODING
+#define mrb_obj_alloc_string(mrb) ((struct RString *) mrb_obj_alloc((mrb), MRB_TT_STRING, (mrb)->string_class))
+
static inline mrb_value
str_alloc(mrb_state *mrb)
{
struct RString* s;
- s = mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class);
+ s = mrb_obj_alloc_string(mrb);
//NEWOBJ(str, struct RString);
//OBJSETUP(str, klass, T_STRING);
@@ -480,7 +482,7 @@ str_new4(mrb_state *mrb, mrb_value str)
{
mrb_value str2;
- str2 = mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class));
+ str2 = mrb_obj_value(mrb_obj_alloc_string(mrb));
RSTRING(str2)->len = RSTRING_LEN(str);
RSTRING(str2)->buf = RSTRING_PTR(str);
@@ -537,7 +539,7 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
{
struct RString *s;
- s = mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class);
+ s = mrb_obj_alloc_string(mrb);
if (capa < STR_BUF_MIN_SIZE) {
capa = STR_BUF_MIN_SIZE;
@@ -607,7 +609,7 @@ mrb_str_new(mrb_state *mrb, const char *p, size_t len)
if (len == 0) {
return mrb_str_buf_new(mrb, len);
}
- s = mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class);
+ s = mrb_obj_alloc_string(mrb);
s->buf = mrb_malloc(mrb, len+1);
if (p) {
memcpy(s->buf, p, len);
@@ -655,7 +657,7 @@ mrb_str_new_cstr(mrb_state *mrb, const char *p)
struct RString *s;
size_t len = strlen(p);
- s = mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class);
+ s = mrb_obj_alloc_string(mrb);
s->buf = mrb_malloc(mrb, len+1);
memcpy(s->buf, p, len);
s->buf[len] = 0;
@@ -1420,7 +1422,7 @@ mrb_str_dup(mrb_state *mrb, mrb_value str)
struct RString *s = mrb_str_ptr(str);
struct RString *dup;
- dup = mrb_obj_alloc(mrb, MRB_TT_STRING, mrb->string_class);
+ dup = mrb_obj_alloc_string(mrb);
dup->buf = mrb_malloc(mrb, s->len+1);
if (s->buf) {
memcpy(dup->buf, s->buf, s->len);