2016-01-04 03:56:42 -08:00
|
|
|
/*
|
|
|
|
* moocpp/gobjptr.h
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2016 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* This file is part of medit. medit 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "moocpp/gobjref.h"
|
|
|
|
|
|
|
|
namespace moo {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// gobj_raw_ptr
|
|
|
|
//
|
|
|
|
|
|
|
|
template<typename Object>
|
|
|
|
class gobj_raw_ptr
|
|
|
|
{
|
2016-01-04 09:56:33 -08:00
|
|
|
using ref_type = gobj_ref<Object>;
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
gobj_raw_ptr(Object* obj = nullptr) { m_ref._set_gobj(obj); }
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
operator Object*() const { return gobj(); }
|
|
|
|
operator GTypeInstance*() const { return reinterpret_cast<GTypeInstance*>(gobj()); }
|
|
|
|
operator gpointer() const { return gobj(); }
|
2016-01-04 09:56:33 -08:00
|
|
|
operator gobj_ref<Object>*() const { return m_ref.self(); }
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
ref_type* operator->() const { return m_ref.self(); }
|
|
|
|
ref_type& operator*() const { return m_ref; }
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
Object* gobj() const { return m_ref.gobj(); }
|
2016-01-04 03:56:42 -08:00
|
|
|
void set(Object* p) { m_ref._set_gobj(p); }
|
|
|
|
|
|
|
|
template<typename Super>
|
2016-01-04 10:24:03 -08:00
|
|
|
Super* gobj() const
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
|
|
|
return gobj_is_subclass<Object, Super>::down_cast(m_ref.gobj());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Subclass>
|
|
|
|
void set(Subclass* p)
|
|
|
|
{
|
|
|
|
set(gobj_is_subclass<Subclass, Object>::down_cast(p));
|
|
|
|
}
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
operator bool() const { return gobj() != nullptr; }
|
|
|
|
bool operator!() const { return gobj() == nullptr; }
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
gobj_raw_ptr(const gobj_raw_ptr& other) = default;
|
|
|
|
gobj_raw_ptr& operator=(const gobj_raw_ptr& other) = default;
|
|
|
|
|
|
|
|
gobj_raw_ptr(gobj_raw_ptr&& other)
|
2016-01-04 11:14:37 -08:00
|
|
|
: m_ref(std::move(other.m_ref))
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
2016-01-05 02:18:52 -08:00
|
|
|
other.set(nullptr);
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
gobj_raw_ptr& operator=(gobj_raw_ptr&& other)
|
|
|
|
{
|
2016-01-04 11:14:37 -08:00
|
|
|
m_ref = std::move(other.m_ref);
|
2016-01-04 03:56:42 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
gobj_raw_ptr& operator=(T* p)
|
|
|
|
{
|
|
|
|
set(p);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-01-04 09:56:33 -08:00
|
|
|
mutable gobj_ref<Object> m_ref;
|
2016-01-04 03:56:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Object>
|
|
|
|
class gobj_raw_ptr<const Object>
|
|
|
|
{
|
2016-01-04 09:56:33 -08:00
|
|
|
using ref_type = gobj_ref<Object>;
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
gobj_raw_ptr(const Object* obj = nullptr) { m_ref._set_gobj(const_cast<Object*>(obj)); }
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
operator const Object*() const { return gobj(); }
|
|
|
|
operator const GTypeInstance*() const { return reinterpret_cast<GTypeInstance*>(gobj()); }
|
|
|
|
operator const void*() const { return gobj(); }
|
2016-01-04 09:56:33 -08:00
|
|
|
operator const gobj_ref<Object>*() const { return m_ref.self(); }
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
const ref_type* operator->() const { return m_ref.self(); }
|
|
|
|
const ref_type& operator*() const { return m_ref; }
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
const Object* gobj() const { return m_ref.gobj(); }
|
2016-01-04 03:56:42 -08:00
|
|
|
void set(const Object* p) { m_ref._set_gobj(p); }
|
|
|
|
|
|
|
|
template<typename Super>
|
2016-01-04 10:24:03 -08:00
|
|
|
const Super* gobj() const
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
|
|
|
return gobj_is_subclass<Object, Super>::down_cast(m_ref.gobj());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Subclass>
|
|
|
|
void set(const Subclass* p)
|
|
|
|
{
|
|
|
|
set(gobj_is_subclass<Subclass, Object>::down_cast(p));
|
|
|
|
}
|
|
|
|
|
2016-01-04 10:24:03 -08:00
|
|
|
operator bool() const { return gobj() != nullptr; }
|
|
|
|
bool operator!() const { return gobj() == nullptr; }
|
2016-01-04 03:56:42 -08:00
|
|
|
|
|
|
|
gobj_raw_ptr(const gobj_raw_ptr& other) = default;
|
|
|
|
gobj_raw_ptr& operator=(const gobj_raw_ptr& other) = default;
|
|
|
|
|
|
|
|
gobj_raw_ptr(gobj_raw_ptr&& other)
|
2016-01-04 10:24:03 -08:00
|
|
|
: m_ref(other.gobj())
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
|
|
|
other = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gobj_raw_ptr& operator=(gobj_raw_ptr&& other)
|
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
m_ref._set_gobj(other.gobj());
|
2016-01-04 03:56:42 -08:00
|
|
|
other.m_ref._set_gobj(nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
gobj_raw_ptr& operator=(const T* p)
|
|
|
|
{
|
|
|
|
set(p);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-01-04 09:56:33 -08:00
|
|
|
mutable gobj_ref<Object> m_ref;
|
2016-01-04 03:56:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace moo
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const moo::gobj_raw_ptr<X>& p, const nullptr_t&)
|
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
return p.gobj() == nullptr;
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const nullptr_t&, const moo::gobj_raw_ptr<X>& p)
|
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
return p.gobj() == nullptr;
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:14:37 -08:00
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const moo::gobj_raw_ptr<X>& p1, const moo::gobj_raw_ptr<X>& p2)
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
return p1.gobj() == p2.gobj();
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:14:37 -08:00
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const moo::gobj_raw_ptr<X>& p1, const X* p2)
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
return p1.gobj() == p2;
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:14:37 -08:00
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const moo::gobj_raw_ptr<X>& p1, X* p2)
|
|
|
|
{
|
|
|
|
return p1.gobj() == p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(const X* p1, const moo::gobj_raw_ptr<X>& p2)
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
2016-01-04 10:24:03 -08:00
|
|
|
return p1 == p2.gobj();
|
2016-01-04 03:56:42 -08:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:14:37 -08:00
|
|
|
template<typename X>
|
|
|
|
inline bool operator==(X* p1, const moo::gobj_raw_ptr<X>& p2)
|
|
|
|
{
|
|
|
|
return p1 == p2.gobj();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
bool operator!=(const moo::gobj_raw_ptr<X>& p1, const moo::gobj_raw_ptr<X>& p2)
|
2016-01-04 03:56:42 -08:00
|
|
|
{
|
|
|
|
return !(p1 == p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X, typename Y>
|
|
|
|
bool operator!=(const moo::gobj_raw_ptr<X>& p1, const Y& p2)
|
|
|
|
{
|
|
|
|
return !(p1 == p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X, typename Y>
|
|
|
|
bool operator!=(const X& p1, const moo::gobj_raw_ptr<Y>& p2)
|
|
|
|
{
|
|
|
|
return !(p1 == p2);
|
|
|
|
}
|