Skip to content

Commit

Permalink
Merge pull request #303 from mcorino/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mcorino authored Sep 28, 2024
2 parents 5b567e0 + fa126e1 commit 14d0543
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 97 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/msw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ jobs:
swig: '4'
wxw_type: embed
wxr_type: develop
- os: windows-2022
platform: 'x64'
CXX: g++-12
ruby: '3.1'
swig: '4'
wxw_type: embed
wxr_type: gem
- os: windows-2022
platform: 'x64'
CXX: g++-12
Expand Down
27 changes: 22 additions & 5 deletions lib/wx/core/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@

class Wx::Enum

class << self

def enumerators
self.constants(false).inject({}) do |tbl, cn|
cv = self.const_get(cn)
tbl[cv.to_i] = cn if self === cv
tbl
end
end

def [](enum_name)
return self.const_get(enum_name) if self.const_defined?(enum_name)
nil
end

end

def |(other)
if other.instance_of?(self.class)
self.class.new(to_i | other.to_i)
Expand Down Expand Up @@ -52,10 +69,10 @@ def bitmask_to_s
return '' if to_i == 0
enums = []
mask = to_i
self.class.values.each_value do |enum|
if enum != 0 && mask.allbits?(enum)
enums << enum.to_s
mask &= ~enum
self.class.enumerators.each_pair do |eval, ename|
if eval != 0 && mask.allbits?(eval)
enums << ename
mask &= ~eval
break if mask == 0
end
end
Expand All @@ -65,7 +82,7 @@ def bitmask_to_s
private :bitmask_to_s

def to_s
self.class.values.has_key?(to_i) ? "#{self.class.name}::#{self.class.names_by_value[self]}" : bitmask_to_s
self.class.enumerators.has_key?(to_i) ? "#{self.class.name}::#{self.class.enumerators[to_i]}" : bitmask_to_s
end

end
11 changes: 0 additions & 11 deletions lib/wx/doc/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@ def to_int; end
# of a bitwise **or** expression is returned matching the mask.
def to_s; end

# Create a new class and associated enum values.
# @param [String,Symbol] name name of new enum class
# @param [Hash] enum_values hash with enum value name and enum integer value pairs
# @return [Class] new enum class
def self.create(name, enum_values) end

# Returns enum class matching name or nil.
# @param [String,Symbol] name name of enum class
# @return [Class,nil] enum class
def self.[](name) end

end

end
2 changes: 1 addition & 1 deletion lib/wx/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# This software is released under the MIT license.

module Wx
WXRUBY_VERSION = '1.2.0'
WXRUBY_VERSION = '1.2.1'
end
80 changes: 0 additions & 80 deletions rakelib/lib/core/include/enum.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static const char * __iv_Enum_sc_enums = "@enums";

// instance variables for derived Enum class singleton classes
static const char * __iv_enum_klass_values = "@values"; // hash map of all value instances of derived Enum (by integer value)
static const char * __iv_enum_klass_values_by_name = "@values_by_name"; // hash map of all value instances of derived Enum
static const char * __iv_enum_klass_name = "@name"; // unscoped name of derived Enum

static VALUE wx_Enum_initialize(int argc, VALUE *argv, VALUE self)
Expand Down Expand Up @@ -149,55 +148,10 @@ static VALUE wx_Enum_to_int(int argc, VALUE *argv, VALUE self)
return rb_iv_get(self, __iv_cEnum_value);
}

static VALUE wx_Enum_sc_get_enum_class(int argc, VALUE *argv, VALUE self)
{
if ((argc < 1) || (argc > 1))
{
rb_raise(rb_eArgError, "wrong # of arguments(%d for 1)", argc); return Qnil;
}
return rb_hash_aref(rb_iv_get(cEnum_Singleton, __iv_Enum_sc_enums), rb_to_symbol(argv[0]));
}

static VALUE wx_Enum_sc_create_enum_class(int argc, VALUE *argv, VALUE self)
{
if ((argc < 2) || (argc > 2))
{
rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc); return Qnil;
}
VALUE enum_name = rb_to_symbol(argv[0]);
if (TYPE(argv[1]) != T_HASH)
{
VALUE str = rb_inspect(argv[1]);
rb_raise(rb_eArgError,
"Invalid enum_values; expected Hash but got %s.",
StringValuePtr(str));
return Qnil;
}
ID id_new = rb_intern("new");
ID id_to_i = rb_intern("to_i");
ID id_const_set = rb_intern("const_set");
VALUE enum_klass = rb_funcall(rb_cClass, id_new, 1, cWxEnum, 0);
VALUE enum_singleton_klass = rb_funcall(enum_klass, rb_intern("singleton_class"), 0, 0);
VALUE enum_values = rb_funcall(argv[1], rb_intern("keys"), 0, 0);
for (int i=0; i<RARRAY_LEN(enum_values) ;++i)
{
VALUE enum_value_name = rb_ary_entry(enum_values, i);
VALUE enum_value_num = rb_funcall(rb_hash_aref(argv[1], enum_value_name), id_to_i, 0, 0);
VALUE enum_value = rb_funcall(enum_klass, id_new, 1, enum_value_num, 0);
rb_funcall(enum_klass, id_const_set, 2, enum_value_name, enum_value, 0);
rb_hash_aset(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values), enum_value_num, enum_value);
rb_hash_aset(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values_by_name), rb_to_symbol(enum_value_name), enum_value);
}
rb_hash_aset(rb_iv_get(cEnum_Singleton, __iv_Enum_sc_enums), enum_name, enum_klass);
return enum_klass;
}

