summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eldbus_cxx/eldbus_message.hh
blob: d26a470d38c173b938c02c7f738e8e6dfd110465 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * 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_MESSAGE_HH
#define ELDBUS_CXX_ELDBUS_MESSAGE_HH

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

namespace efl { namespace eldbus {

struct message_value
{
  message_value() : _iterator(0) {}
  message_value( ::Eldbus_Message_Iter* iterator) : _iterator(iterator) {}

  template <typename T>
  T get() const
  {
    assert(!!_iterator);
    std::cout << "trying to read " << _detail::signature_traits<T>::sig
              << " has " << eldbus_message_iter_signature_get(_iterator) << std::endl;
    T object;
    char* c = eldbus_message_iter_signature_get(_iterator);
    if(c[0] != _detail::signature_traits<T>::sig)
      throw std::runtime_error("");
    eldbus_message_iter_arguments_get(_iterator, c, &object);
    return object;
  }
  friend inline bool operator==(message_value const& lhs, message_value const& rhs)
  {
    return
      lhs._iterator == rhs._iterator
      || (!lhs._iterator && rhs._iterator && std::strlen(eldbus_message_iter_signature_get(rhs._iterator)) == 0)
      || (!rhs._iterator && lhs._iterator && std::strlen(eldbus_message_iter_signature_get(lhs._iterator)) == 0)
      ;
  }
private:
  friend struct message_iterator;
  ::Eldbus_Message_Iter* _iterator;
};

inline bool operator!=(message_value const& lhs, message_value const& rhs)
{
  return !(lhs == rhs);
}

struct message_iterator
{
  typedef message_value value_type;
  typedef value_type& reference;
  typedef value_type const& const_reference;
  typedef value_type* pointer;
  typedef value_type const* const_pointer;
  typedef std::ptrdiff_t difference_type;
  typedef std::size_t size_type;
  typedef std::input_iterator_tag iterator_category;

  message_iterator()
  {
  }

  message_iterator(Eldbus_Message_Iter* iterator)
    : value(iterator)
  {}

  reference operator*() const
  {
    return const_cast<reference>(value);
  }
  pointer operator->() const
  {
    return const_cast<pointer>(&value);
  }
  message_iterator& operator++()
  {
    if(!eldbus_message_iter_next(value._iterator))
      {
        value._iterator = 0;
      }
    return *this;
  }
  message_iterator operator++(int)
  {
    message_iterator tmp(*this);
    ++*this;
    return tmp;
  }
  friend inline bool operator==(message_iterator const& lhs, message_iterator const& rhs)
  {
    return lhs.value == rhs.value;
  }
private:
  message_value value;
};

inline bool operator!=(message_iterator const& lhs, message_iterator const& rhs)
{
  return !(lhs == rhs);
}

struct reply_t {};
struct error_t {};
struct method_call_t {};

reply_t const reply = {};
error_t const error = {};
method_call_t const method_call = {};

struct const_message
{
  explicit const_message(Eldbus_Message const* message) : _message(message)
  {
  }
  const_message(const_message const& other, reply_t)
    : _message( ::eldbus_message_method_return_new(other.native_handle()))
  {
  }
  const_message(const_message const& other, const char* error_name, const char* error_msg, error_t)
    : _message( ::eldbus_message_error_new(other.native_handle(), error_name, error_msg))
  {
  }
  const_message(const char* destination, const char* path, const char* iface
          , const char* method, method_call_t)
    : _message( ::eldbus_message_method_call_new(destination, path, iface, method))
  {
  }
  const_message(const_message const& other)
    : _message( ::eldbus_message_ref(const_cast<Eldbus_Message*>(other.native_handle())))
  {
  }
  ~const_message()
  {
    eldbus_message_unref(const_cast<Eldbus_Message*>(native_handle()));
  }

  const char* path_get() const
  {
    return ::eldbus_message_path_get(_message);
  }
  const char* interface_get() const
  {
    return ::eldbus_message_interface_get(_message);
  }
  const char* member_get() const
  {
    return ::eldbus_message_member_get(_message);
  }
  const char* destination_get() const
  {
    return ::eldbus_message_destination_get(_message);
  }
  const char* sender_get() const
  {
    return ::eldbus_message_sender_get(_message);
  }
  const char* signature_get() const
  {
    return ::eldbus_message_signature_get(_message);
  }
  bool operator!() const
  {
    bool b (*this);
    return !b;
  }
  explicit operator bool() const
  {
    const char* name, * text;
    return !eldbus_message_error_get(_message, &name, &text);
  }
  std::pair<const char*, const char*> error_get() const
  {
    const char* name, * text;
    eldbus_message_error_get(_message, &name, &text);
    return {name, text};
  }

  typedef message_iterator const_iterator;
  typedef const_iterator iterator;
  typedef Eldbus_Message const* native_handle_type;
  typedef Eldbus_Message const* const_native_handle_type;
  const_native_handle_type native_handle() const { return _message; }
  const_native_handle_type const_native_handle() const { return _message; }

  const_iterator begin() const
  {
    return const_iterator( ::eldbus_message_iter_get(_message));
  }
  const_iterator end() const
  {
    return const_iterator();
  }
private:
  Eldbus_Message const* _message;
};

struct message : const_message
{
  typedef const_message base_type;

  explicit message(Eldbus_Message* message_) : const_message(message_)
  {
  }
  message(const_message const& other, reply_t r)
    : const_message(other, r)
  {
  }
  message(const_message const& other, const char* error_name, const char* error_msg, error_t e)
    : const_message(other, error_name, error_msg, e)
  {
  }
  message(const char* destination, const char* path, const char* iface
          , const char* method, method_call_t m)
    : const_message(destination, path, iface, method, m)
  {
  }
  message(message const& other)
    : const_message(static_cast<base_type const&>(other))
  {
  }
  typedef Eldbus_Message* native_handle_type;
  native_handle_type native_handle() { return const_cast<native_handle_type>(const_message::native_handle()); }
};

struct const_pending
{
  const_pending() : _pending(0) {}
  const_pending(Eldbus_Pending const* pending) : _pending(pending) {}

  void* data_get(const char* key) const
  {
    return ::eldbus_pending_data_get(_pending, key);
  }
  const char* destination_get() const
  {
    return ::eldbus_pending_destination_get(_pending);
  }
  const char* path_get() const
  {
    return ::eldbus_pending_path_get(_pending);
  }
  const char* interface_get() const
  {
    return ::eldbus_pending_interface_get(_pending);
  }
  const char* method_get() const
  {
    return ::eldbus_pending_method_get(_pending);
  }

  typedef Eldbus_Pending const* native_handle_type;
  typedef Eldbus_Pending const* const_native_handle_type;
  native_handle_type native_handle() const { return _pending; }
  const_native_handle_type const_native_handle() const { return _pending; }
private:
  const_native_handle_type _pending;
};

struct pending : const_pending
{
  pending() : const_pending(0) {}
  pending(Eldbus_Pending* pending_) : const_pending(pending_) {}

  void data_set(const char* key, const char* data)
  {
    ::eldbus_pending_data_set(native_handle(), key, data);
  }
  void* data_del(const char* key)
  {
    return ::eldbus_pending_data_del(native_handle(), key);
  }
  void cancel()
  {
    ::eldbus_pending_cancel(native_handle());
  }

  typedef Eldbus_Pending* native_handle_type;
  native_handle_type native_handle()
  {
    return const_cast<native_handle_type>(const_pending::native_handle());
  }
};

} }

#endif