Go to the documentation of this file.00001 #include "typelib.hh"
00002 #include <ruby/version.h>
00003 #include <ruby/encoding.h>
00004
00005 #include <typelib/typemodel.hh>
00006
00007 using namespace Typelib;
00008 using namespace typelib_ruby;
00009
00010 VALUE typelib_ruby::cMetaData;
00011 rb_encoding* enc_utf8;
00012
00013 VALUE cxx2rb::metadata_wrap(MetaData& metadata)
00014 {
00015 return Data_Wrap_Struct(cMetaData, 0, 0, &metadata);
00016 }
00017
00018 static VALUE metadata_include_p(VALUE self, VALUE key)
00019 {
00020 MetaData& metadata = rb2cxx::object<MetaData>(self);
00021 return metadata.include(StringValuePtr(key)) ? Qtrue : Qfalse;
00022 }
00023
00024 static VALUE metadata_get(VALUE self, VALUE key)
00025 {
00026 MetaData& metadata = rb2cxx::object<MetaData>(self);
00027 MetaData::Values v = metadata.get(StringValuePtr(key));
00028
00029 VALUE result = rb_ary_new();
00030 for (MetaData::Values::const_iterator it = v.begin(); it != v.end(); ++it)
00031 rb_ary_push(result, rb_enc_str_new(it->c_str(), it->length(), enc_utf8));
00032 return result;
00033 }
00034
00035 static VALUE metadata_keys(VALUE self)
00036 {
00037 MetaData& metadata = rb2cxx::object<MetaData>(self);
00038 MetaData::Map const& map = metadata.get();
00039
00040 VALUE result = rb_ary_new();
00041 for (MetaData::Map::const_iterator it = map.begin(); it != map.end(); ++it)
00042 rb_ary_push(result, rb_enc_str_new(it->first.c_str(), it->first.length(), enc_utf8));
00043 return result;
00044 }
00045
00046 static VALUE metadata_add(int argc, VALUE* argv, VALUE self)
00047 {
00048 VALUE key;
00049 VALUE values;
00050 rb_scan_args(argc, argv, "1*", &key, &values);
00051
00052 MetaData& metadata = rb2cxx::object<MetaData>(self);
00053 std::string rb_key(StringValuePtr(key));
00054
00055 MetaData::Values new_values;
00056 long length = RARRAY_LEN(values);
00057 for (long i = 0; i < length; ++i)
00058 {
00059 VALUE val = rb_ary_entry(values, i);
00060 new_values.insert(StringValuePtr(val));
00061 }
00062 metadata.add(rb_key, new_values);
00063 return Qnil;
00064 }
00065
00066 static VALUE metadata_clear(int argc, VALUE* argv, VALUE self)
00067 {
00068 MetaData& metadata = rb2cxx::object<MetaData>(self);
00069 if (argc == 0)
00070 metadata.clear();
00071 else if (argc == 1)
00072 metadata.clear(StringValuePtr(argv[0]));
00073 return Qnil;
00074 }
00075
00076 static void metadata_free(void* metadata)
00077 {
00078 delete reinterpret_cast<MetaData*>(metadata);
00079 }
00080
00081 static VALUE metadata_alloc(VALUE klass)
00082 {
00083 MetaData* metadata = new MetaData;
00084 return Data_Wrap_Struct(cMetaData, 0, metadata_free, metadata);
00085 }
00086
00087 void typelib_ruby::Typelib_init_metadata()
00088 {
00089 VALUE mTypelib = rb_define_module("Typelib");
00090 cMetaData = rb_define_class_under(mTypelib, "MetaData", rb_cObject);
00091 rb_define_alloc_func(cMetaData, metadata_alloc);
00092
00093 rb_define_method(cMetaData, "include?", RUBY_METHOD_FUNC(metadata_include_p), 1);
00094 rb_define_method(cMetaData, "get", RUBY_METHOD_FUNC(metadata_get), 1);
00095 rb_define_method(cMetaData, "add", RUBY_METHOD_FUNC(metadata_add), -1);
00096 rb_define_method(cMetaData, "clear", RUBY_METHOD_FUNC(metadata_clear), -1);
00097 rb_define_method(cMetaData, "keys", RUBY_METHOD_FUNC(metadata_keys), 0);
00098 enc_utf8 = rb_enc_find("utf-8");
00099 }
00100