summaryrefslogtreecommitdiff
path: root/glib/src/variant.ccg
blob: 39b88e0452e7e53a4cacd843afe9f011af99afc3 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Copyright 2010 The glibmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmm/variant.h>
#include <glibmm/utility.h>
#include <glib.h>

namespace Glib
{

void VariantBase::get_normal_form(VariantBase& result) const
{
  GVariant* const g_value =
    g_variant_get_normal_form(const_cast<GVariant*>(gobj()));

  //The C function never returns NULL, according to its docuemenation,
  //so we don't need a bool return value.
  result.init(g_value); // g_value is already referenced.
}

void VariantBase::byteswap(VariantBase& result) const
{
  GVariant* const g_value = g_variant_byteswap(const_cast<GVariant*>(gobj()));
  result.init(g_value); // g_value is already referenced.
}

//static
void VariantStringBase::create_object_path(VariantStringBase& output,
  const std::string& object_path)
{
  GVariant* result = 0;
  result = g_variant_new_object_path(object_path.c_str());
  g_variant_ref_sink(result);
  output.init(result);
}

//static
void VariantStringBase::create_signature(VariantStringBase& output,
  const std::string& signature)
{
  GVariant* result = 0;
  result = g_variant_new_signature(signature.c_str());
  g_variant_ref_sink(result);
  output.init(result);
}

//static
VariantContainerBase
VariantContainerBase::create_tuple(const std::vector<VariantBase>& children)
{
  typedef GVariant* var_ptr;
  var_ptr* const var_array = new var_ptr[children.size()];

  for(std::vector<VariantBase>::size_type i = 0; i < children.size(); i++)
  {
    var_array[i] = const_cast<GVariant*>(children[i].gobj());
  }

  VariantContainerBase result = VariantContainerBase(g_variant_new_tuple(
    var_array, children.size()));

  g_variant_ref_sink(result.gobj());

  delete[] var_array;

  return result;
}

void VariantContainerBase::get(VariantBase& child, gsize index) const
{
  if(index > g_variant_n_children(gobject_))
    throw std::out_of_range(
      "VariantContainerBase::get(): Index out of bounds.");

  GVariant* const gvariant = g_variant_get_child_value(gobject_, index);
  child.init(gvariant);
}

// VariantContainerBase has no method variant_type()
template<>
VariantContainerBase VariantBase::cast_dynamic<VariantContainerBase>(const VariantBase& v)
throw(std::bad_cast)
{
  if(v.gobj() == NULL)
  {
    return VariantContainerBase();
  }
  if(v.get_type().is_container())
  {
    return VariantContainerBase(const_cast<GVariant*>(v.gobj()), true);
  }
  else
  {
    throw std::bad_cast();
  }
}

bool VariantContainerBase::get_maybe(Glib::VariantBase& maybe) const
{
  GVariant* const g_value =
    g_variant_get_maybe(const_cast<GVariant*>(gobj()));

  if(g_value)
  {
    maybe.init(g_value); // g_value is already referenced.
    return true;
  }
  else
    return false;
}

/****************** Specializations ***********************************/

void VariantBase::init(const GVariant* cobject, bool take_a_reference)
{
  if(gobject_)
    g_variant_unref(gobject_);

  gobject_ = const_cast<GVariant*>(cobject);
  if(take_a_reference)
    g_variant_ref(gobject_);
}

// static
const VariantType& Variant<VariantBase>::variant_type()
{
  static VariantType type(G_VARIANT_TYPE_VARIANT);
  return type;
}

Variant<VariantBase> Variant<VariantBase>::create(const VariantBase& data)
{
  Variant<VariantBase> result = Variant<VariantBase>(g_variant_new_variant(const_cast<GVariant*>(data.gobj())));

  // Remove the floating reference (since it is newly created).
  g_variant_ref_sink(result.gobj());

  return result;
}

void Variant<VariantBase>::get(Glib::VariantBase& variant) const
{
  GVariant* const gvariant = g_variant_get_variant(gobject_);
  variant.init(gvariant);
}

// static
const VariantType& Variant<Glib::ustring>::variant_type()
{
  static VariantType type(G_VARIANT_TYPE_STRING);
  return type;
}

Variant<Glib::ustring>
Variant<Glib::ustring>::create(const Glib::ustring& data)
{
  Variant<Glib::ustring> result =
    Variant<Glib::ustring>(g_variant_new_string(data.c_str()));

  // Remove the floating reference (since it is newly created).
  g_variant_ref_sink(result.gobj());

  return result;
}

Glib::ustring Variant<Glib::ustring>::get() const
{
  return Glib::ustring(g_variant_get_string(gobject_, 0));
}

// static
const VariantType& Variant<std::string>::variant_type()
{
  static VariantType type(G_VARIANT_TYPE_BYTESTRING);
  return type;
}

Variant<std::string>
Variant<std::string>::create(const std::string& data)
{
  Variant<std::string> result =
    Variant<std::string>(g_variant_new_bytestring(data.c_str()));

  // Remove the floating reference (since it is newly created).
  g_variant_ref_sink(result.gobj());

  return result;
}

typedef std::vector<Glib::ustring> type_vec_ustring;

// static
const VariantType& Variant<type_vec_ustring>::variant_type()
{
  static VariantType type(G_VARIANT_TYPE_STRING_ARRAY);
  return type;
}

Variant<type_vec_ustring>
Variant<type_vec_ustring>::create(const type_vec_ustring& data)
{
  // Get the variant type of the elements.
  VariantType element_variant_type = Variant<Glib::ustring>::variant_type();

  // Get the variant type of the array.
  VariantType array_variant_type = Variant<type_vec_ustring>::variant_type();

  // Create a GVariantBuilder to build the array.
  GVariantBuilder* builder = g_variant_builder_new(array_variant_type.gobj());

  // Add the elements of the vector into the builder.
  for(type_vec_ustring::const_iterator iter = data.begin();
   iter < data.end(); iter++)
  {
    g_variant_builder_add(builder,
      reinterpret_cast<gchar*>(element_variant_type.gobj()), iter->c_str());
  }

  // Create the variant using the builder.
  Variant<type_vec_ustring> result =
    Variant<type_vec_ustring>(g_variant_new(
      reinterpret_cast<gchar*>(array_variant_type.gobj()), builder));

  // Remove the floating reference (since it is newly created).
  g_variant_ref_sink(result.gobj());

  return result;
}

Glib::ustring Variant<type_vec_ustring>::get(gsize index) const
{
  gsize n_elements = 0;

  const gchar** array = g_variant_get_strv(const_cast<GVariant*>(gobj()),
    &n_elements);

  if(index > n_elements)
    throw std::out_of_range(
      "Variant< std::vector<Glib::ustring> >::get(): Index out of bounds.");

  Glib::ustring const result(array[index]);
  g_free(array);
  return result;
}

type_vec_ustring Variant<type_vec_ustring>::get() const
{
  gsize n_elements = 0;

  const gchar** array = g_variant_get_strv(const_cast<GVariant*>(gobj()),
    &n_elements);

  type_vec_ustring const result(array, array + n_elements);
  g_free(array);
  return result;
}

VariantIter Variant<type_vec_ustring>::get_iter() const
{
  // Get the variant type of the elements.
  VariantType element_variant_type = Variant<Glib::ustring>::variant_type();

  // Get the variant type of the array.
  VariantType array_variant_type = Variant<type_vec_ustring>::variant_type();

  // Get the GVariantIter.
  GVariantIter* g_iter = 0;
  g_variant_get(const_cast<GVariant*>(gobj()),
    reinterpret_cast<gchar*>(array_variant_type.gobj()), &g_iter);

  return VariantIter(g_iter);
}

typedef std::vector<std::string> type_vec_string;

// static
const VariantType& Variant<type_vec_string>::variant_type()
{
  static VariantType type(G_VARIANT_TYPE_BYTESTRING_ARRAY);
  return type;
}

Variant<type_vec_string>
Variant<type_vec_string>::create(const type_vec_string& data)
{
  // Get the variant type of the elements.
  VariantType element_variant_type = Variant<std::string>::variant_type();

  // Get the variant type of the array.
  VariantType array_variant_type = Variant<type_vec_string>::variant_type();

  // Create a GVariantBuilder to build the array.
  GVariantBuilder* builder = g_variant_builder_new(array_variant_type.gobj());

  // Add the elements of the vector into the builder.
  for(type_vec_string::const_iterator iter = data.begin();
   iter < data.end(); iter++)
  {
    g_variant_builder_add(builder,
      reinterpret_cast<gchar*>(element_variant_type.gobj()), iter->c_str());
  }

  // Create the variant using the builder.
  Variant<type_vec_string> result =
    Variant<type_vec_string>(g_variant_new(
      reinterpret_cast<gchar*>(array_variant_type.gobj()), builder));

  // Remove the floating reference (since it is newly created).
  g_variant_ref_sink(result.gobj());

  return result;
}

std::string Variant<type_vec_string>::get(gsize index) const
{
  gsize n_elements = 0;

  const gchar** array =
    g_variant_get_bytestring_array(const_cast<GVariant*>(gobj()), &n_elements);

  if(index > n_elements)
    throw std::out_of_range(
      "Variant< std::vector<std::string> >::get(): Index out of bounds.");

  std::string const result(array[index]);
  g_free(array);
  return result;
}

type_vec_string Variant<type_vec_string>::get() const
{
  gsize n_elements = 0;

  const gchar** array =
    g_variant_get_bytestring_array(const_cast<GVariant*>(gobj()), &n_elements);

  type_vec_string const result(array, array + n_elements);
  g_free(array);
  return result;
}

VariantIter Variant<type_vec_string>::get_iter() const
{
  // Get the variant type of the elements.
  VariantType element_variant_type = Variant<std::string>::variant_type();

  // Get the variant type of the array.
  VariantType array_variant_type = Variant<type_vec_string>::variant_type();

  // Get the GVariantIter.
  GVariantIter* g_iter = 0;
  g_variant_get(const_cast<GVariant*>(gobj()),
    reinterpret_cast<gchar*>(array_variant_type.gobj()), &g_iter);

  return VariantIter(g_iter);
}

} // namespace Glib