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
|
|
|
|
*/
|
2010-01-09 17:13:03 -05:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
template<class DEST, class RET> class RubyValueSource {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An instance of the destination class
|
|
|
|
*/
|
|
|
|
DEST dest;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RubyValueSource() {}
|
|
|
|
~RubyValueSource() {}
|
|
|
|
|
|
|
|
|
2010-01-09 17:13:03 -05:00
|
|
|
bool operator() (VALUE& value, RET& result) {
|
2009-10-17 23:45:02 -04:00
|
|
|
switch (TYPE(value)) {
|
|
|
|
case T_FIXNUM:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = dest.pushInt(FIX2INT(value));
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_FLOAT:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = dest.pushDouble(NUM2DBL(value));
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_STRING:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = convertString(value);
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_NIL:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = dest.pushNull();
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_TRUE:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = dest.pushBool(true);
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
case T_FALSE:
|
2010-01-09 17:13:03 -05:00
|
|
|
result = dest.pushBool(false);
|
|
|
|
return true;
|
2009-10-17 23:45:02 -04:00
|
|
|
}
|
2010-01-09 17:13:03 -05:00
|
|
|
return false;
|
2009-10-17 23:45:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-12-13 11:07:34 -05:00
|
|
|
RET convertString(VALUE& value) {
|
2010-02-11 18:14:47 -05:00
|
|
|
std::string stringValue(RSTRING_PTR(value));
|
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
|