Back to index

unity  6.0.0
GLibWrapper.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2011 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 "GLibWrapper.h"
00021 
00022 namespace unity
00023 {
00024 namespace glib
00025 {
00026 
00027 Error::Error()
00028   : error_(0)
00029 {}
00030 
00031 Error::~Error()
00032 {
00033   if (error_)
00034     g_error_free(error_);
00035 }
00036 
00037 GError** Error::AsOutParam()
00038 {
00039   return &error_;
00040 }
00041 
00042 GError** Error::operator&()
00043 {
00044   return &error_;
00045 }
00046 
00047 Error::operator bool() const
00048 {
00049   return bool(error_);
00050 }
00051 
00052 Error::operator GError* ()
00053 {
00054     return error_;
00055 }
00056 
00057 std::string Error::Message() const
00058 {
00059   std::string result;
00060   if (error_)
00061     result = error_->message;
00062   return result;
00063 }
00064 
00065 std::ostream& operator<<(std::ostream& o, Error const& e)
00066 {
00067   if (e)
00068     o << e.Message();
00069   return o;
00070 }
00071 
00072 
00073 String::String()
00074   : string_(0)
00075 {}
00076 
00077 String::String(gchar* str)
00078   : string_(str)
00079 {}
00080 
00081 String::~String()
00082 {
00083   if (string_)
00084     g_free(string_);
00085 }
00086 
00087 gchar** String::AsOutParam()
00088 {
00089   return &string_;
00090 }
00091 
00092 gchar** String::operator&()
00093 {
00094   return &string_;
00095 }
00096 
00097 gchar* String::Value()
00098 {
00099   return string_;
00100 }
00101 
00102 String::operator char*()
00103 {
00104   return string_;
00105 }
00106 
00107 String::operator std::string()
00108 {
00109   return Str();
00110 }
00111 
00112 String::operator bool() const
00113 {
00114   return bool(string_);
00115 }
00116 
00117 std::string String::Str() const
00118 {
00119   if (string_)
00120     return std::string(string_);
00121   else
00122     return std::string("");
00123 }
00124 
00125 std::ostream& operator<<(std::ostream& o, String const& s)
00126 {
00127   if (s)
00128     o << s.Str();
00129   else
00130     o << "<null>";
00131   return o;
00132 }
00133 
00134 }
00135 }
00136