Back to index

unity  6.0.0
Variant.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Tim Penhey <tim.penhey@canonical.com>
00018  */
00019 
00020 #include "Variant.h"
00021 
00022 namespace unity
00023 {
00024 namespace glib
00025 {
00026 
00027 Variant::Variant()
00028   : variant_(NULL)
00029 {}
00030 
00031 Variant::Variant(GVariant* variant)
00032   : variant_(variant)
00033 {
00034   g_variant_ref_sink(variant_);
00035 }
00036 
00037 Variant::Variant(GVariant* variant, StealRef const& ref)
00038   : variant_(variant)
00039 {}
00040 
00041 Variant::Variant(Variant const& other)
00042   : variant_(other.variant_)
00043 {
00044   if (variant_) g_variant_ref_sink(variant_);
00045 }
00046 
00047 Variant::~Variant()
00048 {
00049   if (variant_) g_variant_unref(variant_);
00050 }
00051 
00052 std::string Variant::GetString() const
00053 {
00054   // g_variant_get_string doesn't duplicate the string
00055   const gchar *result = g_variant_get_string (variant_, NULL);
00056   return result != NULL ? result : "";
00057 }
00058 
00059 int Variant::GetInt() const
00060 {
00061   return static_cast<int>(g_variant_get_int32 (variant_));
00062 }
00063 
00064 unsigned Variant::GetUInt() const
00065 {
00066   return static_cast<unsigned>(g_variant_get_uint32 (variant_));
00067 }
00068 
00069 bool Variant::GetBool() const
00070 {
00071   return (g_variant_get_boolean (variant_) != FALSE);
00072 }
00073 
00074 bool Variant::ASVToHints(HintsMap& hints) const
00075 {
00076   GVariantIter* hints_iter;
00077   char* key = NULL;
00078   GVariant* value = NULL;
00079 
00080   if (!g_variant_is_of_type (variant_, G_VARIANT_TYPE ("(a{sv})")) &&
00081       !g_variant_is_of_type (variant_, G_VARIANT_TYPE ("a{sv}")))
00082   {
00083     return false;
00084   }
00085 
00086   g_variant_get(variant_, g_variant_get_type_string(variant_), &hints_iter);
00087 
00088   while (g_variant_iter_loop(hints_iter, "{sv}", &key, &value))
00089   {
00090     hints[key] = value;
00091   }
00092 
00093   g_variant_iter_free (hints_iter);
00094 
00095   return true;
00096 }
00097 
00098 Variant& Variant::operator=(GVariant* val)
00099 {
00100   if (variant_) g_variant_unref (variant_);
00101   variant_ = g_variant_ref_sink (val);
00102 
00103   return *this;
00104 }
00105 
00106 Variant::operator GVariant* () const
00107 {
00108   return variant_;
00109 }
00110 
00111 } // namespace glib
00112 
00113 namespace variant
00114 {
00115 
00116 BuilderWrapper::BuilderWrapper(GVariantBuilder* builder)
00117   : builder_(builder)
00118 {}
00119 
00120 BuilderWrapper& BuilderWrapper::add(char const* name, bool value)
00121 {
00122   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_boolean(value));
00123   return *this;
00124 }
00125 
00126 BuilderWrapper& BuilderWrapper::add(char const* name, char const* value)
00127 {
00128   if (value)
00129     g_variant_builder_add(builder_, "{sv}", name, g_variant_new_string(value));
00130   else
00131     g_variant_builder_add(builder_, "{sv}", name, g_variant_new_string(""));
00132 
00133   return *this;
00134 }
00135 
00136 BuilderWrapper& BuilderWrapper::add(char const* name, std::string const& value)
00137 {
00138   g_variant_builder_add(builder_, "{sv}", name,
00139                         g_variant_new_string(value.c_str()));
00140   return *this;
00141 }
00142 
00143 BuilderWrapper& BuilderWrapper::add(char const* name, int value)
00144 {
00145   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_int32(value));
00146   return *this;
00147 }
00148 
00149 BuilderWrapper& BuilderWrapper::add(char const* name, long int value)
00150 {
00151   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_int64(value));
00152   return *this;
00153 }
00154 
00155 BuilderWrapper& BuilderWrapper::add(char const* name, long long int value)
00156 {
00157   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_int64(value));
00158   return *this;
00159 }
00160 
00161 BuilderWrapper& BuilderWrapper::add(char const* name, unsigned int value)
00162 {
00163   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_uint32(value));
00164   return *this;
00165 }
00166 
00167 BuilderWrapper& BuilderWrapper::add(char const* name, long unsigned int value)
00168 {
00169   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_uint64(value));
00170   return *this;
00171 }
00172 
00173 BuilderWrapper& BuilderWrapper::add(char const* name, long long unsigned int value)
00174 {
00175   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_uint64(value));
00176   return *this;
00177 }
00178 
00179 BuilderWrapper& BuilderWrapper::add(char const* name, float value)
00180 {
00181   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_double(value));
00182   return *this;
00183 }
00184 
00185 BuilderWrapper& BuilderWrapper::add(char const* name, double value)
00186 {
00187   g_variant_builder_add(builder_, "{sv}", name, g_variant_new_double(value));
00188   return *this;
00189 }
00190 
00191 BuilderWrapper& BuilderWrapper::add(char const* name, GVariant* value)
00192 {
00193   g_variant_builder_add(builder_, "{sv}", name, value);
00194   return *this;
00195 }
00196 
00197 BuilderWrapper& BuilderWrapper::add(nux::Rect const& value)
00198 {
00199   add("x", value.x);
00200   add("y", value.y);
00201   add("width", value.width);
00202   add("height", value.height);
00203   return *this;
00204 }
00205 
00206 
00207 }
00208 }