#include "rays/ruby/point.h" #include #include "defs.h" using namespace Rucy; using Rays::coord; static Class cPoint; namespace Rays { Class point_class () { return cPoint; } }// Rays namespace Rucy { Value value (const Rays::Point& obj) { return new_type(cPoint, new Rays::Point(obj)); } Value value (const Rays::Point* obj) { return obj ? value(*obj) : nil(); } }// Rucy #define THIS to(self) #define CHECK RUCY_CHECK_OBJ(self, Rays::Point, cPoint) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE initialize(VALUE self) { RUCY_CHECK_OBJ(self, Rays::Point, cPoint); if (argc != 0 && argc != 1 && argc != 2 && argc != 3) arg_count_error("Point#initialize", argc, 0, 1, 2, 3); if (argc == 0) return self; switch (argc) { case 1: *THIS = Rays::Point(to(argv[0])); break; case 2: *THIS = Rays::Point(to(argv[0]), to(argv[1])); break; case 3: *THIS = Rays::Point( to(argv[0]), to(argv[1]), to(argv[2])); break; } return self; } static VALUE initialize_copy(VALUE self, VALUE obj) { RUCY_CHECK_OBJ(self, Rays::Point, cPoint); Rays::Point* point = to(obj); if (!point) argument_error(); *THIS = *point; return self; } static VALUE set_x(VALUE self, VALUE x) { CHECK; return value(THIS->x = to(x)); } static VALUE get_x(VALUE self) { CHECK; return value(THIS->x); } static VALUE set_y(VALUE self, VALUE y) { CHECK; return value(THIS->y = to(y)); } static VALUE get_y(VALUE self) { CHECK; return value(THIS->y); } static VALUE set_z(VALUE self, VALUE z) { CHECK; return value(THIS->z = to(z)); } static VALUE get_z(VALUE self) { CHECK; return value(THIS->z); } void Init_point () { Module mRays = rb_define_module("Rays"); cPoint = rb_define_class_under(mRays, "Point", rb_cObject); rb_define_alloc_func(cPoint, alloc); rb_define_private_method(cPoint, "initialize", RUBY_METHOD_FUNC(initialize), -1); rb_define_private_method(cPoint, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1); rb_define_method(cPoint, "x=", RUBY_METHOD_FUNC(set_x), 1); rb_define_method(cPoint, "x", RUBY_METHOD_FUNC(get_x), 0); rb_define_method(cPoint, "y=", RUBY_METHOD_FUNC(set_y), 1); rb_define_method(cPoint, "y", RUBY_METHOD_FUNC(get_y), 0); rb_define_method(cPoint, "z=", RUBY_METHOD_FUNC(set_z), 1); rb_define_method(cPoint, "z", RUBY_METHOD_FUNC(get_z), 0); }