GSI: enabling enums as hash keys and int(enum) in Python

This commit is contained in:
Matthias Koefferlein 2023-03-26 17:52:33 +02:00
parent 1ac8ad0739
commit fa61f96194
5 changed files with 146 additions and 1 deletions

View File

@ -0,0 +1,94 @@
#!/bin/bash -e
qt=()
for qm in qmake qmake4 qmake5 qmake6; do
if sh -c "$qm -v" 2>/dev/null >/dev/null; then
qt_version=$($qm -v | grep 'Qt version' | sed 's/.*Qt version *\([0-9]\)\..*/\1/')
if [ "$qt_version" != "" ]; then
echo "Found qmake for Qt$qt_version: $qm"
qt[$qt_version]=$qm
fi
fi
done
qmake=qmake
inst_dir_common=$(pwd)/scripts/mkqtdecl_common
inst_dir4=$(pwd)/scripts/mkqtdecl4
inst_dir5=$(pwd)/scripts/mkqtdecl5
inst_dir6=$(pwd)/scripts/mkqtdecl6
inst_dir=$inst_dir4
work_dir="mkqtdecl.tmp"
while [ "$1" != "" ]; do
a="$1"
shift
case "$a" in
-h)
echo "Update event and property tables"
echo "Usage:"
echo " mkqtdecl_update_tables.sh Update tables for Qt4"
echo " mkqtdecl_update_tables.sh -qt5 Update tables for Qt5"
echo " mkqtdecl_update_tables.sh -qt6 Update tables for Qt6"
echo " mkqtdecl_update_tables.sh -qt <qmake-path> Update tables for specific Qt installation"
exit 0
;;
-qt)
qmake="$1"
shift
;;
-qt5)
qmake="${qt[5]}"
if [ "$qmake" == "" ]; then
echo "*** ERROR: Could not find qmake for Qt5"
exit 1
fi
work_dir="mkqtdecl5.tmp"
inst_dir="$inst_dir5"
;;
-qt6)
qmake="${qt[6]}"
if [ "$qmake" == "" ]; then
echo "*** ERROR: Could not find qmake for Qt6"
exit 1
fi
work_dir="mkqtdecl6.tmp"
inst_dir="$inst_dir6"
;;
*)
echo "*** ERROR: unknown command option $a"
exit 1
;;
esac
done
if ! [ -e build.sh ]; then
echo "*** ERROR: could not find build script in current directy - did you start this script from top level?"
exit 1
fi
mkdir -p $work_dir
bin=$work_dir/bin-update-tables
build=$work_dir/build-update-tables
log=$work_dir/build-update-table.log
echo "Building in $build (log in $log) .."
./build.sh -qmake $qmake -nopython -j8 -release -prefix $(pwd)/$bin -bin $bin -build $build >$log 2>&1
echo "Extracting tables .."
export LD_LIBRARY_PATH=$bin
echo "[1] for properties .."
$bin/klayout -b -r $inst_dir_common/mkqtdecl_extract_props.rb -rd output=$inst_dir/mkqtdecl.properties
echo "[2] for signals .."
$bin/klayout -b -r $inst_dir_common/mkqtdecl_extract_signals.rb -rd output=$inst_dir/mkqtdecl.events
echo "Done."

View File

@ -275,6 +275,7 @@ public:
gsi::method_ext ("to_s", &enum_to_string_ext, "@brief Gets the symbolic string from an enum") +
gsi::method_ext ("inspect", &enum_to_string_inspect_ext, "@brief Converts an enum to a visual string") +
gsi::method_ext ("to_i", &enum_to_int, "@brief Gets the integer value from the enum") +
gsi::method_ext ("hash", &enum_to_int, "@brief Gets the hash value from the enum") +
gsi::method_ext ("==", &enum_eq, gsi::arg("other"), "@brief Compares two enums") +
gsi::method_ext ("==", &enum_eq_with_int, gsi::arg("other"), "@brief Compares an enum with an integer value") +
gsi::method_ext ("!=", &enum_ne, gsi::arg("other"), "@brief Compares two enums for inequality") +

View File

