summaryrefslogtreecommitdiff
path: root/tests/giomm_asyncresult_sourceobject/main.cc
blob: 6e71a7559904ec7811483d99cd448ddb4b9a2346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <giomm.h>
#include <iostream>
#include <typeinfo>

void
on_read_async(const Glib::RefPtr<Gio::AsyncResult>& result)
{
  if (!result)
  {
    std::cerr << G_STRFUNC << ": result is empty." << std::endl;
    exit(EXIT_FAILURE);
  }

  auto cobj = g_async_result_get_source_object(result->gobj());
  if (!cobj)
  {
    std::cerr << G_STRFUNC << ": g_async_result_get_source_object() failed." << std::endl;
    exit(EXIT_FAILURE);
  }

  // Show why Glib::wrap(cobj) can't be used in Gio::AsyncResult::get_source_object_base().
  // cppobjbase is not a Glib::Object*, it's a Gio::File* which is a Glib::Interface*.
  std::cout << "GType name: " << G_OBJECT_TYPE_NAME(cobj) << std::endl;
  auto cppobjbase = Glib::wrap_auto(cobj); // Glib::ObjectBase::_get_current_wrapper(cobj);
  if (cppobjbase)
  {
    std::cout << "C++ type name: " << typeid(*cppobjbase).name() << std::endl;
    auto cppobj = dynamic_cast<Glib::Object*>(cppobjbase); // Part of Glib::wrap(GObject*, bool)
    auto cppiface = dynamic_cast<Glib::Interface*>(cppobjbase);
    std::cout << "dynamic_cast<Glib::Object*>: " << cppobj << std::endl;
    std::cout << "dynamic_cast<Glib::Interface*>: " << cppiface << std::endl;
  }

  if (!result->get_source_object_base())
  {
    std::cerr << G_STRFUNC << ": result->get_source_object_base() failed." << std::endl;
    exit(EXIT_FAILURE);
  }

  exit(EXIT_SUCCESS);
}

int
main(int, char**)
{
  Glib::init();
  Gio::init();

  auto mainloop = Glib::MainLoop::create();

  auto file = Gio::File::create_for_path("/etc/passwd");
  file->read_async(&on_read_async);

  mainloop->run();
  return EXIT_SUCCESS;
}