A bit more C++

This commit is contained in:
Yevgen Muntyan 2016-10-17 00:47:37 -07:00
parent 914b7773cd
commit c05d4244f0
5 changed files with 72 additions and 2 deletions

View File

@ -32,8 +32,7 @@ public:
~ObjectPtr()
{
if (m_p != nullptr)
g_object_unref(m_p);
reset();
}
ObjectPtr(FooObject* obj, ObjectMemPolicy policy)
@ -65,6 +64,13 @@ public:
return *m_p;
}
void reset()
{
if (m_p != nullptr)
g_object_unref(m_p);
m_p = nullptr;
}
FooObject* get() const
{
return m_p;
@ -80,6 +86,12 @@ public:
return ObjectPtr(obj, ObjectMemPolicy::CopyReference);
}
ObjectPtr& operator=(nullptr_t)
{
reset();
return *this;
}
ObjectPtr& operator=(const ObjectPtr& p)
{
if (this != &p)
@ -102,6 +114,7 @@ public:
p.m_p = nullptr;
if (tmp != nullptr)
g_object_unref(tmp);
return *this;
}
operator bool() const

View File

@ -85,6 +85,11 @@ public:
return result;
}
gstr dup() const
{
return take(m_p ? g_strdup(m_p) : nullptr);
}
gstr& operator=(const gstr& other)
{
if (this != &other)

View File

@ -2,4 +2,6 @@ SET(moocpp_sources
moocpp/moocpp.cmake
moocpp/gobjptr.h
moocpp/gstr.h
moocpp/moocpp.h
moocpp/util.h
)

20
moo/moocpp/moocpp.h Normal file
View File

@ -0,0 +1,20 @@
/*
* moocpp.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/gobjptr.h"
#include "moocpp/gstr.h"
#include "moocpp/util.h"

30
moo/moocpp/util.h Normal file
View File

@ -0,0 +1,30 @@
/*
* util.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 <algorithm>
template<typename Container, typename Element>
bool contains(const Container& container, const Element& element)
{
return std::find(container.begin(), container.end(), element) != container.end();
}
template<typename Container, typename Element>
void remove(Container& container, const Element& element)
{
container.erase(std::remove(container.begin(), container.end(), element), container.end());
}