Skip to content

Instantly share code, notes, and snippets.

@shelling
Forked from hamajyotan/extconf.rb
Created August 3, 2014 14:03
Show Gist options
  • Save shelling/ac13832bedb5acf464de to your computer and use it in GitHub Desktop.
Save shelling/ac13832bedb5acf464de to your computer and use it in GitHub Desktop.
require "mkmf"
$libs += " -lstdc++ "
create_makefile("human")
#include "human.hxx"
Human::Human(const char* name, int age) {
this->_sig = _signature;
this->_name = name;
this->_age = age;
}
std::string Human::toString() const {
std::ostringstream ret;
ret << "Human [name="
<< this->_name
<< ", age="
<< this->_age
<< "]";
return ret.str();
}
std::string Human::greet() const {
std::ostringstream ret;
ret << "My name is "
<< this->_name
<< ". I'm "
<< this->_age
<< " years old.";
return ret.str();
}
#ifndef _INCLUDE_HUMAN_H_
#define _INCLUDE_HUMAN_H_
#include <string>
#include <sstream>
class Human
{
private:
static const int _signature = 0x123f3f7c;
public:
Human(const char* name, int age);
virtual ~Human() {}
bool isLegal() const { return this->_sig == _signature; }
std::string getName() const { return this->_name; }
int getAge() const { return this->_age; }
std::string toString() const;
std::string greet() const;
private:
int _sig;
std::string _name;
int _age;
};
#endif // _INCLUDE_HUMAN_H_
#include <new>
#include "ruby.h"
#include "human.hxx"
// function prototypes
extern "C" void Init_human();
static void wrap_Human_free(Human* p);
static VALUE wrap_Human_alloc(VALUE klass);
static VALUE wrap_Human_init(VALUE self, VALUE _name, VALUE _age);
static VALUE wrap_Human_toString(VALUE self);
static VALUE wrap_Human_greet(VALUE self);
static VALUE wrap_Human_getName(VALUE self);
static VALUE wrap_Human_getAge(VALUE self);
static Human* getHuman(VALUE self);
static Human* getHuman(VALUE self) {
Human* p;
Data_Get_Struct(self, Human, p);
return p;
}
static void wrap_Human_free(Human* p) {
if (p->isLegal()) p->~Human();
ruby_xfree(p);
}
static VALUE wrap_Human_alloc(VALUE klass) {
return Data_Wrap_Struct(klass, NULL, wrap_Human_free, ruby_xmalloc(sizeof(Human)));
}
static VALUE wrap_Human_init(VALUE self, VALUE _name, VALUE _age) {
const char* name = StringValuePtr(_name);
int age = NUM2INT(_age);
Human* p = getHuman(self);
new (p) Human(name, age);
return Qnil;
}
static VALUE wrap_Human_toString(VALUE self) {
return rb_str_new2(getHuman(self)->toString().c_str());
}
static VALUE wrap_Human_greet(VALUE self) {
return rb_str_new2(getHuman(self)->greet().c_str());
}
static VALUE wrap_Human_getName(VALUE self) {
return rb_str_new2(getHuman(self)->getName().c_str());
}
static VALUE wrap_Human_getAge(VALUE self) {
return INT2NUM(getHuman(self)->getAge());
}
void Init_human() {
VALUE c = rb_define_class("Human", rb_cObject);
rb_define_alloc_func(c, wrap_Human_alloc);
rb_define_private_method(c, "initialize", RUBY_METHOD_FUNC(wrap_Human_init), 2);
rb_define_method(c, "inspect", RUBY_METHOD_FUNC(wrap_Human_toString), 0);
rb_define_method(c, "to_s", RUBY_METHOD_FUNC(wrap_Human_toString), 0);
rb_define_method(c, "greet", RUBY_METHOD_FUNC(wrap_Human_greet), 0);
rb_define_method(c, "name", RUBY_METHOD_FUNC(wrap_Human_getName), 0);
rb_define_method(c, "age", RUBY_METHOD_FUNC(wrap_Human_getAge), 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment