2 /* $Id: optiongroup.ccg,v 1.15.4.3 2006/03/30 12:19:58 murrayc Exp $ */
4 /* Copyright (C) 2002 The gtkmm Development Team
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <glibmm/optionentry.h>
22 #include <glibmm/optioncontext.h>
23 #include <glibmm/utility.h>
24 //#include <glibmm/containers.h>
25 #include <glib/gmem.h> // g_malloc
26 #include <glib/goption.h>
37 static gboolean g_callback_pre_parse(GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** /* TODO error */)
39 OptionContext cppContext(context, false /* take_ownership */);
40 //OptionGroup cppGroup(group, true /* take_copy */); //Maybe this should be option_group.
42 OptionGroup* option_group = static_cast<OptionGroup*>(data);
44 return option_group->on_pre_parse(cppContext, *option_group);
49 static gboolean g_callback_post_parse(GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** /* TODO error */)
51 OptionContext cppContext(context, false /* take_ownership */);
52 //OptionGroup cppGroup(group, true /* take_copy */); //Maybe this should be option_group.
54 OptionGroup* option_group = static_cast<OptionGroup*>(data);
57 return option_group->on_post_parse(cppContext, *option_group);
63 static void g_callback_error(GOptionContext* context, GOptionGroup* /* group */, gpointer data, GError** /* TODO error*/)
65 OptionContext cppContext(context, false /* take_ownership */);
66 //OptionGroup cppGroup(group); //Maybe this should be option_group.
68 OptionGroup* option_group = static_cast<OptionGroup*>(data);
70 return option_group->on_error(cppContext, *option_group);
75 } //anonymous namespace
78 OptionGroup::OptionGroup(const Glib::ustring& name, const Glib::ustring& description, const Glib::ustring& help_description)
79 : gobject_( g_option_group_new(name.c_str(), description.c_str(), help_description.c_str(), this, 0 /* destroy_func */) ),
82 //Connect callbacks, so that derived classes can override the virtual methods:
83 g_option_group_set_parse_hooks(gobj(), &g_callback_pre_parse, &g_callback_post_parse);
84 g_option_group_set_error_hook(gobj(), &g_callback_error);
87 OptionGroup::OptionGroup(GOptionGroup* castitem)
91 //Always takes ownership - never takes copy.
95 OptionGroup::~OptionGroup()
97 //Free any C types that were allocated during add_entry():
98 for(type_map_entries::iterator iter = map_entries_.begin(); iter != map_entries_.end(); ++iter)
100 CppOptionEntry& cpp_entry = iter->second;
101 cpp_entry.release_c_arg();
106 g_option_group_free(gobj());
111 void OptionGroup::add_entry(const OptionEntry& entry)
113 //It does not copy the entry, so it needs to live as long as the group.
115 //g_option_group_add_entries takes an array, with the last item in the array having a null long_name.
116 //Hopefully this will be properly documented eventually - see bug #
118 //Create a temporary array, just so we can give the correct thing to g_option_group_add_entries:
119 GOptionEntry array[2];
120 array[0] = *(entry.gobj()); //Copy contents.
121 GLIBMM_INITIALIZE_STRUCT(array[1], GOptionEntry);
123 g_option_group_add_entries(gobj(), array);
126 void OptionGroup::add_entry(const OptionEntry& entry, bool& arg)
128 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);
131 void OptionGroup::add_entry(const OptionEntry& entry, int& arg)
133 add_entry_with_wrapper(entry, G_OPTION_ARG_INT, &arg);
136 void OptionGroup::add_entry(const OptionEntry& entry, Glib::ustring& arg)
138 add_entry_with_wrapper(entry, G_OPTION_ARG_STRING, &arg);
141 void OptionGroup::add_entry(const OptionEntry& entry, vecustrings& arg)
143 add_entry_with_wrapper(entry, G_OPTION_ARG_STRING_ARRAY, &arg);
146 void OptionGroup::add_entry_filename(const OptionEntry& entry, std::string& arg)
148 add_entry_with_wrapper(entry, G_OPTION_ARG_FILENAME, &arg);
151 void OptionGroup::add_entry_filename(const OptionEntry& entry, vecstrings& arg)
153 add_entry_with_wrapper(entry, G_OPTION_ARG_FILENAME_ARRAY, &arg);
156 void OptionGroup::add_entry_with_wrapper(const OptionEntry& entry, GOptionArg arg_type, void* cpp_arg)
158 const Glib::ustring name = entry.get_long_name();
159 type_map_entries::iterator iterFind = map_entries_.find(name);
160 if( iterFind == map_entries_.end() ) //If we have not added this entry already
162 CppOptionEntry cppEntry;
163 cppEntry.carg_type_ = arg_type;
164 cppEntry.allocate_c_arg();
165 cppEntry.set_c_arg_default(cpp_arg);
167 cppEntry.cpparg_ = cpp_arg;
169 //Give the information to the C API:
171 cppEntry.entry_ = new OptionEntry(entry); //g_option_group_add_entry() does not take its own copy, so we must keep the instance alive. */
172 //cppEntry.entry_ is deleted in release_c_arg(), via the destructor.
174 cppEntry.entry_->gobj()->arg = arg_type;
175 cppEntry.entry_->gobj()->arg_data = cppEntry.carg_;
177 //Remember the C++/C mapping so that we can use it later:
178 map_entries_[name] = cppEntry;
180 add_entry(*(cppEntry.entry_));
185 bool OptionGroup::on_pre_parse(OptionContext& /* context */, OptionGroup& /* group */)
190 bool OptionGroup::on_post_parse(OptionContext& /* context */, OptionGroup& /* group */)
192 //Call this at the start of overrides.
194 //TODO: Maybe put this in the C callback:
196 //The C args have now been given values by GOption.
197 //Convert C values to C++ values:
199 for(type_map_entries::iterator iter = map_entries_.begin(); iter != map_entries_.end(); ++iter)
201 CppOptionEntry& cpp_entry = iter->second;
202 cpp_entry.convert_c_to_cpp();
208 void OptionGroup::on_error(OptionContext& /* context */, OptionGroup& /* group */)
213 OptionGroup::CppOptionEntry::CppOptionEntry()
214 : carg_type_(G_OPTION_ARG_NONE), carg_(0), cpparg_(0), entry_(0)
217 void OptionGroup::CppOptionEntry::allocate_c_arg()
219 //Create an instance of the appropriate C type.
220 //This will be destroyed in the OptionGroup destructor.
222 //We must also call set_c_arg_default() to give these C types the specified defaults based on the C++-typed arguments.
225 case G_OPTION_ARG_STRING: //The char* will be for UTF8 strins.
226 case G_OPTION_ARG_FILENAME: //The char* will be for strings in the current locale's encoding.
228 char** typed_arg = new char*;
229 //The C code will allocate a char* and put it here, for us to g_free() later.
230 //Alternatively, set_c_arg_default() might allocate a char*, and the C code might or might not free and replace that.
236 case G_OPTION_ARG_INT:
238 int* typed_arg = new int;
244 case G_OPTION_ARG_STRING_ARRAY:
245 case G_OPTION_ARG_FILENAME_ARRAY:
247 char*** typed_arg = new char**;
253 case G_OPTION_ARG_NONE: /* Actually a boolean. */
255 gboolean* typed_arg = new gboolean;
268 void OptionGroup::CppOptionEntry::set_c_arg_default(void* cpp_arg)
272 case G_OPTION_ARG_INT:
274 *static_cast<int*>(carg_) = *static_cast<int*>(cpp_arg);
277 case G_OPTION_ARG_NONE:
279 *static_cast<gboolean*>(carg_) = *static_cast<bool*>(cpp_arg);
282 case G_OPTION_ARG_STRING:
284 Glib::ustring* typed_cpp_arg = static_cast<Glib::ustring*>(cpp_arg);
285 if(typed_cpp_arg && !typed_cpp_arg->empty())
287 const char** typed_c_arg = static_cast<const char**>(carg_);
288 *typed_c_arg = g_strdup(typed_cpp_arg->c_str()); //Freed in release_c_arg().
292 case G_OPTION_ARG_FILENAME:
294 std::string* typed_cpp_arg = static_cast<std::string*>(cpp_arg);
295 if(typed_cpp_arg && !typed_cpp_arg->empty())
297 const char** typed_c_arg = static_cast<const char**>(carg_);
298 *typed_c_arg = g_strdup(typed_cpp_arg->c_str()); //Freed in release_c_arg().
302 case G_OPTION_ARG_STRING_ARRAY:
304 std::vector<Glib::ustring>* typed_cpp_arg = static_cast<std::vector<Glib::ustring>*>(cpp_arg);
307 std::vector<Glib::ustring>& vec = *typed_cpp_arg;
308 const char** array = static_cast<const char**>( g_malloc(sizeof(gchar*) * (vec.size() + 1)) );
310 for(std::vector<Glib::ustring>::size_type i = 0; i < vec.size(); ++i)
312 array[i] = g_strdup( vec[i].c_str() );
315 array[vec.size()] = 0;
317 const char*** typed_c_arg = static_cast<const char***>(carg_);
318 *typed_c_arg = array;
322 case G_OPTION_ARG_FILENAME_ARRAY:
324 std::vector<std::string>* typed_cpp_arg = static_cast<std::vector<std::string>*>(cpp_arg);
327 std::vector<std::string>& vec = *typed_cpp_arg;
328 const char** array = static_cast<const char**>( g_malloc(sizeof(gchar*) * (vec.size() + 1)) );
330 for(std::vector<Glib::ustring>::size_type i = 0; i < vec.size(); ++i)
332 array[i] = g_strdup( vec[i].c_str() );
335 array[vec.size()] = 0;
337 const char*** typed_c_arg = static_cast<const char***>(carg_);
338 *typed_c_arg = array;
349 void OptionGroup::CppOptionEntry::release_c_arg()
351 //Delete the instances that we created in allocate_c_arg().
352 //Notice that we delete the type that we created, but not the value to which it points.
357 case G_OPTION_ARG_STRING:
358 case G_OPTION_ARG_FILENAME:
360 char** typed_arg = static_cast<char**>(carg_);
361 g_free(*typed_arg); //Free the char* string at type_arg, which was allocated by the C code.
362 delete typed_arg; //Delete the char** that we allocated in allocate_c_arg;
366 case G_OPTION_ARG_INT:
368 int* typed_arg = static_cast<int*>(carg_);
373 case G_OPTION_ARG_STRING_ARRAY:
374 case G_OPTION_ARG_FILENAME_ARRAY:
376 delete (char**)carg_;
379 case G_OPTION_ARG_NONE: /* Actually a boolean. */
381 gboolean* typed_arg = static_cast<gboolean*>(carg_);
389 G_OPTION_ARG_CALLBACK,
402 void OptionGroup::CppOptionEntry::convert_c_to_cpp()
406 case G_OPTION_ARG_STRING:
408 char** typed_arg = static_cast<char**>(carg_);
409 Glib::ustring* typed_cpp_arg = static_cast<Glib::ustring*>(cpparg_);
410 if(typed_arg && typed_cpp_arg)
412 char* pch = *typed_arg;
413 (*typed_cpp_arg) = Glib::convert_const_gchar_ptr_to_ustring(pch);
418 case G_OPTION_ARG_FILENAME:
420 char** typed_arg = static_cast<char**>(carg_);
421 std::string* typed_cpp_arg = static_cast<std::string*>(cpparg_);
422 if(typed_arg && typed_cpp_arg)
424 char* pch = *typed_arg;
425 (*typed_cpp_arg) = Glib::convert_const_gchar_ptr_to_stdstring(pch);
430 case G_OPTION_ARG_INT:
432 *((int*)cpparg_) = *(static_cast<int*>(carg_));
435 case G_OPTION_ARG_STRING_ARRAY:
437 char*** typed_arg = static_cast<char***>(carg_);
438 vecustrings* typed_cpp_arg = static_cast<vecustrings*>(cpparg_);
439 if(typed_arg && typed_cpp_arg)
441 typed_cpp_arg->clear();
443 //The C array seems to be null-terminated.
444 //Glib::StringArrayHandle array_handle(*typed_arg, Glib::OWNERSHIP_NONE);
446 //The SUN Forte compiler complains about this:
447 // "optiongroup.cc", line 354: Error: Cannot assign Glib::ArrayHandle<Glib::ustring,
448 // Glib::Container_Helpers::TypeTraits<Glib::ustring>> to std::vector<Glib::ustring> without
449 // "std::vector<Glib::ustring>::operator=(const std::vector<Glib::ustring>&)";.
451 //(*typed_cpp_arg) = array_handle;
453 //And the Tru64 compiler does not even like us to instantiate the StringArrayHandle:
455 // cxx: Error: ../../glib/glibmm/containerhandle_shared.h, line 149: the operand
456 // of a pointer dynamic_cast must be a pointer to a complete class type
457 // return dynamic_cast<CppType>(Glib::wrap_auto(cobj, false /* take_copy */));
459 //for(Glib::StringArrayHandle::iterator iter = array_handle.begin(); iter != array_handle.end(); ++iter)
461 // typed_cpp_arg->push_back(*iter);
466 char** char_array_next = *typed_arg;
467 while(char_array_next && *char_array_next)
469 typed_cpp_arg->push_back(*char_array_next);
476 case G_OPTION_ARG_FILENAME_ARRAY:
478 char*** typed_arg = static_cast<char***>(carg_);
479 vecustrings* typed_cpp_arg = static_cast<vecustrings*>(cpparg_);
480 if(typed_arg && typed_cpp_arg)
482 typed_cpp_arg->clear();
484 //See comments above about the SUN Forte and Tru64 compilers.
486 char** char_array_next = *typed_arg;
487 while(char_array_next && *char_array_next)
489 typed_cpp_arg->push_back(*char_array_next);
496 case G_OPTION_ARG_NONE: /* Actually a boolean. */
498 *(static_cast<bool*>(cpparg_)) = *(static_cast<gboolean*>(carg_));
504 G_OPTION_ARG_CALLBACK,
511 GOptionGroup* OptionGroup::gobj_give_ownership()
513 has_ownership_ = false;