summaryrefslogtreecommitdiff
path: root/glib/src/optiongroup.ccg
blob: ee9b7bfe64a8c4ff345d2e1ef1a8242e568df1e4 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* Copyright (C) 2002 The gtkmm 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, see <http://www.gnu.org/licenses/>.
 */

#include <glibmm/optionentry.h>
#include <glibmm/optioncontext.h>
#include <glibmm/utility.h>
#include <glibmm/exceptionhandler.h>
#include <glib.h> // g_malloc
#include <cstring> // std::memset()

namespace Glib
{

namespace // anonymous
{

// A pointer to an OptionArgCallback instance is stored in CppOptionEntry::cpparg_
// when a callback function shall parse the command option's value.
class OptionArgCallback
{
public:
  explicit OptionArgCallback(const OptionGroup::SlotOptionArgString& slot)
  : slot_string_(new OptionGroup::SlotOptionArgString(slot)), slot_filename_(nullptr)
  {
  }

  explicit OptionArgCallback(const OptionGroup::SlotOptionArgFilename& slot)
  : slot_string_(nullptr), slot_filename_(new OptionGroup::SlotOptionArgFilename(slot))
  {
  }

  bool is_filename_option() const { return slot_filename_ != nullptr; }

  const OptionGroup::SlotOptionArgString* get_slot_string() const { return slot_string_; }

  const OptionGroup::SlotOptionArgFilename* get_slot_filename() const { return slot_filename_; }

  ~OptionArgCallback()
  {
    delete slot_string_;
    delete slot_filename_;
  }

private:
  // One of these slot pointers is 0 and the other one points to a slot.
  OptionGroup::SlotOptionArgString* slot_string_;
  OptionGroup::SlotOptionArgFilename* slot_filename_;

