aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-11 19:00:14 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-11 19:00:14 +0000
commit7d90b508b22284628e64ee4fe3d8e40d58780d89 (patch)
treed03ff00148a368114aa9b0ccf4a895da9d5f982d
parentb76b8615cfa141eb081853f9e22ef6ad5fa49869 (diff)
downloadpaludis-7d90b508b22284628e64ee4fe3d8e40d58780d89.tar.gz
paludis-7d90b508b22284628e64ee4fe3d8e40d58780d89.tar.xz
Add OutputManagerFactory
-rw-r--r--paludis/util/files.m41
-rw-r--r--paludis/util/output_manager_factory-fwd.hh28
-rw-r--r--paludis/util/output_manager_factory.cc131
-rw-r--r--paludis/util/output_manager_factory.hh89
4 files changed, 249 insertions, 0 deletions
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 304d420..abbd5a0 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -47,6 +47,7 @@ add(`no_type', `hh')
add(`operators', `hh')
add(`options', `hh', `fwd', `cc', `test')
add(`output_manager', `hh', `fwd', `cc', `se')
+add(`output_manager_factory', `hh', `fwd', `cc')
add(`output_wrapper', `test', `testscript')
add(`pipe', `hh', `cc')
add(`pretty_print', `hh', `cc', `test')
diff --git a/paludis/util/output_manager_factory-fwd.hh b/paludis/util/output_manager_factory-fwd.hh
new file mode 100644
index 0000000..cc7006c
--- /dev/null
+++ b/paludis/util/output_manager_factory-fwd.hh
@@ -0,0 +1,28 @@
+/* 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_UTIL_OUTPUT_MANAGER_FACTORY_FWD_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_OUTPUT_MANAGER_FACTORY_FWD_HH 1
+
+namespace paludis
+{
+ class OutputManagerFactory;
+}
+
+#endif
diff --git a/paludis/util/output_manager_factory.cc b/paludis/util/output_manager_factory.cc
new file mode 100644
index 0000000..05fcb8e
--- /dev/null
+++ b/paludis/util/output_manager_factory.cc
@@ -0,0 +1,131 @@
+/* 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/util/output_manager_factory.hh>
+#include <paludis/util/private_implementation_pattern-impl.hh>
+#include <paludis/util/instantiation_policy-impl.hh>
+#include <paludis/util/member_iterator-impl.hh>
+#include <paludis/util/wrapped_forward_iterator-impl.hh>
+#include <paludis/util/join.hh>
+#include <paludis/util/named_value.hh>
+#include <paludis/util/make_named_values.hh>
+#include <paludis/util/set.hh>
+#include <paludis/util/fs_entry.hh>
+#include <paludis/util/system.hh>
+#include <paludis/util/dir_iterator.hh>
+#include <paludis/util/is_file_with_extension.hh>
+#include <paludis/about.hh>
+#include <tr1/unordered_map>
+#include <list>
+#include <dlfcn.h>
+#include <stdint.h>
+
+using namespace paludis;
+
+namespace paludis
+{
+ namespace n
+ {
+ struct create_function;
+ }
+}
+
+namespace
+{
+ struct Funcs
+ {
+ NamedValue<n::create_function, OutputManagerFactory::CreateFunction> create_function;
+ };
+
+ typedef std::tr1::unordered_map<std::string, Funcs> Keys;
+
+ const Funcs & fetch(const Keys & keys, const std::string & key)
+ {
+ if (key.empty())
+ throw ConfigurationError("Key 'manager' not specified when creating an output manager");
+
+ Keys::const_iterator i(keys.find(key));
+ if (i == keys.end())
+ throw ConfigurationError("Format '" + key + "' not supported when creating an output manager (known formats are { "
+ + join(first_iterator(keys.begin()), first_iterator(keys.end()), ", ") + "})");
+
+ return i->second;
+ }
+}
+
+namespace paludis
+{
+ template <>
+ struct Implementation<OutputManagerFactory>
+ {
+ Keys keys;
+ std::list<void *> dl_opened;
+ };
+}
+
+OutputManagerFactory::OutputManagerFactory() :
+ PrivateImplementationPattern<OutputManagerFactory>(new Implementation<OutputManagerFactory>)
+{
+}
+
+OutputManagerFactory::~OutputManagerFactory()
+{
+}
+
+const std::tr1::shared_ptr<OutputManager>
+OutputManagerFactory::create(
+ const KeyFunction & key_function
+ ) const
+{
+ Context context("When creating output manager:");
+ return fetch(_imp->keys, key_function("manager")).create_function()(key_function);
+}
+
+OutputManagerFactory::ConstIterator
+OutputManagerFactory::begin_keys() const
+{
+ return first_iterator(_imp->keys.begin());
+}
+
+OutputManagerFactory::ConstIterator
+OutputManagerFactory::end_keys() const
+{
+ return first_iterator(_imp->keys.end());
+}
+
+void
+OutputManagerFactory::add_manager(
+ const std::tr1::shared_ptr<const Set<std::string> > & formats,
+ const CreateFunction & create_function
+ )
+{
+ for (Set<std::string>::ConstIterator f(formats->begin()), f_end(formats->end()) ;
+ f != f_end ; ++f)
+ {
+ if (! _imp->keys.insert(std::make_pair(*f, make_named_values<Funcs>(
+ value_for<n::create_function>(create_function)
+ ))).second)
+ throw ConfigurationError("Handler for output manager format '" + stringify(*f) + "' already exists");
+ }
+}
+
+template class PrivateImplementationPattern<OutputManagerFactory>;
+template class InstantiationPolicy<OutputManagerFactory, instantiation_method::SingletonTag>;
+template class WrappedForwardIterator<OutputManagerFactory::ConstIteratorTag, const std::string>;
+
diff --git a/paludis/util/output_manager_factory.hh b/paludis/util/output_manager_factory.hh
new file mode 100644
index 0000000..972576b
--- /dev/null
+++ b/paludis/util/output_manager_factory.hh
@@ -0,0 +1,89 @@
+/* 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_UTIL_OUTPUT_MANAGER_FACTORY_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_OUTPUT_MANAGER_FACTORY_HH 1
+
+#include <paludis/util/output_manager_factory-fwd.hh>
+#include <paludis/util/attributes.hh>
+#include <paludis/util/private_implementation_pattern.hh>
+#include <paludis/util/instantiation_policy.hh>
+#include <paludis/util/wrapped_forward_iterator-fwd.hh>
+#include <paludis/util/output_manager-fwd.hh>
+#include <paludis/util/set-fwd.hh>
+#include <tr1/functional>
+#include <tr1/memory>
+
+namespace paludis
+{
+ class PALUDIS_VISIBLE OutputManagerFactory :
+ private PrivateImplementationPattern<OutputManagerFactory>,
+ public InstantiationPolicy<OutputManagerFactory, instantiation_method::SingletonTag>
+ {
+ friend class InstantiationPolicy<OutputManagerFactory, instantiation_method::SingletonTag>;
+
+ private:
+ OutputManagerFactory();
+ ~OutputManagerFactory();
+
+ public:
+ typedef std::tr1::function<std::string (const std::string &)> KeyFunction;
+
+ typedef std::tr1::function<const std::tr1::shared_ptr<OutputManager>(
+ const KeyFunction &
+ )> CreateFunction;
+
+ /**
+ * Construct a given OutputManager, or throw ConfigurationError.
+ *
+ * \param key_function should return the value for a given key. The
+ * 'manager' key must return a value (e.g. 'standard'), which is used
+ * to select the return type. Other key names are manager defined,
+ * but typically include things like 'location' and 'keep_on_success'.
+ */
+ const std::tr1::shared_ptr<OutputManager> create(
+ const KeyFunction & key_function
+ ) const PALUDIS_ATTRIBUTE((warn_unused_result));
+
+ /**
+ * Add a manager.
+ *
+ * \param managers must have at least one value, and no value may be
+ * specified more than once across all invocations.
+ *
+ * \param create_function is used to implement OutputManagerFactory::create.
+ */
+ void add_manager(
+ const std::tr1::shared_ptr<const Set<std::string> > & managers,
+ const CreateFunction & create_function
+ );
+
+ struct ConstIteratorTag;
+ typedef WrappedForwardIterator<ConstIteratorTag, const std::string> ConstIterator;
+ ConstIterator begin_keys() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ ConstIterator end_keys() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ };
+
+#ifdef PALUDIS_HAVE_EXTERN_TEMPLATE
+ extern template class PrivateImplementationPattern<OutputManagerFactory>;
+ extern template class InstantiationPolicy<OutputManagerFactory, instantiation_method::SingletonTag>;
+#endif
+}
+
+#endif