Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=2e23be8f599da26724b108d3f08290876e85e552

commit 2e23be8f599da26724b108d3f08290876e85e552
Author: Michel Hermier <herm...@frugalware.org>
Date:   Thu Oct 16 14:20:52 2014 +0200

libpacman: Extract refcounting aspect to a separated class of flib::object.

diff --git a/lib/libpacman/CMakeLists.txt b/lib/libpacman/CMakeLists.txt
index caac641..c196048 100644
--- a/lib/libpacman/CMakeLists.txt
+++ b/lib/libpacman/CMakeLists.txt
@@ -7,6 +7,8 @@ CONFIGURE_FILE("pacman.pc.in" "pacman.pc" @ONLY)
set(FLIB_SOURCES
kernel/fobject.cpp
kernel/fobject.h
+       kernel/frefcounted.cpp
+       kernel/frefcounted.h
kernel/fsignal.h
kernel/fstr.cpp
kernel/fstr.h
diff --git a/lib/libpacman/kernel/fobject.cpp b/lib/libpacman/kernel/fobject.cpp
index a23ad7f..108afd3 100644
--- a/lib/libpacman/kernel/fobject.cpp
+++ b/lib/libpacman/kernel/fobject.cpp
@@ -1,5 +1,5 @@
/*
- *  object.c
+ *  fobject.c
*
*  Copyright (c) 2014 by Michel Hermier <herm...@frugalware.org>
*
@@ -40,25 +40,10 @@ void *FObject::operator new(std::size_t size)
}

FObject::FObject()
-       : m_reference_counter(1)
{ }

FObject::~FObject()
-{
-}
-
-void FObject::acquire() const
-{
-       ++m_reference_counter;
-}
-
-void FObject::release() const
-{
-       if(--m_reference_counter == 0) {
-               aboutToDestroy(const_cast<FObject *>(this));
-               delete this;
-       }
-}
+{ }

int FObject::get(unsigned val, unsigned long *data) const
{
diff --git a/lib/libpacman/kernel/fobject.h b/lib/libpacman/kernel/fobject.h
index 2cef01b..d2f19e0 100644
--- a/lib/libpacman/kernel/fobject.h
+++ b/lib/libpacman/kernel/fobject.h
@@ -1,5 +1,5 @@
/*
- *  object.h
+ *  fobject.h
*
*  Copyright (c) 2014 by Michel Hermier <herm...@frugalware.org>
*
@@ -21,7 +21,7 @@
#ifndef FOBJECT_H
#define FOBJECT_H

-#include "kernel/fsignal.h"
+#include "kernel/frefcounted.h"

#include <cstddef>

@@ -29,34 +29,20 @@ namespace flib
{

class FObject
+       : public flib::refcounted
{
public:
void operator delete(void *ptr);
void *operator new(std::size_t size);

public:
-       flib::FSignal<void(FObject *)> aboutToDestroy;
-
-public:
FObject();
protected:
virtual ~FObject();

public:
-       void acquire() const;
-       void release() const;
-
virtual int get(unsigned val, unsigned long *data) const;
virtual int set(unsigned val, unsigned long data);
-
-private:
-       void operator delete[](void *ptr);
-       void *operator new[](std::size_t size);
-
-       FObject(const flib::FObject &other);
-       flib::FObject &operator =(const flib::FObject &other);
-
-       mutable unsigned m_reference_counter;
};

static inline void fAcquire(flib::FObject *object)
diff --git a/lib/libpacman/kernel/frefcounted.cpp 
b/lib/libpacman/kernel/frefcounted.cpp
new file mode 100644
index 0000000..6789ef5
--- /dev/null
+++ b/lib/libpacman/kernel/frefcounted.cpp
@@ -0,0 +1,53 @@
+/*
+ *  frefcounted.c
+ *
+ *  Copyright (c) 2014 by Michel Hermier <herm...@frugalware.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+
+#include "config.h"
+
+#include "kernel/frefcounted.h"
+
+#include "util.h"
+
+#include <fstdlib.h>
+
+using namespace flib;
+
+refcounted::refcounted()
+       : m_reference_counter(1)
+{ }
+
+refcounted::~refcounted()
+{
+}
+
+void refcounted::acquire() const
+{
+       ++m_reference_counter;
+}
+
+void refcounted::release() const
+{
+       if(--m_reference_counter == 0) {
+               aboutToDestroy(const_cast<refcounted *>(this));
+               delete this;
+       }
+}
+
+/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libpacman/kernel/frefcounted.h 
b/lib/libpacman/kernel/frefcounted.h
new file mode 100644
index 0000000..9e32621
--- /dev/null
+++ b/lib/libpacman/kernel/frefcounted.h
@@ -0,0 +1,70 @@
+/*
+ *  frefcounted.h
+ *
+ *  Copyright (c) 2014 by Michel Hermier <herm...@frugalware.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+#ifndef FREFCOUNTED_H
+#define FREFCOUNTED_H
+
+#include "kernel/fsignal.h"
+
+namespace flib
+{
+
+       class refcounted
+       {
+       public:
+               flib::FSignal<void(flib::refcounted *)> aboutToDestroy;
+
+       public:
+               refcounted();
+       protected:
+               virtual ~refcounted();
+
+       public:
+               void acquire() const;
+               void release() const;
+
+       private:
+               void operator delete[](void *ptr);
+               void *operator new[](std::size_t size);
+
+               refcounted(const flib::refcounted &other);
+               flib::refcounted &operator =(const flib::refcounted &other);
+
+               mutable unsigned m_reference_counter;
+       };
+
+       static inline void acquire(flib::refcounted *refcounted)
+       {
+               if(refcounted != nullptr) {
+                       refcounted->acquire();
+               }
+       }
+
+       static inline void release(flib::refcounted *refcounted)
+       {
+               if(refcounted != nullptr) {
+                       refcounted->release();
+               }
+       }
+}
+
+#endif /* FREFCOUNTED_H */
+
+/* vim: set ts=2 sw=2 noet: */
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to