Back to index

unity  6.0.0
GLibWrapper.h
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 *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00019 */
00020 
00021 #ifndef UNITY_GLIB_WRAPPER_H
00022 #define UNITY_GLIB_WRAPPER_H
00023 
00024 #include <iosfwd>
00025 #include <string>
00026 
00027 #include <boost/utility.hpp>
00028 #include <glib.h>
00029 
00030 namespace unity
00031 {
00032 namespace glib
00033 {
00034 
00035 struct AddRef{};
00036 
00037 template <typename T>
00038 class Object
00039 {
00040 public:
00041   Object();
00042   explicit Object(T* val);
00043   Object(T* val, AddRef const& ref);
00044 
00045   Object(Object const&);
00046   ~Object();
00047 
00048   void swap(Object<T>& other);
00049 
00050   Object& operator=(T* val);
00051   Object& operator=(Object other);
00052 
00053   operator T* () const;
00054   operator bool() const;
00055   T* operator->() const;
00056   T* RawPtr() const;
00057   // Release ownership of the object. No unref will occur.
00058   T* Release();
00059 
00060 private:
00061   T* object_;
00062 };
00063 
00064 template <typename T>
00065 bool operator==(Object<T> const& lhs, Object<T> const& rhs)
00066 {
00067   return (lhs.RawPtr() == rhs.RawPtr());
00068 }
00069 
00070 template <typename T>
00071 bool operator!=(Object<T> const& lhs, Object<T> const& rhs)
00072 {
00073   return !(lhs == rhs);
00074 }
00075 
00076 template <typename T>
00077 bool operator!=(T* lhs, Object<T> const& rhs)
00078 {
00079   return !(lhs == rhs.RawPtr());
00080 }
00081 
00082 template <typename G, typename T>
00083 Object<G> object_cast(Object<T> const& obj)
00084 {
00085   return Object<G>(reinterpret_cast<G*>(obj.RawPtr()), AddRef());
00086 }
00087 
00088 class Error : boost::noncopyable
00089 {
00090 public:
00091   Error();
00092   ~Error();
00093 
00094   GError** AsOutParam();
00095   GError** operator&();
00096 
00097   operator GError*();
00098 
00099   operator bool() const;
00100   std::string Message() const;
00101 
00102 private:
00103   GError* error_;
00104 };
00105 
00106 class String : boost::noncopyable
00107 {
00108 public:
00109   String();
00110   explicit String(gchar* str);
00111   ~String();
00112 
00113   gchar** AsOutParam();
00114   gchar** operator&();
00115 
00116   operator bool() const;
00117   operator char*();
00118   operator std::string();
00119   gchar* Value();
00120   std::string Str() const;
00121 
00122 private:
00123   gchar* string_;
00124 };
00125 
00126 std::ostream& operator<<(std::ostream& o, Error const& e);
00127 std::ostream& operator<<(std::ostream& o, String const& s);
00128 
00129 }
00130 }
00131 
00132 namespace std
00133 {
00134   template <typename T> 
00135   void swap (unity::glib::Object<T>& lhs, unity::glib::Object<T>& rhs)
00136   {
00137     lhs.swap(rhs);
00138   }
00139 }
00140 
00141 #include "GLibWrapper-inl.h"
00142 
00143 #endif