  // Not copyable
  OptionArgCallback(const OptionArgCallback&);
  OptionArgCallback& operator=(const OptionArgCallback&);
};

extern "C" {

static gboolean
g_callback_pre_parse(
  GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** error)
{
  OptionContext cppContext(context, false /* take_ownership */);

  auto option_group = static_cast<OptionGroup*>(data);
  if (!option_group)
  {
    OptionError(OptionError::FAILED, "Glib::OptionGroup: g_callback_pre_parse(): "
                                     "No OptionGroup pointer available")
      .propagate(error);
    return false;
  }

  try
  {
    return option_group->on_pre_parse(cppContext);
  }
  catch (Glib::Error& err)
  {
    err.propagate(error);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return false;
}

static void
g_callback_error(
  GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** error)
{
  // GError** error is input data containing information on an error that
  // has occurred before this function is called.

  OptionContext cppContext(context, false /* take_ownership */);

  auto option_group = static_cast<OptionGroup*>(data);
  if (option_group && error && *error)
  {
    try
    {
      Error::throw_exception(g_error_copy(*error));
    }
    catch (const Error& err)
    {
      return option_group->on_error(cppContext, err);
    }
  }
}

const gchar*
OptionGroup_Translate_glibmm_callback(const gchar* string, gpointer data)
{
  Glib::OptionGroup::SlotTranslate* the_slot = static_cast<Glib::OptionGroup::SlotTranslate*>(data);

  try
  {
    // The C docs says that the char* belongs to Glib.
    return g_strdup((*the_slot)(Glib::ustring(string)).c_str());
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }

  return nullptr;
}

static void
OptionGroup_Translate_glibmm_callback_destroy(void* data)
{
  delete static_cast<Glib::OptionGroup::SlotTranslate*>(data);
}

} /* extern "C" */

} // anonymous namespace

// static
gboolean
OptionGroup::post_parse_callback(
  GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** error)
{
  OptionContext cppContext(context, false /* take_ownership */);

  OptionGroup* option_group = static_cast<OptionGroup*>(data);
  if (!option_group)
  {
    OptionError(OptionError::FAILED, "Glib::OptionGroup::post_parse_callback(): "
                                     "No OptionGroup pointer available")
      .propagate(error);
    return false;
  }

  // The C args have now been given values by g_option_context_parse().
  // Convert C values to C++ values:
  for (auto& the_pair : option_group->map_entries_)
  {
    auto& cpp_entry = the_pair.second;
    cpp_entry.convert_c_to_cpp();
  }

  try
  {
    return option_group->on_post_parse(cppContext);
  }
  catch (Glib::Error& err)
  {
    err.propagate(error);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return false;
}

// static
gboolean
OptionGroup::option_arg_callback(
  const gchar* option_name, const gchar* value, gpointer data, GError** error)
{
  const Glib::ustring cpp_option_name(option_name);
  const OptionGroup* const option_group = static_cast<const OptionGroup*>(data);
  if (!option_group)
  {
    OptionError(OptionError::FAILED, "Glib::OptionGroup::option_arg_callback(): "
                                     "No OptionGroup pointer available for option " +
                                       cpp_option_name)
      .propagate(error);
    return false;
  }

  // option_name is either a single dash followed by a single letter (for a
  // short name) or two dashes followed by a long option name.
  OptionGroup::type_map_entries::const_iterator iterFind = option_group->map_entries_.end();
  if (option_name[1] == '-')
  {
    // Long option name.
    const auto long_option_name = Glib::ustring(option_name + 2);
    iterFind = option_group->map_entries_.find(long_option_name);
  }
  else
  {
    // Short option name.
    const auto short_option_name = option_name[1];
    for (iterFind = option_group->map_entries_.begin();
         iterFind != option_group->map_entries_.end(); ++iterFind)
    {
      const auto& cppOptionEntry = iterFind->second;
      if (cppOptionEntry.entry_ && cppOptionEntry.entry_->get_short_name() == short_option_name)
        break;
    }
  }

  if (iterFind == option_group->map_entries_.end())
  {
    OptionError(OptionError::UNKNOWN_OPTION, "Glib::OptionGroup::option_arg_callback(): "
                                             "Unknown option " +
                                               cpp_option_name)
      .propagate(error);
    return false;
  }

  const auto& cppOptionEntry = iterFind->second;
  if (cppOptionEntry.carg_type_ != G_OPTION_ARG_CALLBACK)
  {
    OptionError(OptionError::FAILED, "Glib::OptionGroup::option_arg_callback() "
                                     "called for non-callback option " +
                                       cpp_option_name)
      .propagate(error);
    return false;
  }

  const bool has_value = (value != nullptr);
  const OptionArgCallback* const option_arg =
    static_cast<const OptionArgCallback*>(cppOptionEntry.cpparg_);
  try
  {
    if (option_arg->is_filename_option())
    {
      const auto the_slot = option_arg->get_slot_filename();
      const std::string cpp_value(value ? value : "");
      return (*the_slot)(cpp_option_name, cpp_value, has_value);
    }
    else
    {
      const auto the_slot = option_arg->get_slot_string();
      const Glib::ustring cpp_value(value ? value : "");
      return (*the_slot)(cpp_option_name, cpp_value, has_value);
    }
  }
  catch (Glib::Error& err)
  {
    err.propagate(error);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return false;
}

OptionGroup::OptionGroup(const Glib::ustring& name, const Glib::ustring& description,
  const Glib::ustring& help_description)
: gobject_(g_option_group_new(name.c_str(), description.c_str(), help_description.c_str(),
    this /* user_data */, nullptr /* destroy_func */))
{
  // g_callback_pre_parse(), post_parse_callback(), g_callback_error(), and
  // option_arg_callback() depend on user_data being this. The first three
  // functions get a GOptionGroup*, but it would not be correct to use it for
  // creating a new OptionGroup. They must call their virtual functions in the
  // original OptionGroup instance.

  // Connect callbacks, so that derived classes can override the virtual methods:
  g_option_group_set_parse_hooks(gobj(), &g_callback_pre_parse, &post_parse_callback);
  g_option_group_set_error_hook(gobj(), &g_callback_error);
}

OptionGroup::OptionGroup(GOptionGroup* castitem) : gobject_(castitem)
{
  // Always takes ownership - never takes copy.
}

void
OptionGroup::release_gobject() noexcept
{
  // Free any C types that were allocated during add_entry():
  for (auto& the_pair : map_entries_)
  {
    auto& cpp_entry = the_pair.second;
    cpp_entry.release_c_arg();
  }

  if (gobject_)
  {
    g_option_group_unref(gobj());
    gobject_ = nullptr;
  }
}

OptionGroup::~OptionGroup()
{
  release_gobject();
}

void
OptionGroup::add_entry(const OptionEntry& entry)
{
  // It does not copy the entry, so it needs to live as long as the group.

  // g_option_group_add_entries takes an array, with the last item in the array having a null
  // long_name.
  // Hopefully this will be properly documented eventually - see bug #

  // Create a temporary array, just so we can give the correct thing to g_option_group_add_entries:
  GOptionEntry array[2];
  array[0] = *(entry.gobj()); // Copy contents.
  std::memset(&array[1], 0, sizeof(GOptionEntry));

  g_option_group_add_entries(gobj(), array);
}

void
OptionGroup::add_entry(const OptionEntry& entry, bool& arg)
{
  add_entry_with_wrapper(entry,
    G_OPTION_ARG_NONE /* Actually a boolean on/off, depending on whether the argument name was given, without argument parameters. */,
    &arg);
}

void
OptionGroup::add_entry(const OptionEntry& entry, int& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_INT, &arg);
}

void
OptionGroup::add_entry(const OptionEntry& entry, double& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_DOUBLE, &arg);
}

void
OptionGroup::add_entry(const OptionEntry& entry, Glib::ustring& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_STRING, &arg);
}

void
OptionGroup::add_entry(const OptionEntry& entry, vecustrings& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_STRING_ARRAY, &arg);
}

