diff --git a/moo/moocpp/gobjptr.h b/moo/moocpp/gobjptr.h index b5a6bea1..1df18f72 100644 --- a/moo/moocpp/gobjptr.h +++ b/moo/moocpp/gobjptr.h @@ -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 diff --git a/moo/moocpp/gstr.h b/moo/moocpp/gstr.h index 556d075a..402541be 100644 --- a/moo/moocpp/gstr.h +++ b/moo/moocpp/gstr.h @@ -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) diff --git a/moo/moocpp/moocpp.cmake b/moo/moocpp/moocpp.cmake index efbaa04a..6f6092c8 100644 --- a/moo/moocpp/moocpp.cmake +++ b/moo/moocpp/moocpp.cmake @@ -2,4 +2,6 @@ SET(moocpp_sources moocpp/moocpp.cmake moocpp/gobjptr.h moocpp/gstr.h + moocpp/moocpp.h + moocpp/util.h ) diff --git a/moo/moocpp/moocpp.h b/moo/moocpp/moocpp.h new file mode 100644 index 00000000..1d43567e --- /dev/null +++ b/moo/moocpp/moocpp.h @@ -0,0 +1,20 @@ +/* + * moocpp.h + * + * Copyright (C) 2004-2016 by Yevgen Muntyan + * + * 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 . + */ + +#pragma once + +#include "moocpp/gobjptr.h" +#include "moocpp/gstr.h" +#include "moocpp/util.h" diff --git a/moo/moocpp/util.h b/moo/moocpp/util.h new file mode 100644 index 00000000..e768ec6c --- /dev/null +++ b/moo/moocpp/util.h @@ -0,0 +1,30 @@ +/* + * util.h + * + * Copyright (C) 2004-2016 by Yevgen Muntyan + * + * 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 . + */ + +#pragma once + +#include + +template +bool contains(const Container& container, const Element& element) +{ + return std::find(container.begin(), container.end(), element) != container.end(); +} + +template +void remove(Container& container, const Element& element) +{ + container.erase(std::remove(container.begin(), container.end(), element), container.end()); +}