2009-10-15 23:23:53 -04:00
|
|
|
#ifndef __ruby_data_h__
|
|
|
|
#define __ruby_data_h__
|
|
|
|
|
|
|
|
#include <ruby.h>
|
2009-10-16 10:32:20 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#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() {}
|
|
|
|
|
|
|
|
|
|
|
|
RET 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 convertString(value, 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 dest.pushUndefined(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RET convertString(VALUE& value, const char* name=0) {
|
|
|
|
std::string stringValue(RSTRING(value)->ptr);
|
|
|
|
return dest.pushString(stringValue, name);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* A RubyDest class receives on of the types defined in
|
|
|
|
* data_conversion.txt, and converts it to the appropriate
|
|
|
|
* Ruby VALUE.
|
|
|
|
*/
|
2009-10-15 23:23:53 -04:00
|
|
|
class RubyDest {
|
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
public:
|
|
|
|
RubyDest();
|
|
|
|
~RubyDest();
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushString(const std::string& value, const char* name=0) {
|
|
|
|
return rb_str_new2(value.c_str());
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushInt(int64_t value, const char* name=0) {
|
|
|
|
return INT2FIX(value);
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushDouble(double value, const char* name=0) {
|
|
|
|
return rb_float_new(value);
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushBool(bool value, const char* name=0) {
|
|
|
|
return value ? Qtrue : Qfalse;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushNull(const char* name=0) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
VALUE pushUndefined(const char* name=0) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|