static void wx_setup_Enum_singleton_class()
{
cEnum_Singleton = rb_funcall(cWxEnum, rb_intern("singleton_class"), 0, 0);
rb_iv_set(cEnum_Singleton, __iv_Enum_sc_enums, rb_hash_new());
rb_define_method(cEnum_Singleton, "create", VALUEFUNC(wx_Enum_sc_create_enum_class), -1);
rb_define_singleton_method(cWxEnum, "[]", VALUEFUNC(wx_Enum_sc_get_enum_class), -1);
}

static void wx_define_Enum_class()
Expand All @@ -223,44 +177,11 @@ WXRB_EXPORT_FLAG VALUE wxRuby_GetEnumClass(const char* enum_class_name_cstr)
return rb_hash_aref(enum_hash, rb_str_new2(enum_class_name_cstr));
}

static VALUE wx_Enum_sc_get_enum_value(int argc, VALUE *argv, VALUE self)
{
if ((argc < 1) || (argc > 1))
{
rb_raise(rb_eArgError, "wrong # of arguments(%d for 1)", argc); return Qnil;
}
VALUE enum_singleton_klass = rb_funcall(self, rb_intern("singleton_class"), 0, 0);
return rb_hash_aref(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values_by_name), rb_to_symbol(argv[0]));
}

static VALUE wx_Enum_sc_get_enum_values(VALUE self)
{
VALUE enum_singleton_klass = rb_funcall(self, rb_intern("singleton_class"), 0, 0);
return rb_iv_get(enum_singleton_klass, __iv_enum_klass_values);
}

static VALUE wx_Enum_sc_get_enum_values_by_name(VALUE self)
{
VALUE enum_singleton_klass = rb_funcall(self, rb_intern("singleton_class"), 0, 0);
return rb_iv_get(enum_singleton_klass, __iv_enum_klass_values_by_name);
}

static VALUE wx_Enum_sc_get_enum_names_by_value(VALUE self)
{
VALUE enum_singleton_klass = rb_funcall(self, rb_intern("singleton_class"), 0, 0);
return rb_funcall(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values_by_name), rb_intern("invert"), 0);
}

WXRB_EXPORT_FLAG VALUE wxRuby_CreateEnumClass(const char* enum_class_name_cstr)
{
VALUE enum_klass = rb_funcall(rb_cClass, rb_intern("new"), 1, cWxEnum, 0);
rb_define_singleton_method(enum_klass, "[]", VALUEFUNC(wx_Enum_sc_get_enum_value), -1);
rb_define_singleton_method(enum_klass, "values", VALUEFUNC(wx_Enum_sc_get_enum_values), 0);
rb_define_singleton_method(enum_klass, "values_by_name", VALUEFUNC(wx_Enum_sc_get_enum_values_by_name), 0);
rb_define_singleton_method(enum_klass, "names_by_value", VALUEFUNC(wx_Enum_sc_get_enum_names_by_value), 0);
VALUE enum_singleton_klass = rb_funcall(enum_klass, rb_intern("singleton_class"), 0, 0);
rb_iv_set(enum_singleton_klass, __iv_enum_klass_values, rb_hash_new());
rb_iv_set(enum_singleton_klass, __iv_enum_klass_values_by_name, rb_hash_new());
rb_iv_set(enum_singleton_klass, __iv_enum_klass_name, rb_str_new2(enum_class_name_cstr));
rb_hash_aset(rb_iv_get(cEnum_Singleton, __iv_Enum_sc_enums),
ID2SYM(rb_intern(enum_class_name_cstr)),
Expand All @@ -274,7 +195,6 @@ WXRB_EXPORT_FLAG VALUE wxRuby_AddEnumValue(VALUE enum_klass, const char* enum_va
VALUE enum_value = rb_funcall(enum_klass, rb_intern("new"), 1, enum_value_num, 0);
VALUE enum_singleton_klass = rb_funcall(enum_klass, rb_intern("singleton_class"), 0, 0);
rb_hash_aset(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values), enum_value_num, enum_value);
rb_hash_aset(rb_iv_get(enum_singleton_klass, __iv_enum_klass_values_by_name), enum_value_name, enum_value);
rb_funcall(enum_klass, rb_intern("const_set"), 2, enum_value_name, enum_value, 0);
return enum_value;
}
Expand Down

0 comments on commit 14d0543

Please sign in to comment.