summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eldbus_cxx/eldbus_basic.hh
blob: 20e568680c006a7ae02e1e653d0694769d73327f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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 ELDBUS_CXX_ELDBUS_BASIC_HH
#define ELDBUS_CXX_ELDBUS_BASIC_HH

#include <Eina.hh>
#include <Eldbus.h>

#include <eldbus_proxy_call.hh>

namespace efl { namespace eldbus {

struct unknown_t {};
struct session_t {};
struct system_t {};
struct start_t {};
struct address_t {};

unknown_t const unknown = unknown_t();
session_t const session = session_t();
system_t const system = system_t();
start_t const start = start_t();
address_t const address = address_t();

struct eldbus_init
{
  eldbus_init()
  {
    ::eldbus_init();
  }
  ~eldbus_init()
  {
    ::eldbus_shutdown();
  }
};

struct proxy
{
  typedef Eldbus_Proxy* native_handle_type;
  typedef Eldbus_Proxy const* const_native_handle_type;

  proxy(native_handle_type native)
    : _proxy(native) {}

  template <typename R, typename Callback, typename... Args>
  void call(const char* method, double timeout, Callback&& callback, Args... args) const
  {
    eldbus::_detail::proxy_call<R>(_proxy, method, timeout, std::forward<Callback>(callback), args...);
  }

  template <typename Callback, typename... Args>
  void call(const char* method, double timeout, Callback&& callback, Args... args) const
  {
    eldbus::_detail::proxy_call<void>(_proxy, method, timeout, std::forward<Callback>(callback), args...);
  }

  native_handle_type native_handle() { return _proxy; }
  const_native_handle_type native_handle() const { return _proxy; }
private:
  native_handle_type _proxy;
};

struct object
{
  typedef Eldbus_Object* native_handle_type;
  typedef Eldbus_Object const* const_native_handle_type;
  object(native_handle_type o)
    : _object(o) {}

  proxy get_proxy(const char* interface_) const
  {
    Eldbus_Proxy* p = eldbus_proxy_get
      (const_cast<object*>(this)->native_handle(), interface_);
    if(!p)
      {
        eina::error_code ec = eina::get_error_code();
        assert(!!ec);
        throw eina::system_error(ec, "eldbus");
      }
    return proxy(p);
  }
  
  native_handle_type native_handle() { return _object; }
  const_native_handle_type const_native_handle() { return _object; }
private:
  native_handle_type _object;
};

struct connection
{
  typedef Eldbus_Connection* native_handle_type;
  typedef Eldbus_Connection const* const_native_handle_type;

  explicit connection(native_handle_type connection_)
    : _connection(connection_) {}
  ~connection()
  {
    eldbus_connection_unref(_connection);
  }
  connection(unknown_t)
    : _connection(eldbus_connection_get(ELDBUS_CONNECTION_TYPE_UNKNOWN))
  {
    _check_connection();
  }
  connection(session_t)
    : _connection(eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION))
  {
    _check_connection();
  }
  connection(system_t)
    : _connection(eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM))
  {
    _check_connection();
  }
  connection(address_t)
    : _connection(eldbus_connection_get(ELDBUS_CONNECTION_TYPE_ADDRESS))
  {
    _check_connection();
  }

  object get_object(const char* bus, const char* path) const
  {
    Eldbus_Object* o = eldbus_object_get
      (const_cast<connection*>(this)->native_handle(), bus, path);
    if(!o)
      {
        eina::error_code ec = eina::get_error_code();
        assert(!!ec);
        throw eina::system_error(ec, "eldbus");
      }
    return object(o);
  }
  
  native_handle_type native_handle() { return _connection; }
  const_native_handle_type native_handle() const { return _connection; }
private:
  void _check_connection()
  {
    if(!_connection)
      {
        eina::error_code ec = eina::get_error_code();
        assert(!!ec);
        throw eina::system_error(ec, "eldbus");
      }
  }

  native_handle_type _connection;
};

} }

#endif