summaryrefslogtreecommitdiff
path: root/glib/src/optiongroup.hg
blob: 54a830f5187ae7a9fa563ccbf29e19ce50ebaeb8 (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
/* Copyright (C) 2004 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, see <http://www.gnu.org/licenses/>.
 */

_DEFS(glibmm,glib)

#include <glibmm/error.h>
#include <glibmm/ustring.h>
#include <sigc++/slot.h>
#include <map>
#include <vector>
#include <glib.h>

#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" { typedef struct _GOptionGroup GOptionGroup; }
#endif //DOXYGEN_SHOULD_SKIP_THIS


namespace Glib
{

#ifndef DOXYGEN_SHOULD_SKIP_THIS
class OptionEntry;
class OptionContext;
#endif //DOXYGEN_SHOULD_SKIP_THIS

// GOptionGroup is now refcounted. See https://bugzilla.gnome.org/show_bug.cgi?id=743349
// But Glib::OptionGroup can't be a _CLASS_OPAQUE_REFCOUNTED, because it has
// other data members than gobject_. The memory management would be more involved
// if Glib::OptionGroup were refcounted. It would need a separate refcount for the
// wrapper, like in Glib::IOChannel and Glib::Source. It's easier and good enough
// to let Glib::OptionGroup remain non-refcounted. GOptionGroup's refcount still helps.
// E.g. when GOptionContext takes ownership of a GOptionGroup, it's given a ref.
// A drawback with this approach is that it's possible to have more than one wrapper
// for one GOptionGroup, each wrapper with its own ref.

/** An OptionGroup defines the options in a single group.
 * Libraries which need to parse commandline options are expected to provide a function that allows their OptionGroups to
 * be added to the application's OptionContext.
 */
class GLIBMM_API OptionGroup
{
  _CLASS_GENERIC(OptionGroup, GOptionGroup)
public:
  /** For example Glib::ustring on_translate(const Glib::ustring& original);.
   */
  using SlotTranslate = sigc::slot<Glib::ustring(const Glib::ustring&)>;

  /** For example bool on_option_arg_string(const Glib::ustring& option_name,
   *  const Glib::ustring& value, bool has_value);.
   */
  using SlotOptionArgString = sigc::slot<bool(const Glib::ustring&, const Glib::ustring&, bool)>;

  /** For example bool on_option_arg_filename(const Glib::ustring& option_name,
   *  const std::string& value, bool has_value);.
   */
  using SlotOptionArgFilename = sigc::slot<bool(const Glib::ustring&, const std::string&, bool)>;

  OptionGroup(const Glib::ustring& name, const Glib::ustring& description, const Glib::ustring& help_description = {});

  /** This always takes ownership of the underlying GOptionGroup,
   * so it is only useful with C functions that return newly-allocated GOptionGroups.
   */
  explicit OptionGroup(GOptionGroup* castitem);
  _IGNORE(g_option_group_new, g_option_group_ref)

  // OptionGroup can't be copied or moved. The underlying GOptionGroup contains
  // a pointer to the wrapper, set in the call to g_option_group_new().
  // That pointer can't be changed.
  OptionGroup(const OptionGroup& other) = delete;
  OptionGroup& operator=(const OptionGroup& other) = delete;
  OptionGroup(OptionGroup&& other) = delete;
  OptionGroup& operator=(OptionGroup&& other) = delete;

  virtual ~OptionGroup();
  _IGNORE(g_option_group_free, g_option_group_unref)


  virtual bool on_pre_parse(OptionContext& context);
  virtual bool on_post_parse(OptionContext& context);
  virtual void on_error(OptionContext& context, const Error& error);
  _IGNORE(g_option_group_set_parse_hooks, g_option_group_set_error_hook)

  void add_entry(const OptionEntry& entry);
  _IGNORE(g_option_group_add_entries)


  using vecustrings = std::vector<Glib::ustring>;
  using vecstrings = std::vector<std::string>;

  /** Add a boolean option @a entry.
   * If the option exists among the parsed arguments, the @a arg parameter will
   * be set to <tt>true</tt> or, if @a entry contains OptionEntry::FLAG_REVERSE,
   * to <tt>false</tt> after OptionContext::parse() has returned.
   */
  void add_entry(const OptionEntry& entry, bool& arg);

  /** Add an integer option @a entry.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   */
  void add_entry(const OptionEntry& entry, int& arg);

  /** Add a double option @a entry.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   */
  void add_entry(const OptionEntry& entry, double& arg);

  /** Add a UTF-8 string option @a entry.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   *
   * As indicated by the Glib::ustring type, the string will be
   * UTF-8 encoded.
   */
  void add_entry(const OptionEntry& entry, Glib::ustring& arg);

  /** Add a filename string option @a entry.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   *
   * The string will be in glib's filename encoding.
   */
  void add_entry_filename(const OptionEntry& entry, std::string& arg);

  /** Add an option @a entry that provides a list of UTF-8 strings.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   *
   * As indicated by the Glib::ustring type, the strings will be
   * UTF-8 encoded.
   */
  void add_entry(const OptionEntry& entry, vecustrings& arg);

  /** Add an option @a entry that provides a list of filename strings.
   * The @a arg parameter will be set to the option's extra argument
   * after OptionContext::parse() has returned.
   *
   * The strings will be in glib's filename encoding.
   */
  void add_entry_filename(const OptionEntry& entry, vecstrings& arg);

  /** Add a string option @a entry, but let a callback slot parse the extra argument
   * instead of just setting a variable to its value.
   */
  void add_entry(const OptionEntry& entry, const SlotOptionArgString& slot);

  /** Add a filename option @a entry, but let a callback slot parse the extra argument
   * instead of just setting a variable to its value.
   */
  void add_entry_filename(const OptionEntry& entry, const SlotOptionArgFilename& slot);

  /** Sets the function which is used to translate user-visible strings, for
   * --help output. Different groups can use a different SlotTranslate. If a
   * translate function is not set, strings are not translated.
   *
   * If you are using gettext(), you only need to set the translation domain,
   * see set_translation_domain().
   *
   * @param slot the slot to be used for translation.
   *
   * @newin{2,28}
   */
  void set_translate_func(const SlotTranslate& slot);
  _IGNORE(g_option_group_set_translate_func)

  _WRAP_METHOD(void set_translation_domain(const Glib::ustring& domain), g_option_group_set_translation_domain)

  /// Provides access to the underlying C instance.
  GOptionGroup*       gobj()       { return gobject_; }

  /// Provides access to the underlying C instance.
  const GOptionGroup* gobj() const { return gobject_; }

  /// Provides access to the underlying C instance. The caller is responsible for unrefing it.
  GOptionGroup*       gobj_copy() const;

protected:

#ifndef DOXYGEN_SHOULD_SKIP_THIS
  /** This is not public API. It is an implementation detail.
   */
  class CppOptionEntry
  {
  public:
    CppOptionEntry();

    void allocate_c_arg();
    void set_c_arg_default(void* cpp_arg);
    void convert_c_to_cpp();
    void release_c_arg();

    GOptionArg carg_type_;
    void* carg_;
    void* cpparg_;
    OptionEntry* entry_;
  };

  void add_entry_with_wrapper(const OptionEntry& entry, GOptionArg arg_type, void* cpp_arg);

  static gboolean post_parse_callback(GOptionContext* context,
    GOptionGroup* group, gpointer data, GError** error);

  static gboolean option_arg_callback(const gchar* option_name, const gchar* value,
    gpointer data, GError** error);

  //Map of entry names to CppOptionEntry:
  using type_map_entries = std::map<Glib::ustring, CppOptionEntry>;
  type_map_entries map_entries_;

  GOptionGroup* gobject_;
#endif //DOXYGEN_SHOULD_SKIP_THIS

private:
  void release_gobject() noexcept;
};

} // namespace Glib