1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ruby_data.h

65 lines
1.2 KiB
C++

#ifndef __ruby_data_h__
#define __ruby_data_h__
#include <ruby.h>
#include <stdio.h>
#include <string>
template<class T, class R> class RubyDataSource {
T dest;
public:
RubyDataSource() {}
~RubyDataSource() {}
R push(VALUE& value, const char* name=0) {
switch (TYPE(value)) {
case T_FIXNUM:
return dest.pushInt(FIX2INT(value), name);
case T_FLOAT:
return dest.pushDouble(NUM2DBL(value), name);
case T_STRING:
return dest.pushString(RSTRING(value)->ptr, name);
case T_NIL:
return dest.pushNull(name);
case T_TRUE:
return dest.pushBool(true, name);
case T_FALSE:
return dest.pushBool(false, name);
}
return false;
}
};
class RubyDest {
public:
RubyDest();
~RubyDest();
VALUE pushString(std::string value, const char* name=0) {
return rb_str_new2(value.c_str());
}
VALUE pushInt(int64_t value, const char* name=0) {
return INT2FIX(value);
}
VALUE pushDouble(double value, const char* name=0) {
return rb_float_new(value);
}
VALUE pushBool(bool value, const char* name=0) {
return value ? Qtrue : Qfalse;
}
VALUE pushNull(const char* name=0) {
return Qnil;
}
};
#endif