void
OptionGroup::add_entry_filename(const OptionEntry& entry, std::string& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_FILENAME, &arg);
}

void
OptionGroup::add_entry_filename(const OptionEntry& entry, vecstrings& arg)
{
  add_entry_with_wrapper(entry, G_OPTION_ARG_FILENAME_ARRAY, &arg);
}

// When the command argument value is to be parsed by a user-supplied function
// (indicated by G_OPTION_ARG_CALLBACK), the OptionEntry::Flags::FILENAME in 'entry' is ignored.
// set_c_arg_default() clears or sets it as required in a copy of 'entry'.
//
// The glib API is inconsistent here. The choice between UTF-8 and filename
// encoding is done with G_OPTION_ARG_STRING, G_OPTION_ARG_FILENAME,
// G_OPTION_ARG_STRING_ARRAY, and G_OPTION_ARG_FILENAME_ARRAY, which in glibmm
// are set by OptionGroup::add_entry[_filename]. But when a callback function
// is chosen, there is only G_OPTION_ARG_CALLBACK, and the encoding is chosen
// with OptionEntry::Flags::FILENAME. We do this automatiically in set_c_arg_default().
// Other option flags are set by OptionEntry::set_flags().

void
OptionGroup::add_entry(const OptionEntry& entry, const SlotOptionArgString& slot)
{
  // The OptionArgCallback is deleted in release_c_arg().
  add_entry_with_wrapper(entry, G_OPTION_ARG_CALLBACK, new OptionArgCallback(slot));
}

void
OptionGroup::add_entry_filename(const OptionEntry& entry, const SlotOptionArgFilename& slot)
{
  // The OptionArgCallback is deleted in release_c_arg().
  add_entry_with_wrapper(entry, G_OPTION_ARG_CALLBACK, new OptionArgCallback(slot));
}