@ -521,6 +521,22 @@ MethodTable::add_method (const std::string &name, const gsi::MethodBase *mb)
mp_module->add_python_doc (mb, tl::to_string (tr ("This method is also available as 'str(object)'")));
}
} else if (name == "to_i" && mb->compatible_with_num_args (0)) {
// The hash method is also routed via the tp_int implementation
add_method_basic ("__int__", mb);
add_method_basic (name, mb);
mp_module->add_python_doc (mb, tl::to_string (tr ("This method is also available as 'int(object)'")));
} else if (name == "to_f" && mb->compatible_with_num_args (0)) {
// The hash method is also routed via the tp_int implementation
add_method_basic ("__float__", mb);
add_method_basic (name, mb);
mp_module->add_python_doc (mb, tl::to_string (tr ("This method is also available as 'float(object)'")));
} else if (name == "hash" && mb->compatible_with_num_args (0)) {
// The hash method is also routed via the tp_hash implementation

View File

@ -696,6 +696,12 @@ class QtBindingTest(unittest.TestCase):
# The various ways to refer to enums
self.assertEqual(pya.Qt.MouseButton(4).to_i(), 4)
self.assertEqual(pya.Qt_MouseButton(4).to_i(), 4)
self.assertEqual(pya.Qt_MouseButton(4).__int__(), 4)
self.assertEqual(pya.Qt_MouseButton(4).__hash__(), 4)
self.assertEqual(int(pya.Qt_MouseButton(4)), 4)
self.assertEqual(str(pya.Qt_MouseButton(4)), "MiddleButton")
self.assertEqual(pya.Qt.MouseButton.LeftButton.to_i(), 1)
self.assertEqual(pya.Qt_MouseButton.LeftButton.to_i(), 1)
self.assertEqual(pya.Qt.LeftButton.to_i(), 1)
@ -706,6 +712,18 @@ class QtBindingTest(unittest.TestCase):
self.assertEqual((pya.Qt.LeftButton | pya.Qt.RightButton).to_i(), 3)
self.assertEqual(type(pya.Qt.LeftButton | pya.Qt.RightButton).__name__, "Qt_QFlags_MouseButton")
def test_59(self):
# Enums can act as hash keys
h = {}
h[pya.Qt.MouseButton.LeftButton] = "left"
h[pya.Qt.MouseButton.RightButton] = "right"
self.assertEqual(pya.Qt.MouseButton.LeftButton in h, True)
self.assertEqual(h[pya.Qt.MouseButton.LeftButton], "left")
self.assertEqual(h[pya.Qt.MouseButton.RightButton], "right")
self.assertEqual(pya.Qt.MouseButton.MiddleButton in h, False)
# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(QtBindingTest)

View File

@ -825,7 +825,10 @@ class QtBinding_TestClass < TestBase
# The various ways to refer to enums
assert_equal(RBA::Qt::MouseButton::LeftButton.to_i, 1)
assert_equal(RBA::Qt::MouseButton::new(4).to_i, 4)
assert_equal(RBA::Qt_MouseButton::new(4).to_i, 4)
assert_equal(RBA::Qt_MouseButton::new(4).hash, 4)
assert_equal(RBA::Qt_MouseButton::new(4).to_s, "MiddleButton")
assert_equal(RBA::Qt_MouseButton::LeftButton.to_i, 1)
assert_equal(RBA::Qt::LeftButton.to_i, 1)
assert_equal((RBA::Qt_MouseButton::LeftButton | RBA::Qt_MouseButton::RightButton).to_i, 3)
@ -837,6 +840,19 @@ class QtBinding_TestClass < TestBase
end
def test_59
# Enums can act as hash keys
h = {}
h[RBA::Qt::MouseButton::LeftButton] = "left"
h[RBA::Qt::MouseButton::RightButton] = "right"
assert_equal(h[RBA::Qt::MouseButton::LeftButton], "left")
assert_equal(h[RBA::Qt::MouseButton::RightButton], "right")
assert_equal(h[RBA::Qt::MouseButton::MiddleButton], nil)
end
end
load("test_epilogue.rb")