summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eldbus_cxx/eldbus_freedesktop.hh
blob: d7414e536dc9f7e5594f79f83b8fe99d1a9b9424 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EFL_ELDBUS_CXX_ELDBUS_FREEDESKTOP_HH
#define EFL_ELDBUS_CXX_ELDBUS_FREEDESKTOP_HH

#include <eldbus_message.hh>
#include <eldbus_service.hh>
#include <eldbus_basic.hh>
#include <eldbus_raw_tuple.hh>

namespace efl { namespace eldbus { namespace _detail {

template <typename Callback, typename... Ins>
void _callback_wrapper(void* data, Eldbus_Message const* message, Eldbus_Pending* pending)
{
  Callback* callback(static_cast<Callback*>(data));

  const char* errname, *errmsg;
  if (eldbus_message_error_get(message, &errname, &errmsg))
    {
      std::cout << "error " << errname << " " << errmsg << std::endl;
      std::tuple<Ins...> tuple;
      eldbus::const_message msg( ::eldbus_message_ref(const_cast<Eldbus_Message*>(message)));
      eina::error_code ec (eldbus::call_error_code(), eina::eina_error_category());
      eina::call_tuple_unwrap_prefix
        (*callback, tuple, eina::make_index_sequence<sizeof...(Ins)>()
         , ec, msg, pending);
    }

  typename raw_tuple<std::tuple<Ins...> >::type tuple;
  if(sizeof...(Ins))
    {
      if(!_detail::_init_raw_tuple<0u, std::tuple<Ins...> >
         (eldbus_message_iter_get(message)
          , tuple, std::integral_constant<bool, (sizeof...(Ins) == 0)>()))
        {
          std::cout << "error init raw tuple" << std::endl;
          std::tuple<Ins...> tuple_;
          eldbus::const_message msg( ::eldbus_message_ref(const_cast<Eldbus_Message*>(message)));
          eina::error_code ec(eldbus::signature_mismatch_error_code(), eina::eina_error_category());
          eina::call_tuple_unwrap_prefix
            (*callback, tuple_, eina::make_index_sequence<sizeof...(Ins)>()
             , ec, msg, pending);
        }
    }
  std::cout << "OK go" << std::endl;
  eina::error_code ec;
  eldbus::const_message msg( ::eldbus_message_ref(const_cast<Eldbus_Message*>(message)));
  eina::call_tuple_unwrap_prefix
    (*callback, tuple, eina::make_index_sequence<sizeof...(Ins)>()
     , ec, msg, pending);
}

template <typename F>
void _free_cb(void* data, const void*)
{
  delete static_cast<F*>(data);
}

}

template <typename... Ins, typename F>
pending name_request(connection& c, const char* bus, unsigned int flags, F&& function)
{
  F* f = new F(std::forward<F>(function));
  pending r = ::eldbus_name_request(c.native_handle(), bus, flags
                                    , &_detail::_callback_wrapper<F, Ins...>, f);
  eldbus_pending_free_cb_add(r.native_handle(), &_detail::_free_cb<F>, f);
  return r;
}

} }

#endif