ruby: basic stl::set<uint32> support, add Viewscreen.feed_keys

develop
jj 2012-09-14 13:55:22 +02:00
parent a9ce19b826
commit b05905a0ba
3 changed files with 105 additions and 1 deletions

@ -760,6 +760,52 @@ module DFHack
end
end
class StlSet
attr_accessor :_memaddr, :_enum
def self.cpp_new(init=nil, enum=nil)
ret = new DFHack.memory_stlset_new, enum
init.each { |k| ret.set(k) } if init
ret
end
def initialize(addr, enum=nil)
addr = nil if addr == 0
@_memaddr = addr
@_enum = enum
end
def isset(key)
raise unless @_memaddr
key = @_enum.int(key) if _enum
DFHack.memory_stlset_isset(@_memaddr, key)
end
alias is_set? isset
def set(key)
raise unless @_memaddr
key = @_enum.int(key) if _enum
DFHack.memory_stlset_set(@_memaddr, key)
end
def delete(key)
raise unless @_memaddr
key = @_enum.int(key) if _enum
DFHack.memory_stlset_deletekey(@_memaddr, key)
end
def clear
raise unless @_memaddr
DFHack.memory_stlset_clear(@_memaddr)
end
def _cpp_delete
raise unless @_memaddr
DFHack.memory_stlset_delete(@_memaddr)
@_memaddr = nil
end
end
# cpp rtti name -> rb class
@rtti_n2c = {}
@rtti_c2n = {}
@ -822,7 +868,7 @@ module DFHack
when true; 1
when Integer; arg
#when String; [arg].pack('p').unpack('L')[0] # raw pointer to buffer
when MemHack::Compound; arg._memaddr
when MemHack::Compound, StlSet; arg._memaddr
else raise "bad vmethod arg #{arg.class}"
end
end

@ -835,6 +835,49 @@ static VALUE rb_dfmemory_bitarray_set(VALUE self, VALUE addr, VALUE idx, VALUE v
return Qtrue;
}
// add basic support for std::set<uint32> used for passing keyboard keys to viewscreens
#include <set>
static VALUE rb_dfmemory_set_new(VALUE self)
{
std::set<unsigned long> *ptr = new std::set<unsigned long>;
return rb_uint2inum((uint32_t)ptr);
}
static VALUE rb_dfmemory_set_delete(VALUE self, VALUE set)
{
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
if (ptr)
delete ptr;
return Qtrue;
}
static VALUE rb_dfmemory_set_set(VALUE self, VALUE set, VALUE key)
{
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
ptr->insert(rb_num2ulong(key));
return Qtrue;
}
static VALUE rb_dfmemory_set_isset(VALUE self, VALUE set, VALUE key)
{
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
return ptr->count(rb_num2ulong(key)) ? Qtrue : Qfalse;
}
static VALUE rb_dfmemory_set_deletekey(VALUE self, VALUE set, VALUE key)
{
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
ptr->erase(rb_num2ulong(key));
return Qtrue;
}
static VALUE rb_dfmemory_set_clear(VALUE self, VALUE set)
{
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
ptr->clear();
return Qtrue;
}
/* call an arbitrary object virtual method */
#ifdef WIN32
@ -950,4 +993,10 @@ static void ruby_bind_dfhack(void) {
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_resize", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_resize), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_isset", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_isset), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_set", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_set), 3);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_new", RUBY_METHOD_FUNC(rb_dfmemory_set_new), 0);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_delete", RUBY_METHOD_FUNC(rb_dfmemory_set_delete), 1);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_set", RUBY_METHOD_FUNC(rb_dfmemory_set_set), 2);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_isset", RUBY_METHOD_FUNC(rb_dfmemory_set_isset), 2);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_deletekey", RUBY_METHOD_FUNC(rb_dfmemory_set_deletekey), 2);
rb_define_singleton_method(rb_cDFHack, "memory_stlset_clear", RUBY_METHOD_FUNC(rb_dfmemory_set_clear), 1);
}

@ -79,4 +79,13 @@ module DFHack
world.status.popups << pop
end
end
class Viewscreen
def feed_keys(*keys)
keyset = StlSet.cpp_new(keys, InterfaceKey)
ret = feed(keyset)
keyset._cpp_delete
ret
end
end
end