aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-07-14 08:12:47 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-07-14 03:32:41 +0100
commitb03992195e908054f1815f23452dfd4f910468cd (patch)
treebb51bf798d2719ef4f3e2a7430b7c2015fe8b6b8
parent76ebda679c4eae54a693ac2e7613842ff9611200 (diff)
downloadpaludis-b03992195e908054f1815f23452dfd4f910468cd.tar.gz
paludis-b03992195e908054f1815f23452dfd4f910468cd.tar.xz
Add notifier callbacks
-rw-r--r--paludis/environment.hh24
-rw-r--r--paludis/environment_implementation.cc44
-rw-r--r--paludis/environment_implementation.hh13
-rw-r--r--paludis/files.m41
-rw-r--r--paludis/notifier_callback-fwd.hh38
-rw-r--r--paludis/notifier_callback.cc67
-rw-r--r--paludis/notifier_callback.hh66
7 files changed, 251 insertions, 2 deletions
diff --git a/paludis/environment.hh b/paludis/environment.hh
index 48c431b..d094031 100644
--- a/paludis/environment.hh
+++ b/paludis/environment.hh
@@ -40,6 +40,7 @@
#include <paludis/metadata_key_holder.hh>
#include <paludis/choice-fwd.hh>
#include <paludis/create_output_manager_info-fwd.hh>
+#include <paludis/notifier_callback-fwd.hh>
/** \file
* Declarations for the Environment class.
@@ -381,6 +382,29 @@ namespace paludis
virtual const std::tr1::shared_ptr<OutputManager> create_output_manager(
const CreateOutputManagerInfo &) const = 0;
+ /**
+ * Set a callback function to use when a particular event occurs.
+ *
+ * The return value can be passed to remove_notifier_callback.
+ *
+ * \since 0.40
+ */
+ virtual NotifierCallbackID add_notifier_callback(const NotifierCallbackFunction &) = 0;
+
+ /**
+ * Remove a function added with add_notifier_callback.
+ *
+ * \since 0.40
+ */
+ virtual void remove_notifier_callback(const NotifierCallbackID) = 0;
+
+ /**
+ * Trigger a notifier callback.
+ *
+ * \since 0.40
+ */
+ virtual void trigger_notifier_callback(const NotifierCallbackEvent &) const = 0;
+
///\}
};
diff --git a/paludis/environment_implementation.cc b/paludis/environment_implementation.cc
index ef932fc..077e537 100644
--- a/paludis/environment_implementation.cc
+++ b/paludis/environment_implementation.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008, 2009 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -32,10 +32,26 @@
#include <paludis/distribution.hh>
#include <paludis/selection.hh>
#include <algorithm>
+#include <map>
#include "config.h"
using namespace paludis;
+namespace paludis
+{
+ template <>
+ struct Implementation<EnvironmentImplementation>
+ {
+ std::map<unsigned, NotifierCallbackFunction> notifier_callbacks;
+ };
+}
+
+EnvironmentImplementation::EnvironmentImplementation() :
+ PrivateImplementationPattern<EnvironmentImplementation>(new Implementation<EnvironmentImplementation>),
+ _imp(PrivateImplementationPattern<EnvironmentImplementation>::_imp)
+{
+}
+
EnvironmentImplementation::~EnvironmentImplementation()
{
}
@@ -165,3 +181,29 @@ EnvironmentImplementation::operator[] (const Selection & selection) const
return selection.perform_select(this);
}
+NotifierCallbackID
+EnvironmentImplementation::add_notifier_callback(const NotifierCallbackFunction & f)
+{
+ unsigned idx(0);
+ if (! _imp->notifier_callbacks.empty())
+ idx = _imp->notifier_callbacks.rbegin()->first + 1;
+
+ _imp->notifier_callbacks.insert(std::make_pair(idx, f));
+ return idx;
+}
+
+void
+EnvironmentImplementation::remove_notifier_callback(const NotifierCallbackID i)
+{
+ _imp->notifier_callbacks.erase(i);
+}
+
+void
+EnvironmentImplementation::trigger_notifier_callback(const NotifierCallbackEvent & e) const
+{
+ for (std::map<unsigned, NotifierCallbackFunction>::const_iterator i(_imp->notifier_callbacks.begin()),
+ i_end(_imp->notifier_callbacks.end()) ;
+ i != i_end ; ++i)
+ (i->second)(e);
+}
+
diff --git a/paludis/environment_implementation.hh b/paludis/environment_implementation.hh
index 2fc37c9..cb880eb 100644
--- a/paludis/environment_implementation.hh
+++ b/paludis/environment_implementation.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008, 2009 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -47,8 +47,12 @@ namespace paludis
* \see Environment
*/
class PALUDIS_VISIBLE EnvironmentImplementation :
+ private PrivateImplementationPattern<EnvironmentImplementation>,
public Environment
{
+ private:
+ PrivateImplementationPattern<EnvironmentImplementation>::ImpPtr & _imp;
+
protected:
virtual const std::tr1::shared_ptr<const SetSpecTree> local_set(const SetName &) const
PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;
@@ -60,6 +64,7 @@ namespace paludis
///\name Basic operations
///\{
+ EnvironmentImplementation();
virtual ~EnvironmentImplementation() = 0;
///\}
@@ -90,6 +95,12 @@ namespace paludis
virtual std::tr1::shared_ptr<PackageIDSequence> operator[] (const Selection &) const
PALUDIS_ATTRIBUTE((warn_unused_result));
+
+ virtual NotifierCallbackID add_notifier_callback(const NotifierCallbackFunction &);
+
+ virtual void remove_notifier_callback(const NotifierCallbackID);
+
+ virtual void trigger_notifier_callback(const NotifierCallbackEvent &) const;
};
}
diff --git a/paludis/files.m4 b/paludis/files.m4
index ff976bd..a306954 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -57,6 +57,7 @@ add(`name', `hh', `cc', `fwd', `test')
add(`ndbam', `hh', `cc', `fwd')
add(`ndbam_merger', `hh', `cc')
add(`ndbam_unmerger', `hh', `cc')
+add(`notifier_callback', `hh', `cc', `fwd')
add(`output_manager', `hh', `fwd', `cc', `se')
add(`output_manager_factory', `hh', `fwd', `cc')
add(`output_manager_from_environment', `hh', `fwd', `cc')
diff --git a/paludis/notifier_callback-fwd.hh b/paludis/notifier_callback-fwd.hh
new file mode 100644
index 0000000..16e7634
--- /dev/null
+++ b/paludis/notifier_callback-fwd.hh
@@ -0,0 +1,38 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008, 2009 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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 PALUDIS_GUARD_PALUDIS_NOTIFIER_CALLBACK_FWD_HH
+#define PALUDIS_GUARD_PALUDIS_NOTIFIER_CALLBACK_FWD_HH 1
+
+#include <tr1/functional>
+
+namespace paludis
+{
+ struct NotifierCallbackEvent;
+ struct NotifierCallbackGeneratingMetadataEvent;
+ struct NotifierCallbackResolverStepEvent;
+
+ typedef std::tr1::function<void (const NotifierCallbackEvent &) > NotifierCallbackFunction;
+
+ typedef unsigned NotifierCallbackID;
+
+ struct ScopedNotifierCallback;
+}
+
+#endif
diff --git a/paludis/notifier_callback.cc b/paludis/notifier_callback.cc
new file mode 100644
index 0000000..7425bbc
--- /dev/null
+++ b/paludis/notifier_callback.cc
@@ -0,0 +1,67 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2009 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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 <paludis/notifier_callback.hh>
+#include <paludis/util/private_implementation_pattern-impl.hh>
+#include <paludis/util/stringify.hh>
+#include <paludis/environment.hh>
+
+using namespace paludis;
+
+namespace paludis
+{
+ template <>
+ struct Implementation<ScopedNotifierCallback>
+ {
+ Environment * const env;
+ const NotifierCallbackID id;
+ bool removed;
+
+ Implementation(Environment * const e, const NotifierCallbackFunction & f) :
+ env(e),
+ id(e->add_notifier_callback(f)),
+ removed(false)
+ {
+ }
+ };
+}
+
+ScopedNotifierCallback::ScopedNotifierCallback(Environment * const e, const NotifierCallbackFunction & f) :
+ PrivateImplementationPattern<ScopedNotifierCallback>(new Implementation<ScopedNotifierCallback>(e, f))
+{
+}
+
+ScopedNotifierCallback::~ScopedNotifierCallback()
+{
+ if (! _imp->removed)
+ remove_now();
+}
+
+void
+ScopedNotifierCallback::remove_now()
+{
+ if (_imp->removed)
+ throw InternalError(PALUDIS_HERE, "already removed");
+
+ _imp->env->remove_notifier_callback(_imp->id);
+ _imp->removed = true;
+}
+
+template class PrivateImplementationPattern<ScopedNotifierCallback>;
+
diff --git a/paludis/notifier_callback.hh b/paludis/notifier_callback.hh
new file mode 100644
index 0000000..5709d84
--- /dev/null
+++ b/paludis/notifier_callback.hh
@@ -0,0 +1,66 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2009 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis 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 PALUDIS_GUARD_PALUDIS_NOTIFIER_CALLBACK_HH
+#define PALUDIS_GUARD_PALUDIS_NOTIFIER_CALLBACK_HH 1
+
+#include <paludis/notifier_callback-fwd.hh>
+#include <paludis/util/private_implementation_pattern.hh>
+#include <paludis/util/simple_visitor.hh>
+#include <paludis/util/type_list.hh>
+#include <paludis/environment-fwd.hh>
+
+namespace paludis
+{
+ class PALUDIS_VISIBLE NotifierCallbackEvent :
+ public virtual DeclareAbstractAcceptMethods<NotifierCallbackEvent, MakeTypeList<
+ NotifierCallbackGeneratingMetadataEvent,
+ NotifierCallbackResolverStepEvent>::Type>
+ {
+ };
+
+ class PALUDIS_VISIBLE NotifierCallbackGeneratingMetadataEvent :
+ public NotifierCallbackEvent,
+ public ImplementAcceptMethods<NotifierCallbackEvent, NotifierCallbackGeneratingMetadataEvent>
+ {
+ };
+
+ class PALUDIS_VISIBLE NotifierCallbackResolverStepEvent :
+ public NotifierCallbackEvent,
+ public ImplementAcceptMethods<NotifierCallbackEvent, NotifierCallbackResolverStepEvent>
+ {
+ };
+
+ class PALUDIS_VISIBLE ScopedNotifierCallback :
+ private PrivateImplementationPattern<ScopedNotifierCallback>
+ {
+ public:
+ ScopedNotifierCallback(Environment * const, const NotifierCallbackFunction &);
+ ~ScopedNotifierCallback();
+
+ void remove_now();
+ };
+
+#ifdef PALUDIS_HAVE_EXTERN_TEMPLATE
+ extern template class PrivateImplementationPattern<ScopedNotifierCallback>;
+#endif
+
+}
+
+#endif