2009-12-13 14:44:28 -05:00
|
|
|
#ifndef __convert_ruby_h__
|
|
|
|
#define __convert_ruby_h__
|
2009-10-15 23:23:53 -04:00
|
|
|
|
|
|
|
#include <ruby.h>
|
2009-10-16 10:32:20 -04:00
|
|
|
#include <string>
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* A RubyValueSource takes a Destination class and a return
|
|
|
|
* type as a template argument, it converts a Ruby Value to
|
|
|
|
* the appropriate intermediate type and feed it to an
|
|
|
|
* instance Destination type.
|
|
|
|
*
|
|
|
|
* The destination type should have a default constructor,
|
|
|
|
* and provide the methods detailed in data_conversion.txt
|
|
|
|
* \tparam DEST destination type for the converted data
|
|
|
|
* \tparam RET is the return type of T's methods
|
|
|
|
*/
|
|
|
|
template<class DEST, class RET> class RubyValueSource {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An instance of the destination class
|
|
|
|
*/
|
|
|
|
DEST dest;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RubyValueSource() {}
|
|
|
|
~RubyValueSource() {}
|
|
|
|
|
|
|
|
|
2009-12-13 14:44:28 -05:00
|
|
|
RET operator() (VALUE& value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
switch (TYPE(value)) {
|
|
|
|
case T_FIXNUM:
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushInt(FIX2INT(value));
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_FLOAT:
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushDouble(NUM2DBL(value));
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_STRING:
|
2009-12-13 11:07:34 -05:00
|
|
|
return convertString(value);
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_NIL:
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushNull();
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_TRUE:
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushBool(true);
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_FALSE:
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushBool(false);
|
2009-10-17 23:45:02 -04:00
|
|
|
}
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushUndefined();
|
2009-10-17 23:45:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-12-13 11:07:34 -05:00
|
|
|
RET convertString(VALUE& value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
std::string stringValue(RSTRING(value)->ptr);
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushString(stringValue);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
2009-12-13 14:44:28 -05:00
|
|
|
* A RubyValueDest class receives on of the types defined in
|
2009-10-17 23:45:02 -04:00
|
|
|
* data_conversion.txt, and converts it to the appropriate
|
|
|
|
* Ruby VALUE.
|
|
|
|
*/
|
2009-12-13 14:44:28 -05:00
|
|
|
class RubyValueDest {
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
public:
|
2009-12-13 14:44:28 -05:00
|
|
|
RubyValueDest();
|
|
|
|
~RubyValueDest();
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushString(const std::string& value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
return rb_str_new2(value.c_str());
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushInt(int64_t value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
return INT2FIX(value);
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushDouble(double value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
return rb_float_new(value);
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushBool(bool value) {
|
2009-10-17 23:45:02 -04:00
|
|
|
return value ? Qtrue : Qfalse;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushNull() {
|
2009-10-17 23:45:02 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
VALUE pushUndefined() {
|
2009-10-17 23:45:02 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|