void
OptionGroup::add_entry_with_wrapper(const OptionEntry& entry, GOptionArg arg_type, void* cpp_arg)
{
  const auto name = entry.get_long_name();
  type_map_entries::iterator iterFind = map_entries_.find(name);
  if (iterFind == map_entries_.end()) // If we have not added this entry already
  {
    CppOptionEntry cppEntry;
    // g_option_group_add_entry() does not take its own copy, so we must keep the instance alive.
    cppEntry.entry_ = new OptionEntry(entry);
    // cppEntry.entry_ is deleted in release_c_arg(), via the destructor.

    // Several options can refer to the same C++ variable,
    // typically a pair of --enable-x / --disable-x options.
    // Only one C variable shall be allocated for them.
    // See https://bugzilla.gnome.org/show_bug.cgi?id=744854.
    bool is_duplicate = false;
    void* carg = nullptr;
    if (arg_type != G_OPTION_ARG_CALLBACK)
    {
      for (type_map_entries::iterator iter = map_entries_.begin(); iter != map_entries_.end();
           ++iter)
      {
        const auto& cpp_entry = iter->second;
        if (cpp_entry.cpparg_ == cpp_arg && cpp_entry.carg_type_ == arg_type && cpp_entry.carg_)
        {
          is_duplicate = true;
          carg = cpp_entry.carg_;
          break;
        }
      }
    }

    cppEntry.carg_type_ = arg_type;
    if (!is_duplicate)
    {
      cppEntry.allocate_c_arg();
      cppEntry.set_c_arg_default(cpp_arg);
      carg = cppEntry.carg_;
    }
    cppEntry.cpparg_ = cpp_arg;

    // Give the information to the C API:
    cppEntry.entry_->gobj()->arg = arg_type;
    cppEntry.entry_->gobj()->arg_data = carg;

    // Remember the C++/C mapping so that we can use it later:
    map_entries_[name] = cppEntry;

    add_entry(*(cppEntry.entry_));
  }
  else if (arg_type == G_OPTION_ARG_CALLBACK)
  {
    // Delete the OptionArgCallback instance that was allocated by add_entry()
    // or add_entry_filename().
    auto option_arg = static_cast<OptionArgCallback*>(cpp_arg);
    delete option_arg;
  }
}

bool
OptionGroup::on_pre_parse(OptionContext& /* context */)
{
  return true;
}

bool
OptionGroup::on_post_parse(OptionContext& /* context */)
{
  return true;
}

void
OptionGroup::on_error(OptionContext& /* context */, const Error& /* error */)
{
}

void
OptionGroup::set_translate_func(const SlotTranslate& slot)
{
  // Create a copy of the slot. A pointer to this will be passed through the
  // callback's data parameter.  It will be deleted when
  // OptionGroup_Translate_glibmm_callback_destroy() is called.
  auto slot_copy = new SlotTranslate(slot);
  g_option_group_set_translate_func(gobj(), &OptionGroup_Translate_glibmm_callback, slot_copy,
    &OptionGroup_Translate_glibmm_callback_destroy);
}

OptionGroup::CppOptionEntry::CppOptionEntry()
: carg_type_(G_OPTION_ARG_NONE), carg_(nullptr), cpparg_(nullptr), entry_(nullptr)
{
}

void
OptionGroup::CppOptionEntry::allocate_c_arg()
{
  // Create an instance of the appropriate C type.
  // This will be destroyed in the OptionGroup destructor.
  //
  // We must also call set_c_arg_default() to give these C types the specified
  // defaults based on the C++-typed arguments.
  switch (carg_type_)
  {
  case G_OPTION_ARG_STRING: // The char* will be for UTF8 a string.
  case G_OPTION_ARG_FILENAME: // The char* will be for a string in the current locale's encoding.
  {
    char** typed_arg = new char*;
    // The C code will allocate a char* and put it here, for us to g_free() later.
    *typed_arg = nullptr;
    carg_ = typed_arg;

    break;
  }
  case G_OPTION_ARG_INT:
  {
    int* typed_arg = new int;
    *typed_arg = 0;
    carg_ = typed_arg;

    break;
  }
  case G_OPTION_ARG_DOUBLE:
  {
    double* typed_arg = new double;
    *typed_arg = 0.0;
    carg_ = typed_arg;

    break;
  }
  case G_OPTION_ARG_STRING_ARRAY:
  case G_OPTION_ARG_FILENAME_ARRAY:
  {
    char*** typed_arg = new char**;
    // The C code will allocate a char** and put it here, for us to g_strfreev() later.
    *typed_arg = nullptr;
    carg_ = typed_arg;

    break;
  }
  case G_OPTION_ARG_NONE: // Actually a boolean.
  {
    gboolean* typed_arg = new gboolean;
    *typed_arg = false;
    carg_ = typed_arg;

    break;
  }
  case G_OPTION_ARG_CALLBACK:
  {
    // The C arg pointer is a function pointer, cast to void*.
    //
    //   carg_ = reinterpret_cast<void*>(&OptionGroup::option_arg_callback);
    // or
    //   union {
    //     void* dp;
    //     GOptionArgFunc fp;
    //   } u;
    //   u.fp = &OptionGroup::option_arg_callback;
    //   carg_ = u.dp;
    // ? See
    // https://bugzilla.gnome.org/show_bug.cgi?id=589197
    // https://github.com/libsigcplusplus/libsigcplusplus/issues/1
    // https://github.com/libsigcplusplus/libsigcplusplus/issues/8
    carg_ = reinterpret_cast<void*>(&OptionGroup::option_arg_callback);

    break;
  }
  default:
  {
    break;
  }
  }
}

