summaryrefslogtreecommitdiff
path: root/glib/src/optionentry.ccg
blob: 49810c4db0b5f54abbd58d2c277b834b944a6fda (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
/* 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, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

namespace Glib
{

OptionEntry::OptionEntry()
{
  gobject_ = g_new0(GOptionEntry, 1);
}

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

void OptionEntry::release_gobject() noexcept
{
  if(!gobject_)
    return;

  g_free(const_cast<char*>(gobject_->long_name));
  g_free(const_cast<char*>(gobject_->description));
  g_free(const_cast<char*>(gobject_->arg_description));
  g_free(gobject_);
}

OptionEntry::OptionEntry(const OptionEntry& src)
{
  gobject_ = g_new0(GOptionEntry, 1);

  operator=(src);
}

OptionEntry& OptionEntry::operator=(const OptionEntry& src)
{
  if(this != &src)
  {
    if(gobject_->long_name)
      g_free(const_cast<char*>(gobject_->long_name));

    gobject_->long_name = g_strdup(src.gobject_->long_name);

    gobject_->short_name = src.gobject_->short_name; //It's just one char.

    gobject_->flags = src.gobject_->flags;
    gobject_->arg = src.gobject_->arg;
    gobject_->arg_data = src.gobject_->arg_data; //Shared, because it's not owned by any instance of this class anyway.

    if(gobject_->description)
      g_free(const_cast<char*>(gobject_->description));

    gobject_->description = g_strdup(src.gobject_->description);

    if(gobject_->arg_description)
      g_free(const_cast<char*>(gobject_->arg_description));

    gobject_->arg_description = g_strdup(src.gobject_->arg_description);
  }

  return *this;
}

OptionEntry::OptionEntry(OptionEntry&& other) noexcept
: gobject_(std::move(other.gobject_))
{
  other.gobject_ = nullptr;
}

OptionEntry& OptionEntry::operator=(OptionEntry&& other) noexcept
{
  release_gobject();

  gobject_ = std::move(other.gobject_);
  other.gobject_ = nullptr;

  return *this;
}

void OptionEntry::set_long_name(const Glib::ustring& value)
{
  if(gobject_->long_name)
  {
    g_free((gchar*)(gobject_->long_name));
    gobject_->long_name = nullptr;
  }

  //Note that we do not use nullptr for an empty string,
  //because G_OPTION_REMAINING is actually a "", so it actually has a distinct meaning:
  //TODO: Wrap G_OPTION_REMAINING in C++ somehow, maybe as an explicit set_long_name(void) or set_is_remaining()? murrayc.
  gobj()->long_name = (value).c_str() ? g_strdup((value).c_str()) : nullptr;
}

void OptionEntry::set_description(const Glib::ustring& value)
{
  if(gobject_->description)
  {
    g_free((gchar*)(gobject_->description));
    gobject_->description = nullptr;
  }

  gobj()->description = (value).empty() ? nullptr : g_strdup((value).c_str());
}

void OptionEntry::set_arg_description(const Glib::ustring& value)
{
  if(gobject_->arg_description)
  {
    g_free((gchar*)(gobject_->arg_description));
    gobject_->arg_description = nullptr;
  }

  gobj()->arg_description = (value).empty() ? nullptr : g_strdup((value).c_str());
}


} // namespace Glib