diff options
author | José Alburquerque <jaalburqu@svn.gnome.org> | 2010-06-17 17:54:45 -0400 |
---|---|---|
committer | José Alburquerque <jaalburqu@svn.gnome.org> | 2010-06-17 17:54:45 -0400 |
commit | e161cd38da095d7a351d28d726f629a68f4eee7c (patch) | |
tree | 5d8c51bcd6ca47abbe5331715688506e95439317 | |
parent | 5dabbc18913738b4b0dd0277442f95474bcb7a1b (diff) | |
download | glibmm-e161cd38da095d7a351d28d726f629a68f4eee7c.tar.gz |
Preliminary implementation of Glib::Variant<> classes.
* glib/glibmm/variant.cc:
* glib/glibmm/variant.h:
* glib/src/variant_basictypes.cc.m4:
* glib/src/variant_basictypes.h.m4: Add an initial implementation of
of the Glib::Variant<> classes which would wrap glib's GVariant API.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | glib/glibmm/filelist.am | 2 | ||||
-rw-r--r-- | glib/glibmm/variant.cc | 122 | ||||
-rw-r--r-- | glib/glibmm/variant.h | 146 | ||||
-rw-r--r-- | glib/src/filelist.am | 5 | ||||
-rw-r--r-- | glib/src/variant_basictypes.cc.m4 | 68 | ||||
-rw-r--r-- | glib/src/variant_basictypes.h.m4 | 69 |
7 files changed, 420 insertions, 2 deletions
@@ -1,3 +1,13 @@ +2010-06-17 José Alburquerque <jaalburqu@svn.gnome.org> + + Preliminary implementation of Glib::Variant<> classes. + + * glib/glibmm/variant.cc: + * glib/glibmm/variant.h: + * glib/src/variant_basictypes.cc.m4: + * glib/src/variant_basictypes.h.m4: Add an initial implementation of + of the Glib::Variant<> classes which would wrap glib's GVariant API. + 2010-05-30 Daniel Elstner <danielk@openismus.com> Avoid compiler warning in TimeVal::as_double() diff --git a/glib/glibmm/filelist.am b/glib/glibmm/filelist.am index ccc7202d..bd0b1ada 100644 --- a/glib/glibmm/filelist.am +++ b/glib/glibmm/filelist.am @@ -36,6 +36,7 @@ glibmm_files_extra_cc = \ utility.cc \ value.cc \ value_custom.cc \ + variant.cc \ wrap.cc glibmm_files_extra_h = \ @@ -78,6 +79,7 @@ glibmm_files_extra_h = \ utility.h \ value.h \ value_custom.h \ + variant.h \ wrap.h \ wrap_init.h diff --git a/glib/glibmm/variant.cc b/glib/glibmm/variant.cc new file mode 100644 index 00000000..584270f3 --- /dev/null +++ b/glib/glibmm/variant.cc @@ -0,0 +1,122 @@ +/* $Id$ */ + +/* Copyright 2010 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/variant.h> +#include <glibmm/objectbase.h> +#include <glibmm/utility.h> +#include <glibmm/wrap.h> + + +namespace Glib +{ + +/**** Glib::VariantBase ****************************************************/ + +VariantBase::VariantBase() + : gobject_(0) +{ +} + +VariantBase::VariantBase(GVariant *castitem) + : gobject_(castitem) +{ + //TODO: It would be nice to remove a possible floating reference but the C + //API makes it difficult. + if(castitem) + g_variant_ref(castitem); +} + +VariantBase::VariantBase(const VariantBase& other) +{ + // The copy constructor simply copies the underlying GVariant* and increases + // its reference. The reference is decreased upon destruction. + gobject_ = other.gobject_; + + if(gobject_) + g_variant_ref(gobject_); +} + +VariantBase& VariantBase::operator=(const VariantBase& other) +{ + // Check against self-assignment and simply copy the underlying GVariant*, + // increasing its reference. + if( this != &other) + { + gobject_ = other.gobject_; + + if(gobject_) + g_variant_ref(gobject_); + } + return *this; +} + +VariantBase::~VariantBase() +{ + if(gobject_) + g_variant_unref(gobject_); +} + +bool VariantBase::is_container() const +{ + return + static_cast<bool>(g_variant_is_container(const_cast<GVariant*>(gobj()))); +} + +GVariantClass VariantBase::classify() const +{ + return g_variant_classify(const_cast<GVariant*>(gobj())); +} + + +/****************** Specializations ***********************************/ + +// static +const GVariantType* Variant<VariantBase>::variant_type() +{ + return G_VARIANT_TYPE_VARIANT; +} + +Variant<VariantBase> Variant<VariantBase>::create(VariantBase& data) +{ + return Variant<VariantBase>(g_variant_new_variant(data.gobj())); +} + +VariantBase Variant<VariantBase>::get() const +{ + return VariantBase(g_variant_get_variant(gobject_)); +} + +// static +const GVariantType* Variant<Glib::ustring>::variant_type() +{ + return G_VARIANT_TYPE_STRING; +} + +Variant<Glib::ustring> +Variant<Glib::ustring>::create(const Glib::ustring& data) +{ + return Variant<Glib::ustring>(g_variant_new_string(data.c_str())); +} + +Glib::ustring Variant<Glib::ustring>::get() const +{ + return Glib::ustring(g_variant_get_string(gobject_, 0)); +} + +} // namespace Glib diff --git a/glib/glibmm/variant.h b/glib/glibmm/variant.h new file mode 100644 index 00000000..7bc93f97 --- /dev/null +++ b/glib/glibmm/variant.h @@ -0,0 +1,146 @@ +#ifndef _GLIBMM_VARIANT_H +#define _GLIBMM_VARIANT_H + +/* Copyright 2010 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 <glibmmconfig.h> +#include <glibmm/ustring.h> +#include <glib-object.h> + +namespace Glib +{ + +/** @defgroup glibmmVariant Variant Datatype + * + * Glib::Variant<> are specialized classes that deal with strongly typed + * variant data. They are used to wrap glib's GVariant API. For more + * information see the <a + * href="http://library.gnome.org/devel/glib/stable/glib-GVariant.html">glib + * variant + * API</a>. + */ + +/** + * @ingroup glibmmVariant + */ +class VariantBase +{ +public: + /** Default constructor. + * @newin{2,26} + */ + VariantBase(); + + /** Constructs a VariantBase from a GVariant. + * @newin{2,26} + */ + explicit VariantBase(GVariant* castitem); + + /** Copy constructor. Since GVariant is reference counted, the copy + * constructor simply copies the underlying GVariant* and increases its + * reference count. + * @newin{2,26} + */ + VariantBase(const VariantBase& other); + + /** Assignment operator. Since GVariant is reference counted, assignment + * simply copies the underlying GVariant* and increases its reference count. + * @newin{2,26} + */ + VariantBase& operator=(const VariantBase& other); + + /** Get the underlying GVariant. + * @return The underlying GVariant. + * @newin{2,26} + */ + GVariant* gobj() { return gobject_; } + + /** Get the underlying GVariant. + * @return The underlying GVariant. + * @newin{2,26} + */ + const GVariant* gobj() const { return gobject_; } + + /** Tells if the variant contains another variant. + * @return Whether the variant is container. + * @newin{2,26} + */ + bool is_container() const; + + /** Tells the class of the variant. + * @return The class of the variant. + * @newin{2,26} + */ + GVariantClass classify() const; + + virtual ~VariantBase(); + +protected: + GVariant* gobject_; +}; + +/** Template class from which other Glib::Variant<> specializations derive. + * @ingroup glibmmVariant + */ +template <class T> +class Variant : public VariantBase +{ +public: + typedef T CppType; +}; + + +/****************** Specializations ***********************************/ + +/// @ingroup glibmmVariant +template <> +class Variant<VariantBase> : public VariantBase +{ + typedef GVariant* CType; + + Variant<VariantBase>() : VariantBase() { } + Variant<VariantBase>(GVariant* castitem) : VariantBase(castitem) { } + static const GVariantType* variant_type() G_GNUC_CONST; + static Variant<VariantBase> create(Glib::VariantBase& data); + VariantBase get() const; +}; + +/// @ingroup glibmmVariant +template <> +class Variant<Glib::ustring> : public VariantBase +{ + typedef char* CType; + + Variant<Glib::ustring>() : VariantBase() { } + Variant<Glib::ustring>(GVariant* castitem) : VariantBase(castitem) { } + static const GVariantType* variant_type() G_GNUC_CONST; + static Variant<Glib::ustring> create(const Glib::ustring& data); + Glib::ustring get() const; +}; + +} // namespace Glib + + +/* Include generated specializations of Glib::Variant<> for fundamental types: + */ +#define _GLIBMM_VARIANT_H_INCLUDE_VARIANT_BASICTYPES_H +#include <glibmm/variant_basictypes.h> +#undef _GLIBMM_VARIANT_H_INCLUDE_VARIANT_BASICTYPES_H + + +#endif /* _GLIBMM_VARIANT_H */ diff --git a/glib/src/filelist.am b/glib/src/filelist.am index b3910340..791e4f9c 100644 --- a/glib/src/filelist.am +++ b/glib/src/filelist.am @@ -36,5 +36,6 @@ glibmm_files_hg = \ glibmm_files_ccg = $(glibmm_files_hg:.hg=.ccg) -glibmm_files_cc_m4 = value_basictypes.cc.m4 -glibmm_files_h_m4 = signalproxy.h.m4 value_basictypes.h.m4 +glibmm_files_cc_m4 = value_basictypes.cc.m4 variant_basictypes.cc.m4 +glibmm_files_h_m4 = signalproxy.h.m4 value_basictypes.h.m4 \ + variant_basictypes.h.m4 diff --git a/glib/src/variant_basictypes.cc.m4 b/glib/src/variant_basictypes.cc.m4 new file mode 100644 index 00000000..dc345d88 --- /dev/null +++ b/glib/src/variant_basictypes.cc.m4 @@ -0,0 +1,68 @@ +divert(-1) + +dnl $Id$ + +dnl Glib::Variant specializations for fundamental types +dnl +dnl Copyright 2010 The gtkmm Development Team +dnl +dnl This library is free software; you can redistribute it and/or +dnl modify it under the terms of the GNU Lesser General Public +dnl License as published by the Free Software Foundation; either +dnl version 2.1 of the License, or (at your option) any later version. +dnl +dnl This library is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +dnl Lesser General Public License for more details. +dnl +dnl You should have received a copy of the GNU Lesser General Public +dnl License along with this library; if not, write to the Free +dnl Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +include(template.macros.m4) + +dnl +dnl GLIB_VARIANT_BASIC(bool, boolean) +dnl +define([GLIB_VARIANT_BASIC],[dnl +LINE(]__line__[)dnl + +dnl Please ignore the format stuff. I was just tired and played a little. +/**** Glib::Variant<$1> translit(format([%]eval(57-len([$1]))[s],[****/]),[ ],[*]) + +// static +const GVariantType* Variant<$1>::variant_type() +{ + return G_VARIANT_TYPE_[]UPPER($2); +} + +Variant<$1> Variant<$1>::create($1 data) +{ + return Variant<$1>(g_variant_new_$2(data)); +} + +$1 Variant<$1>::get() const +{ + return g_variant_get_$2(gobject_); +} +]) + +divert[]dnl +// This is a generated file, do not edit. Generated from __file__ + +#include <glibmm/variant.h> + +namespace Glib +{ +GLIB_VARIANT_BASIC(bool, boolean) +dnl GLIB_VARIANT_BASIC(unsigned char, byte) +GLIB_VARIANT_BASIC(gint16, int16) +GLIB_VARIANT_BASIC(guint16, uint16) +GLIB_VARIANT_BASIC(gint32, int32) +GLIB_VARIANT_BASIC(guint32, uint32) +GLIB_VARIANT_BASIC(gint64, int64) +GLIB_VARIANT_BASIC(guint64, uint64) +dnl GLIB_VARIANT_BASIC(gint32, handle) +GLIB_VARIANT_BASIC(double, double) +} // namespace Glib diff --git a/glib/src/variant_basictypes.h.m4 b/glib/src/variant_basictypes.h.m4 new file mode 100644 index 00000000..caaf26c9 --- /dev/null +++ b/glib/src/variant_basictypes.h.m4 @@ -0,0 +1,69 @@ +divert(-1) + +dnl $Id$ + +dnl Glib::Variant specializations for fundamental types +dnl +dnl Copyright 2010 The gtkmm Development Team +dnl +dnl This library is free software; you can redistribute it and/or +dnl modify it under the terms of the GNU Lesser General Public +dnl License as published by the Free Software Foundation; either +dnl version 2.1 of the License, or (at your option) any later version. +dnl +dnl This library is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +dnl Lesser General Public License for more details. +dnl +dnl You should have received a copy of the GNU Lesser General Public +dnl License along with this library; if not, write to the Free +dnl Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +include(template.macros.m4) + +dnl +dnl GLIB_VARIANT_BASIC(bool, boolean) +dnl +define([GLIB_VARIANT_BASIC],[dnl +LINE(]__line__[)dnl + +/** + * @ingroup glibmmVariant + */ +template <> +class Variant<$1> : public VariantBase +{ +public: + typedef g$2 CType; + + Variant<$1>() : VariantBase() { } + Variant<$1>(GVariant* castitem) : VariantBase(castitem) { } + static const GVariantType* variant_type() G_GNUC_CONST; + static Variant<$1> create($1 data); + $1 get() const; +}; +]) + +divert[]dnl +// This is a generated file, do not edit. Generated from __file__ + +#ifndef DOXYGEN_SHOULD_SKIP_THIS +#ifndef _GLIBMM_VARIANT_H_INCLUDE_VARIANT_BASICTYPES_H +#error "glibmm/variant_basictypes.h cannot be included directly" +#endif +#endif + +namespace Glib +{ +GLIB_VARIANT_BASIC(bool, boolean) +dnl GLIB_VARIANT_BASIC(unsigned char, byte) +GLIB_VARIANT_BASIC(gint16, int16) +GLIB_VARIANT_BASIC(guint16, uint16) +GLIB_VARIANT_BASIC(gint32, int32) +GLIB_VARIANT_BASIC(guint32, uint32) +GLIB_VARIANT_BASIC(gint64, int64) +GLIB_VARIANT_BASIC(guint64, uint64) +dnl GLIB_VARIANT_BASIC(gint32, handle) +GLIB_VARIANT_BASIC(double, double) +} // namespace Glib |