Cannot convert Glib::ObjectBase back to Gdk::Monitor
I'm trying to use gtk4 with gtkmm4.
Even documentation clearly says https://developer-old.gnome.org/gtkmm/stable/classGdk_1_1Display.html#details Gdk::Display get_monitors() method returns Glib::RefPtrGio::ListModel list of Gdk::Monitor and it's possible to use dynamic_pointer_cast https://developer-old.gnome.org/gtkmm-tutorial/stable/sec-refptr-casting.html.en to cast base object to derived type on my machine this code does not compile:
Glib::RefPtr<Gdk::Display> display{Gdk::Display::get_default()};
auto monitors{display->get_monitors()};
for (guint i{0}; i < monitors->get_n_items(); ++i) {
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(monitors->get_object(i));
}
I'm using gcc (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826 It throws an error:
gcc build log
meson build
Directory already configured.
Just run your build command (e.g. ninja) and Meson will regenerate as necessary.
If ninja fails, run "ninja reconfigure" or "meson setup --reconfigure"
to force Meson to regenerate.
If build failures persist, run "meson setup --wipe" to rebuild from scratch
using the same options as passed when configuring the build.
To change option values, run "meson configure" instead.
WARNING: Running the setup command as `meson [options]` instead of `meson setup [options]` is ambiguous and deprecated.
ninja -C build
ninja: Entering directory `build'
[1/2] Compiling C++ object waybar.p/src_client.cpp.o
FAILED: waybar.p/src_client.cpp.o
c++ -Iwaybar.p -I. -I.. -I../include -Iprotocol -I/usr/include/gtkmm-4.0 -I/usr/lib64/gtkmm-4.0/include -I/usr/include/giomm-2.68 -I/usr/lib64/giomm-2.68/include -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/lib64/libffi/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glibmm-2.68 -I/usr/lib64/glibmm-2.68/include -I/usr/include/sigc++-3.0 -I/usr/lib64/sigc++-3.0/include -I/usr/include/gtk-4.0 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/libpng16 -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/graphene-1.0 -I/usr/lib64/graphene-1.0/include -I/usr/include/cairomm-1.16 -I/usr/lib64/cairomm-1.16/include -I/usr/include/pangomm-2.48 -I/usr/lib64/pangomm-2.48/include -I/usr/include/gtk-4.0/unix-print -I/usr/include/jsoncpp -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++20 -O3 '-DVERSION="0.9.24-20-gb920ec6b (branch '"'"'gtk4'"'"')"' -DHAVE_LANGINFO_1STDAY '-DSYSCONFDIR="//usr/local/etc"' -DSPDLOG_SHARED_LIB -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -mfpmath=sse -msse -msse2 -pthread -MD -MQ waybar.p/src_client.cpp.o -MF waybar.p/src_client.cpp.o.d -o waybar.p/src_client.cpp.o -c ../src/client.cpp
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/chrono:49,
from /usr/include/spdlog/common.h:10,
from /usr/include/spdlog/spdlog.h:12,
from ../src/client.cpp:2:
/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/shared_ptr.h: In instantiation of ‘std::shared_ptr<_Tp> std::dynamic_pointer_cast(shared_ptr<_Tp>&&)[with _Tp = Gdk::Monitor; _Up = Glib::ObjectBase]’:
../src/client.cpp:72:59: required from here
/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/shared_ptr.h:770:23: error: cannot ‘dynamic_cast’ ‘(& __r)->std::shared_ptr<Glib::ObjectBase>::<anonymous>.std::__shared_ptr<Glib::ObjectBase, __gnu_cxx::_S_atomic>::get()’ (of type ‘using std::__shared_ptr<Glib::ObjectBase, __gnu_cxx::_S_atomic>::element_type = std::remove_extent<Glib::ObjectBase>::type*’ {aka ‘class Glib::ObjectBase*’}) to type ‘using std::shared_ptr<Gdk::Monitor>::element_type = using std::__shared_ptr<Gdk::Monitor, __gnu_cxx::_S_atomic>::element_type = std::remove_extent<Gdk::Monitor>::type*’ {aka ‘class Gdk::Monitor*’} (target is not pointer or reference to complete type)
770 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ninja: build stopped: subcommand failed.
make: *** [Makefile:7: build] Error 1
Even I try clan I have no luck. clang version 16.0.6 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/lib/llvm/16/bin Configuration file: /etc/clang/clang.cfg
Clang build log
ninja: Entering directory `build-clang'
[1/2] Compiling C++ object waybar.p/src_client.cpp.o
FAILED: waybar.p/src_client.cpp.o
clang++ -Iwaybar.p -I. -I.. -I../include -Iprotocol -I/usr/include/gtkmm-4.0 -I/usr/lib64/gtkmm-4.0/include -I/usr/include/giomm-2.68 -I/usr/lib64/giomm-2.68/include -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/lib64/libffi/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glibmm-2.68 -I/usr/lib64/glibmm-2.68/include -I/usr/include/sigc++-3.0 -I/usr/lib64/sigc++-3.0/include -I/usr/include/gtk-4.0 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/libpng16 -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/graphene-1.0 -I/usr/lib64/graphene-1.0/include -I/usr/include/cairomm-1.16 -I/usr/lib64/cairomm-1.16/include -I/usr/include/pangomm-2.48 -I/usr/lib64/pangomm-2.48/include -I/usr/include/gtk-4.0/unix-print -I/usr/include/jsoncpp -fcolor-diagnostics -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++20 -O3 '-DVERSION="0.9.24-20-gb920ec6b (branch '"'"'gtk4'"'"')"' -DHAVE_LANGINFO_1STDAY '-DSYSCONFDIR="//usr/local/etc"' -DSPDLOG_SHARED_LIB -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -mfpmath=sse -msse -msse2 -pthread -MD -MQ waybar.p/src_client.cpp.o -MF waybar.p/src_client.cpp.o.d -o waybar.p/src_client.cpp.o -c ../src/client.cpp
In file included from ../src/client.cpp:2:
In file included from /usr/include/spdlog/spdlog.h:12:
In file included from /usr/include/spdlog/common.h:10:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/chrono:49:
/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/shared_ptr.h:770:23: error: 'Gdk::Monitor' is an incomplete type
if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../src/client.cpp:72:25: note: in instantiation of function template specialization 'std::dynamic_pointer_cast<Gdk::Monitor, Glib::ObjectBase>' requested here
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(monitors->get_object(i));
^
/usr/include/gtkmm-4.0/gdkmm/surface.h:57:17: note: forward declaration of 'Gdk::Monitor'
class GDKMM_API Monitor;
^
1 error generated.
ninja: build stopped: subcommand failed.
Found the same closed issue #119 (closed) But code from it does not work