aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-21 00:16:47 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-21 00:16:47 +0000
commit27c1d97a7ba8d54701855fa94088f7e9b099292a (patch)
treed476355644c6ac9946c98d493402c95440088e56
parent7b72a5fd69987f11604717add132e490a94c8ad7 (diff)
downloadpaludis-27c1d97a7ba8d54701855fa94088f7e9b099292a.tar.gz
paludis-27c1d97a7ba8d54701855fa94088f7e9b099292a.tar.xz
Add VirtualConstructor, the coolest toy ever written.
-rw-r--r--paludis/files.m41
-rw-r--r--paludis/virtual_constructor.cc28
-rw-r--r--paludis/virtual_constructor.hh211
-rw-r--r--paludis/virtual_constructor_TEST.cc138
4 files changed, 378 insertions, 0 deletions
diff --git a/paludis/files.m4 b/paludis/files.m4
index c3b8782..c27460b 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -124,5 +124,6 @@ add(`version_metadata', `hh', `cc')
add(`version_operator', `hh', `cc', `test')
add(`version_spec', `hh', `cc', `test')
add(`version_spec_collection', `hh', `cc')
+add(`virtual_constructor', `hh', `cc', `test')
add(`visitor', `hh', `cc', `test')
diff --git a/paludis/virtual_constructor.cc b/paludis/virtual_constructor.cc
new file mode 100644
index 0000000..acbe0c0
--- /dev/null
+++ b/paludis/virtual_constructor.cc
@@ -0,0 +1,28 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * 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 as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * 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 "virtual_constructor.hh"
+
+/** \file
+ * Implementation of paludis::VirtualConstructor.
+ *
+ * \ingroup VirtualConstructor
+ */
+
diff --git a/paludis/virtual_constructor.hh b/paludis/virtual_constructor.hh
new file mode 100644
index 0000000..a3f1d17
--- /dev/null
+++ b/paludis/virtual_constructor.hh
@@ -0,0 +1,211 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * 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 as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * 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_VIRTUAL_CONSTRUCTOR_HH
+#define PALUDIS_GUARD_PALUDIS_VIRTUAL_CONSTRUCTOR_HH 1
+
+#include <vector>
+#include <algorithm>
+#include <paludis/instantiation_policy.hh>
+
+/** \file
+ * Declarations for VirtualConstructor and related classes.
+ *
+ * \ingroup VirtualConstructor
+ */
+
+namespace paludis
+{
+ /**
+ * Behaviour policy classes for what to do if an appropriate constructor
+ * cannot be found for a VirtualConstructor::find_maker call.
+ *
+ * \ingroup VirtualConstructor
+ */
+ namespace virtual_constructor_not_found
+ {
+ /**
+ * Throw an exception of type ExceptionType_, which should have a
+ * constructor that takes a single parameter of KeyType_.
+ *
+ * \ingroup VirtualConstructor
+ */
+ template <typename ExceptionType_>
+ struct ThrowException
+ {
+ /**
+ * Internal use: provide handle_not_found.
+ */
+ template <typename KeyType_, typename ValueType_>
+ struct Parent
+ {
+ /**
+ * Internal use: called when we cannot find a key.
+ */
+ ValueType_ handle_not_found(const KeyType_ & k) const
+ {
+ throw ExceptionType_(k);
+ }
+ };
+ };
+ }
+
+ /**
+ * For internal use by VirtualConstructor.
+ *
+ * \ingroup VirtualConstructor
+ */
+ namespace virtual_constructor_internals
+ {
+ /**
+ * Comparator class for our entries.
+ *
+ * \ingroup VirtualConstructor
+ */
+ template <typename First_, typename Second_>
+ struct ComparePairByFirst
+ {
+ /**
+ * Compare, with the entry on the LHS.
+ */
+ bool operator() (const std::pair<First_, Second_> & a, const First_ & b) const
+ {
+ return a.first < b;
+ }
+
+ /**
+ * Compare, with the entry on the RHS.
+ */
+ bool operator() (const First_ & a, const std::pair<First_, Second_> & b) const
+ {
+ return a < b.first;
+ }
+ };
+ }
+
+ /**
+ * A VirtualConstructor can be used where a mapping between the value of
+ * some key type (often a string) to the construction of some kind of
+ * class (possibly via a functor) is required.
+ *
+ * \ingroup VirtualConstructor
+ */
+ template <typename KeyType_, typename ValueType_, typename NotFoundBehaviour_>
+ class VirtualConstructor :
+ public NotFoundBehaviour_::template Parent<KeyType_, ValueType_>,
+ public InstantiationPolicy<VirtualConstructor<KeyType_, ValueType_, NotFoundBehaviour_>,
+ instantiation_method::SingletonAsNeededTag>
+ {
+ friend class InstantiationPolicy<
+ VirtualConstructor<KeyType_, ValueType_, NotFoundBehaviour_>,
+ instantiation_method::SingletonAsNeededTag>;
+
+ private:
+ VirtualConstructor()
+ {
+ }
+
+ protected:
+ /**
+ * Our entries, sorted.
+ */
+ std::vector<std::pair<KeyType_, ValueType_> > entries;
+
+ public:
+ /**
+ * The type of our key.
+ */
+ typedef KeyType_ KeyType;
+
+ /**
+ * The type of our value.
+ */
+ typedef ValueType_ ValueType;
+
+ /**
+ * The behaviour policy for when a key is not found.
+ */
+ typedef NotFoundBehaviour_ NotFoundBehaviour;
+
+ /**
+ * Find a value for the specified key, or perform the appropriate
+ * NotFoundBehaviour.
+ */
+ ValueType_ find_maker(const KeyType_ & k) const;
+
+ /**
+ * Convenience alias for find_maker.
+ */
+ ValueType_ operator[] (const KeyType_ & k) const
+ {
+ return find_maker(k);
+ }
+
+ /**
+ * Register a new maker (should usually be called by the
+ * RegisterMaker child class.
+ */
+ void register_maker(const KeyType_ & k, const ValueType_ & v);
+
+ /**
+ * An instance of this class registers a new maker with the
+ * specified key.
+ */
+ struct RegisterMaker
+ {
+ /**
+ * Constructor.
+ */
+ RegisterMaker(const KeyType_ & k, const ValueType_ & v)
+ {
+ VirtualConstructor<KeyType_, ValueType_, NotFoundBehaviour_>::get_instance()->
+ register_maker(k, v);
+ }
+ };
+ };
+
+ template <typename KeyType_, typename ValueType_, typename NotFoundBehaviour_>
+ ValueType_
+ VirtualConstructor<KeyType_, ValueType_, NotFoundBehaviour_>::find_maker(
+ const KeyType_ & k) const
+ {
+ std::pair<
+ typename std::vector<std::pair<KeyType_, ValueType_> >::const_iterator,
+ typename std::vector<std::pair<KeyType_, ValueType_> >::const_iterator > m(
+ std::equal_range(entries.begin(), entries.end(), k,
+ virtual_constructor_internals::ComparePairByFirst<KeyType_, ValueType_>()));
+ if (m.first == m.second)
+ return this->handle_not_found(k);
+ else
+ return m.first->second;
+ }
+
+ template <typename KeyType_, typename ValueType_, typename NotFoundBehaviour_>
+ void
+ VirtualConstructor<KeyType_, ValueType_, NotFoundBehaviour_>::register_maker(
+ const KeyType_ & k, const ValueType_ & v)
+ {
+ entries.insert(std::lower_bound(entries.begin(), entries.end(), k,
+ virtual_constructor_internals::ComparePairByFirst<KeyType_, ValueType_>()),
+ std::make_pair(k, v));
+ }
+}
+
+#endif
diff --git a/paludis/virtual_constructor_TEST.cc b/paludis/virtual_constructor_TEST.cc
new file mode 100644
index 0000000..f034581
--- /dev/null
+++ b/paludis/virtual_constructor_TEST.cc
@@ -0,0 +1,138 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+#include "virtual_constructor.hh"
+#include "counted_ptr.hh"
+#include <test/test_runner.hh>
+#include <test/test_framework.hh>
+
+using namespace test;
+using namespace paludis;
+
+#ifndef DOXYGEN
+enum CookieSize
+{
+ cs_small,
+ cs_large
+};
+
+class Cookie :
+ public InternalCounted<Cookie>
+{
+ private:
+ CookieSize _size;
+
+ protected:
+ Cookie(CookieSize size) :
+ _size(size)
+ {
+ };
+
+ public:
+ virtual std::string flavour() const = 0;
+
+ virtual ~Cookie()
+ {
+ }
+
+ CookieSize size() const
+ {
+ return _size;
+ }
+};
+
+struct NoCookie
+{
+ NoCookie(const std::string &)
+ {
+ }
+};
+
+typedef VirtualConstructor<std::string,
+ CountedPtr<Cookie> (*) (CookieSize),
+ virtual_constructor_not_found::ThrowException<NoCookie> > CookieMaker;
+
+class ChocolateChipCookie : public Cookie
+{
+ public:
+ ChocolateChipCookie(CookieSize size) :
+ Cookie(size)
+ {
+ }
+
+ std::string flavour() const
+ {
+ return "Chocolate Chip";
+ }
+
+ static CountedPtr<Cookie> make(CookieSize size)
+ {
+ return CountedPtr<Cookie>(new ChocolateChipCookie(size));
+ }
+};
+
+CookieMaker::RegisterMaker register_chocolate_chip("chocolate chip", &ChocolateChipCookie::make);
+
+class GingerCookie : public Cookie
+{
+ private:
+ bool _with_crunchy_bits;
+
+ public:
+ GingerCookie(CookieSize size, bool with_crunchy_bits) :
+ Cookie(size),
+ _with_crunchy_bits(with_crunchy_bits)
+ {
+ }
+
+ std::string flavour() const
+ {
+ return _with_crunchy_bits ? "Crunchy Ginger" : "Ginger";
+ }
+
+ bool with_crunchy_bits() const
+ {
+ return _with_crunchy_bits;
+ }
+
+ static CountedPtr<Cookie> make(CookieSize size)
+ {
+ return CountedPtr<Cookie>(new GingerCookie(size, false));
+ }
+
+ static CountedPtr<Cookie> make_crunchy(CookieSize size)
+ {
+ return CountedPtr<Cookie>(new GingerCookie(size, true));
+ }
+};
+
+CookieMaker::RegisterMaker register_ginger("ginger", &GingerCookie::make);
+CookieMaker::RegisterMaker register_crunchy_ginger("crunchy ginger", &GingerCookie::make_crunchy);
+
+#endif
+
+namespace test_cases
+{
+ struct VirtualConstructorTest : TestCase
+ {
+ VirtualConstructorTest() : TestCase("virtual constructor") { }
+
+ void run()
+ {
+ TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
+ "chocolate chip")(cs_large)->flavour(), "Chocolate Chip");
+ TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
+ "chocolate chip")(cs_large)->size(), cs_large);
+ TEST_CHECK_EQUAL(CookieMaker::get_instance()->find_maker(
+ "chocolate chip")(cs_small)->size(), cs_small);
+
+ TEST_CHECK_EQUAL((*CookieMaker::get_instance())["ginger"](cs_small)->flavour(),
+ "Ginger");
+ TEST_CHECK_EQUAL((*CookieMaker::get_instance())["crunchy ginger"](cs_small)->flavour(),
+ "Crunchy Ginger");
+
+ TEST_CHECK_THROWS(CookieMaker::get_instance()->find_maker(
+ "gerbil")(cs_large)->flavour(), NoCookie);
+ }
+ } test_virtual_constructor;
+}
+