[niepce] Use std::unique_ptr<> for workers queue.
- From: Hubert Figuière <hub src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [niepce] Use std::unique_ptr<> for workers queue.
- Date: Wed, 19 Jun 2013 01:58:54 +0000 (UTC)
commit f19d3b86cea8e967dfec56b7b060092b2de4298c
Author: Hubert Figuière <hub figuiere net>
Date: Mon Jun 17 20:07:16 2013 -0400
Use std::unique_ptr<> for workers queue.
src/engine/library/op.hpp | 36 +++++++++++++++---------------
src/engine/library/test_opqueue.cpp | 9 +++++--
src/engine/library/thumbnailcache.cpp | 2 +-
src/engine/library/thumbnailcache.hpp | 10 ++++----
src/fwk/utils/mtqueue.hpp | 17 ++++++++++----
src/fwk/utils/worker.hpp | 25 +++++++++++---------
src/libraryclient/locallibraryserver.hpp | 12 +++++-----
7 files changed, 62 insertions(+), 49 deletions(-)
---
diff --git a/src/engine/library/op.hpp b/src/engine/library/op.hpp
index 841fec0..6767422 100644
--- a/src/engine/library/op.hpp
+++ b/src/engine/library/op.hpp
@@ -1,7 +1,7 @@
/*
* niepce - engine/library/op.h
*
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
*
* 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
@@ -29,26 +29,26 @@
namespace eng {
- /** a library operation */
- class Op
- {
- public:
- typedef std::shared_ptr< Op > Ptr;
- typedef std::function<void (const Library::Ptr &)> function_t;
+/** a library operation */
+class Op
+{
+public:
+ typedef std::unique_ptr< Op > Ptr;
+ typedef std::function<void (const Library::Ptr &)> function_t;
- Op(tid_t id, const function_t & func);
+ Op(tid_t id, const function_t & func);
- tid_t id() const
- { return m_id; }
+ tid_t id() const
+ { return m_id; }
- void operator() (const Library::Ptr &);
- const function_t & fn() const
- { return m_function; }
- protected:
- private:
- tid_t m_id;
- function_t m_function;
- };
+ void operator() (const Library::Ptr &);
+ const function_t & fn() const
+ { return m_function; }
+protected:
+private:
+ tid_t m_id;
+ function_t m_function;
+};
}
diff --git a/src/engine/library/test_opqueue.cpp b/src/engine/library/test_opqueue.cpp
index 6a09168..8a30a65 100644
--- a/src/engine/library/test_opqueue.cpp
+++ b/src/engine/library/test_opqueue.cpp
@@ -41,13 +41,16 @@ int test_main(int, char *[])
BOOST_CHECK(q.empty());
+ tid_t old_id = p->id();
+ Op* old_ptr = p.get();
q.add(p);
BOOST_CHECK(!q.empty());
+ BOOST_CHECK(p == nullptr);
Op::Ptr p2(q.pop());
- BOOST_CHECK(p2 == p);
- BOOST_CHECK(p2->id() == p->id());
- BOOST_CHECK(q.empty());
+ BOOST_CHECK(p2.get() == old_ptr);
+ BOOST_CHECK(p2->id() == old_id);
+ BOOST_CHECK(q.empty());
return 0;
}
diff --git a/src/engine/library/thumbnailcache.cpp b/src/engine/library/thumbnailcache.cpp
index 665ae41..67776d7 100644
--- a/src/engine/library/thumbnailcache.cpp
+++ b/src/engine/library/thumbnailcache.cpp
@@ -137,7 +137,7 @@ Glib::RefPtr<Gdk::Pixbuf> getThumbnail(const LibFile::Ptr & f, int w, int h, con
}
-void ThumbnailCache::execute(const ThumbnailTask::Ptr & task)
+void ThumbnailCache::execute(const ptr_t & task)
{
int w, h;
w = task->width();
diff --git a/src/engine/library/thumbnailcache.hpp b/src/engine/library/thumbnailcache.hpp
index 6663112..5ac6ac0 100644
--- a/src/engine/library/thumbnailcache.hpp
+++ b/src/engine/library/thumbnailcache.hpp
@@ -33,12 +33,12 @@ namespace eng {
class ThumbnailTask
{
public:
- typedef std::shared_ptr< ThumbnailTask > Ptr;
-
+ typedef std::unique_ptr<ThumbnailTask> Ptr;
+
ThumbnailTask(const LibFile::Ptr & f, int w, int h)
: m_file(f), m_width(w), m_height(h)
{ }
-
+
const LibFile::Ptr & file()
{ return m_file; }
int width() const
@@ -53,7 +53,7 @@ private:
class ThumbnailCache
- : private fwk::Worker< ThumbnailTask::Ptr >
+ : private fwk::Worker<ThumbnailTask>
{
public:
ThumbnailCache(const std::string & dir,
@@ -66,7 +66,7 @@ public:
static bool is_thumbnail_cached(const std::string & file, const std::string & thumb);
protected:
- virtual void execute(const ThumbnailTask::Ptr & task);
+ virtual void execute(const ptr_t & task);
private:
std::string m_cacheDir;
std::weak_ptr<fwk::NotificationCenter> m_notif_center;
diff --git a/src/fwk/utils/mtqueue.hpp b/src/fwk/utils/mtqueue.hpp
index 1f73bc2..2a38116 100644
--- a/src/fwk/utils/mtqueue.hpp
+++ b/src/fwk/utils/mtqueue.hpp
@@ -1,7 +1,7 @@
/*
* niepce - fwk/utils/mtqueue.h
*
- * Copyright (C) 2007-2008 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
*
* 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
@@ -43,6 +43,7 @@ namespace fwk {
virtual ~MtQueue();
void add(const T &);
+ void add(T & ); // for unique_ptr<>
T pop();
bool empty() const;
void clear();
@@ -74,18 +75,24 @@ namespace fwk {
m_queue.push_back(op);
}
-
+ // for unique_ptr<>
+ template < class T > void
+ MtQueue<T>::add(T &op)
+ {
+ mutex_t::Lock lock(m_mutex);
+ m_queue.push_back(std::move(op));
+ }
+
template < class T >
T MtQueue<T>::pop()
{
T elem;
- mutex_t::Lock lock(m_mutex);
- elem = m_queue.front();
+ mutex_t::Lock lock(m_mutex);
+ elem = std::move(m_queue.front());
m_queue.pop_front();
return elem;
}
-
template < class T >
bool MtQueue<T>::empty() const
{
diff --git a/src/fwk/utils/worker.hpp b/src/fwk/utils/worker.hpp
index 3cb9828..dd0196a 100644
--- a/src/fwk/utils/worker.hpp
+++ b/src/fwk/utils/worker.hpp
@@ -1,7 +1,7 @@
/*
* niepce - fwk/utils/worker.h
*
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
*
* 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
@@ -21,7 +21,7 @@
#ifndef __FWK_UTILS_WORKER_H__
#define __FWK_UTILS_WORKER_H__
-
+#include <memory>
#include <string>
#include "fwk/utils/thread.hpp"
@@ -37,20 +37,23 @@ class Worker
public:
Worker();
virtual ~Worker();
- typedef MtQueue<T> queue_t;
-
+ typedef std::unique_ptr<T> ptr_t;
+ typedef MtQueue<ptr_t> queue_t;
+
#ifdef BOOST_AUTO_TEST_MAIN
- queue_t & _tasks()
+ queue_t & _tasks()
{ return m_tasks; }
#endif
- void schedule(const T & );
+ /** Schedule a task. Takes ownership of it.
+ */
+ void schedule(ptr_t & ); // must be a ref so that the pointer is moved
void clear();
protected:
virtual void main();
-
+
queue_t m_tasks;
private:
- virtual void execute(const T & _op) = 0;
+ virtual void execute(const ptr_t & _op) = 0;
Glib::Threads::Mutex m_q_mutex;
Glib::Threads::Cond m_wait_cond;
};
@@ -79,9 +82,9 @@ template <class T>
void Worker<T>::main()
{
m_terminated = false;
-
+
do {
- T op;
+ ptr_t op;
{
// make sure we terminate the thread before we unlock
// the task queue.
@@ -103,7 +106,7 @@ void Worker<T>::main()
}
template <class T>
-void Worker<T>::schedule(const T & _op)
+void Worker<T>::schedule(ptr_t & _op)
{
Glib::Threads::Mutex::Lock lock(m_q_mutex);
m_tasks.add(_op);
diff --git a/src/libraryclient/locallibraryserver.hpp b/src/libraryclient/locallibraryserver.hpp
index 08c4627..79bccc0 100644
--- a/src/libraryclient/locallibraryserver.hpp
+++ b/src/libraryclient/locallibraryserver.hpp
@@ -1,7 +1,7 @@
/*
* niepce - libraryclient/locallibraryserver.h
*
- * Copyright (C) 2007 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
*
* 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
@@ -15,7 +15,7 @@
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
@@ -30,13 +30,13 @@
namespace libraryclient {
class LocalLibraryServer
- : public fwk::Worker< eng::Op::Ptr >
+ : public fwk::Worker<eng::Op>
{
public:
/** create the local server for the library whose dir is specified */
- LocalLibraryServer(const std::string & dir,
+ LocalLibraryServer(const std::string & dir,
const fwk::NotificationCenter::Ptr & nc)
- : fwk::Worker< eng::Op::Ptr >()
+ : fwk::Worker<eng::Op>()
, m_library(eng::Library::Ptr(new eng::Library(dir, nc)))
{
}
@@ -45,7 +45,7 @@ namespace libraryclient {
return m_library && m_library->ok();
}
protected:
- virtual void execute(const eng::Op::Ptr & _op);
+ virtual void execute(const ptr_t & _op);
private:
eng::Library::Ptr m_library;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]