void
OptionGroup::CppOptionEntry::set_c_arg_default(void* cpp_arg)
{
  switch (carg_type_)
  {
  case G_OPTION_ARG_INT:
  {
    *static_cast<int*>(carg_) = *static_cast<int*>(cpp_arg);
    break;
  }
  case G_OPTION_ARG_DOUBLE:
  {
    *static_cast<double*>(carg_) = *static_cast<double*>(cpp_arg);
    break;
  }
  case G_OPTION_ARG_NONE:
  {
    *static_cast<gboolean*>(carg_) = *static_cast<bool*>(cpp_arg);
    break;
  }
  case G_OPTION_ARG_STRING:
  case G_OPTION_ARG_FILENAME:
  case G_OPTION_ARG_STRING_ARRAY:
  case G_OPTION_ARG_FILENAME_ARRAY:
  {
    // No need to set default values for string-valued options.
    // If *carg_ is still 0, when convert_c_to_cpp() is called, just don't
    // touch *cpparg_. Besides, setting default values in *carg_ can result
    // in memory leaks, because glib would not free the strings before
    // the char*'s are overwritten with pointers to newly allocated copies
    // of the command option arguments.
    break;
  }
  case G_OPTION_ARG_CALLBACK:
  {
    // No value to set here. The arg pointer is a function pointer.

    // Set or clear OptionEntry::Flags::FILENAME in *entry_.
    const OptionArgCallback* const option_arg = static_cast<const OptionArgCallback*>(cpp_arg);
    if (option_arg->is_filename_option())
    {
      entry_->set_flags(entry_->get_flags() | OptionEntry::Flags::FILENAME);
    }
    else
    {
      entry_->set_flags(entry_->get_flags() & ~OptionEntry::Flags::FILENAME);
    }
    break;
  }
  default:
  {
    break;
  }
  }
}

void
OptionGroup::CppOptionEntry::release_c_arg()
{
  // Delete the instances that we created in allocate_c_arg().
  // Notice that we delete the type that we created, but not the value to which it points.
  if (carg_)
  {
    switch (carg_type_)
    {
    case G_OPTION_ARG_STRING:
    case G_OPTION_ARG_FILENAME:
    {
      char** typed_arg = static_cast<char**>(carg_);
      g_free(*typed_arg); // Free the char* string at typed_arg, if allocated by the C code.
      delete typed_arg; // Delete the char** that we allocated in allocate_c_arg().

      break;
    }
    case G_OPTION_ARG_INT:
    {
      int* typed_arg = static_cast<int*>(carg_);
      delete typed_arg;

      break;
    }
    case G_OPTION_ARG_DOUBLE:
    {
      double* typed_arg = static_cast<double*>(carg_);
      delete typed_arg;

      break;
    }
    case G_OPTION_ARG_STRING_ARRAY:
    case G_OPTION_ARG_FILENAME_ARRAY:
    {
      char*** typed_arg = static_cast<char***>(carg_);
      g_strfreev(*typed_arg); // Free the array of strings and the array at typed_arg, if allocated
                              // by the C code.
      delete typed_arg; // Delete the char*** that we allocated in allocate_c_arg().

      break;
    }
    case G_OPTION_ARG_NONE: // Actually a boolean.
    {
      gboolean* typed_arg = static_cast<gboolean*>(carg_);
      delete typed_arg;

      break;
    }
    case G_OPTION_ARG_CALLBACK:
    {
      // Delete the OptionArgCallback instance that was allocated by add_entry()
      // or add_entry_filename().
      auto option_arg = static_cast<OptionArgCallback*>(cpparg_);
      delete option_arg;
      cpparg_ = nullptr;

      break;
    }
    default:
    {
      break;
    }
    }

    carg_ = nullptr;
  }

  if (entry_)
    delete entry_;
}

void
OptionGroup::CppOptionEntry::convert_c_to_cpp()
{
  if (!carg_)
    return;

  switch (carg_type_)
  {
  case G_OPTION_ARG_STRING:
  {
    char** typed_arg = static_cast<char**>(carg_);
    auto typed_cpp_arg = static_cast<Glib::ustring*>(cpparg_);
    if (typed_arg && *typed_arg && typed_cpp_arg)
    {
      *typed_cpp_arg = *typed_arg;
    }
    break;
  }
  case G_OPTION_ARG_FILENAME:
  {
    char** typed_arg = static_cast<char**>(carg_);
    auto typed_cpp_arg = static_cast<std::string*>(cpparg_);
    if (typed_arg && *typed_arg && typed_cpp_arg)
    {
      *typed_cpp_arg = *typed_arg;
    }
    break;
  }
  case G_OPTION_ARG_INT:
  {
    *((int*)cpparg_) = *(static_cast<int*>(carg_));
    break;
  }
  case G_OPTION_ARG_DOUBLE:
  {
    *((double*)cpparg_) = *(static_cast<double*>(carg_));
    break;
  }
  case G_OPTION_ARG_STRING_ARRAY:
  {
    char*** typed_arg = static_cast<char***>(carg_);
    auto typed_cpp_arg = static_cast<vecustrings*>(cpparg_);
    if (typed_arg && *typed_arg && typed_cpp_arg)
    {
      typed_cpp_arg->clear();

      // The C array is null-terminated.
      // std::vector<Glib::ustring> array_handle(*typed_arg,  Glib::OWNERSHIP_NONE);

      // The SUN Forte compiler complains about this:
      // "optiongroup.cc", line 354: Error: Cannot assign Glib::ArrayHandle<Glib::ustring,
      // Glib::Container_Helpers::TypeTraits<Glib::ustring>> to std::vector<Glib::ustring> without
      // "std::vector<Glib::ustring>::operator=(const std::vector<Glib::ustring>&)";.
      //
      //(*typed_cpp_arg) = array_handle;
      //
      // And the Tru64 compiler does not even like us to instantiate the StringArrayHandle:
      //
      // cxx: Error: ../../glib/glibmm/containerhandle_shared.h, line 149: the operand
      //     of a pointer dynamic_cast must be a pointer to a complete class type
      //   return dynamic_cast<CppType>(Glib::wrap_auto(cobj, false /* take_copy */));

      // for(auto iter = array_handle.begin(); iter !=
      // array_handle.end(); ++iter)
      //{
      //  typed_cpp_arg->emplace_back(*iter);
      //}

      // So we do this:

      char** char_array_next = *typed_arg;
      while (*char_array_next)
      {
        typed_cpp_arg->emplace_back(*char_array_next);
        ++char_array_next;
      }
    }
    break;
  }
  case G_OPTION_ARG_FILENAME_ARRAY:
  {
    char*** typed_arg = static_cast<char***>(carg_);
    auto typed_cpp_arg = static_cast<vecstrings*>(cpparg_);
    if (typed_arg && *typed_arg && typed_cpp_arg)
    {
      typed_cpp_arg->clear();

      // See comments above about the SUN Forte and Tru64 compilers.

      char** char_array_next = *typed_arg;
      while (*char_array_next)
      {
        typed_cpp_arg->emplace_back(*char_array_next);
        ++char_array_next;
      }
    }
    break;
  }
  case G_OPTION_ARG_NONE: // Actually a boolean.
  {
    *(static_cast<bool*>(cpparg_)) = *(static_cast<gboolean*>(carg_));
    break;
  }
  case G_OPTION_ARG_CALLBACK:
  {
    // Nothing to convert here. That's a task for the callback function
    //(the SlotOptionArgString or SlotOptionArgFilename).
    break;
  }
  default:
  {
    break;
  }
  }
}

GOptionGroup*
OptionGroup::gobj_copy() const
{
  return g_option_group_ref(gobject_);
}

} // namespace Glib