aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-11-02 13:54:19 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-11-02 13:54:19 +0000
commit9c303b9026c2aa0b0520a08e3f430a8793653db8 (patch)
treee5123e4f468a44e61baefa721726f91986579749
parentea7d4ab21e1fcad22161a1443869b6c74a0dae65 (diff)
downloadpaludis-9c303b9026c2aa0b0520a08e3f430a8793653db8.tar.gz
paludis-9c303b9026c2aa0b0520a08e3f430a8793653db8.tar.xz
If we have concepts, use them on function template types
-rw-r--r--paludis/util/clone-impl.hh4
-rw-r--r--paludis/util/clone.hh5
-rw-r--r--paludis/util/create_iterator-fwd.hh4
-rw-r--r--paludis/util/create_iterator.hh4
-rw-r--r--paludis/util/create_iterator_TEST.cc69
-rw-r--r--paludis/util/destringify.hh36
-rw-r--r--paludis/util/fast_unique_copy.hh34
-rw-r--r--paludis/util/files.m42
-rw-r--r--paludis/util/iterator_funcs.hh13
-rw-r--r--paludis/util/join.hh41
-rw-r--r--paludis/util/log.hh3
-rw-r--r--paludis/util/random.hh9
-rw-r--r--paludis/util/stringify.hh26
13 files changed, 239 insertions, 11 deletions
diff --git a/paludis/util/clone-impl.hh b/paludis/util/clone-impl.hh
index 784b49b..4e98947 100644
--- a/paludis/util/clone-impl.hh
+++ b/paludis/util/clone-impl.hh
@@ -20,6 +20,8 @@
#ifndef PALUDIS_GUARD_PALUDIS_UTIL_CLONE_IMPL_HH
#define PALUDIS_GUARD_PALUDIS_UTIL_CLONE_IMPL_HH 1
+#include <paludis/util/clone.hh>
+
/** \file
* Implementation for paludis/clone.hh .
*
@@ -28,7 +30,7 @@
namespace paludis
{
- template<typename T_>
+ template <typename T_>
Cloneable<T_>::~Cloneable()
{
}
diff --git a/paludis/util/clone.hh b/paludis/util/clone.hh
index ea9b272..bb7dd3f 100644
--- a/paludis/util/clone.hh
+++ b/paludis/util/clone.hh
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2007 David Leverton
+ * Copyright (c) 2007 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
@@ -23,6 +24,10 @@
#include <paludis/util/attributes.hh>
#include <paludis/util/tr1_memory.hh>
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
+
/** \file
* Declares the Cloneable class and helpers.
*
diff --git a/paludis/util/create_iterator-fwd.hh b/paludis/util/create_iterator-fwd.hh
index 470621e..3808a11 100644
--- a/paludis/util/create_iterator-fwd.hh
+++ b/paludis/util/create_iterator-fwd.hh
@@ -20,6 +20,10 @@
#ifndef PALUDIS_GUARD_PALUDIS_UTIL_CREATE_ITERATOR_FWD_HH
#define PALUDIS_GUARD_PALUDIS_UTIL_CREATE_ITERATOR_FWD_HH 1
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
+
namespace paludis
{
template <typename Value_, typename Iter_>
diff --git a/paludis/util/create_iterator.hh b/paludis/util/create_iterator.hh
index 0b4636f..6de97af 100644
--- a/paludis/util/create_iterator.hh
+++ b/paludis/util/create_iterator.hh
@@ -23,10 +23,6 @@
#include <iterator>
#include <paludis/util/create_iterator-fwd.hh>
-#ifdef PALUDIS_HAVE_CONCEPTS
-# include <concepts>
-#endif
-
namespace paludis
{
template <typename Value_, typename Iter_>
diff --git a/paludis/util/create_iterator_TEST.cc b/paludis/util/create_iterator_TEST.cc
new file mode 100644
index 0000000..9c2ae36
--- /dev/null
+++ b/paludis/util/create_iterator_TEST.cc
@@ -0,0 +1,69 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2007 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/create_iterator-impl.hh>
+#include <paludis/util/join.hh>
+#include <algorithm>
+#include <vector>
+#include <test/test_framework.hh>
+#include <test/test_runner.hh>
+
+using namespace paludis;
+using namespace test;
+
+namespace
+{
+ struct Foo
+ {
+ int value;
+
+ Foo(const int i) :
+ value(i)
+ {
+ }
+ };
+
+ std::ostream & operator<< (std::ostream & s, const Foo & y)
+ {
+ s << "<" << y.value << ">";
+ return s;
+ }
+}
+
+namespace test_cases
+{
+ struct CreateIteratorTest : TestCase
+ {
+ CreateIteratorTest() : TestCase("create iterator") { }
+
+ void run()
+ {
+ std::vector<int> x;
+ x.push_back(1);
+ x.push_back(2);
+ x.push_back(3);
+
+ std::vector<Foo> y;
+ std::copy(x.begin(), x.end(), create_inserter<Foo>(std::back_inserter(y)));
+
+ TEST_CHECK_EQUAL(join(y.begin(), y.end(), " "), "<1> <2> <3>");
+ }
+ } create_iterator_test;
+}
+
diff --git a/paludis/util/destringify.hh b/paludis/util/destringify.hh
index aabe2fa..66114d0 100644
--- a/paludis/util/destringify.hh
+++ b/paludis/util/destringify.hh
@@ -157,12 +157,44 @@ namespace paludis
};
}
+#ifdef PALUDIS_HAVE_CONCEPTS
+ /**
+ * A type is destringifiable if it can be default and copy constructed and read from an ostream.
+ *
+ * \ingroup g_strings
+ * \since 0.26
+ */
+ auto concept IsDestringifiable<typename T_>
+ {
+ requires std::DefaultConstructible<T_>;
+ requires std::CopyConstructible<T_>;
+ std::ostream & operator<< (std::ostream &, const T_ &);
+ };
+
+ /**
+ * A type is suitable for destringify exceptions if it can be thrown with a single string parameter.
+ *
+ * \ingroup g_strings
+ * \since 0.26
+ */
+ auto concept IsDestringifyException<typename T_>
+ {
+ requires std::CopyConstructible<T_>;
+ T_::T_(const std::string &);
+ };
+#endif
+
/**
* Extract a value of some type from a string.
*
* \ingroup g_strings
*/
template <typename Type_, typename Exception_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ IsDestringifiable<Type_>,
+ IsDestringifyException<Exception_>
+#endif
Type_ destringify(const std::string & s)
{
if (s == "")
@@ -172,6 +204,10 @@ namespace paludis
}
template <typename Type_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ IsDestringifiable<Type_>
+#endif
Type_ destringify(const std::string & s)
{
return destringify<Type_, DestringifyError>(s);
diff --git a/paludis/util/fast_unique_copy.hh b/paludis/util/fast_unique_copy.hh
index 796c2e0..12909dc 100644
--- a/paludis/util/fast_unique_copy.hh
+++ b/paludis/util/fast_unique_copy.hh
@@ -34,9 +34,15 @@ namespace paludis
* For use by fast_unique_copy only.
*/
template <typename I_, typename O_, typename C_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::RandomAccessIterator<I_>,
+ std::OutputIterator<O_, I_::value_type>,
+ std::BinaryPredicate<C_, I_::reference, I_::reference>
+#endif
void
real_fast_unique_copy(const I_ & start, const I_ & end, const I_ & full_end, O_ out,
- const C_ & comp, const I_ & mbgt)
+ C_ comp, const I_ & mbgt)
{
if (start != end)
{
@@ -48,7 +54,10 @@ namespace paludis
// if our first item is equal to our last item, we have exactly
// one unique item in this sequence
if ((! comp(*start, *previous(end))) && (! comp(*previous(end), *start)))
- *out++ = *start;
+ {
+ *out = *start;
+ ++out;
+ }
else
{
I_ mid = start + (std::distance(start, end) >> 1);
@@ -63,19 +72,36 @@ namespace paludis
* Extract unique elements from a sorted range of random access iterators.
*/
template <typename I_, typename O_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::RandomAccessIterator<I_>,
+ std::OutputIterator<O_, I_::value_type>,
+ std::BinaryPredicate<std::less<I_::value_type>, I_::reference, I_::reference>
+#endif
void
fast_unique_copy(const I_ & start, const I_ & end, O_ out)
{
fast_unique_copy_internals::real_fast_unique_copy(start, end, end, out,
- std::less<typename std::iterator_traits<I_>::value_type>(), end);
+#ifdef PALUDIS_HAVE_CONCEPTS
+ std::less<I_::value_type>(),
+#else
+ std::less<typename std::iterator_traits<I_>::value_type>(),
+#endif
+ end);
}
/**
* Extract unique elements from a sorted range of random access iterators.
*/
template <typename I_, typename O_, typename C_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::RandomAccessIterator<I_>,
+ std::OutputIterator<O_, I_::value_type>,
+ std::BinaryPredicate<C_, I_::reference>
+#endif
void
- fast_unique_copy(const I_ & start, const I_ & end, O_ out, const C_ & comp)
+ fast_unique_copy(const I_ & start, const I_ & end, O_ out, C_ comp)
{
fast_unique_copy_internals::real_fast_unique_copy(start, end, end, out, comp, end);
}
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index e4926c2..c769a30 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -13,7 +13,7 @@ add(`attributes', `hh')
add(`config_file', `hh', `cc', `se', `test', `testscript')
add(`clone', `hh', `impl')
add(`condition_variable', `hh', `cc', `test')
-add(`create_iterator', `hh', `fwd', `impl')
+add(`create_iterator', `hh', `fwd', `impl', `test')
add(`damerau_levenshtein', `hh', `cc', `test')
add(`destringify', `hh', `cc', `test')
add(`dir_iterator', `hh', `cc', `test', `testscript')
diff --git a/paludis/util/iterator_funcs.hh b/paludis/util/iterator_funcs.hh
index 700e52c..ad89900 100644
--- a/paludis/util/iterator_funcs.hh
+++ b/paludis/util/iterator_funcs.hh
@@ -23,6 +23,10 @@
#include <iterator>
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
+
namespace paludis
{
/**
@@ -31,6 +35,9 @@ namespace paludis
* \ingroup g_iterator
*/
template <typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires std::ForwardIterator<T_>
+#endif
T_ next(const T_ & i)
{
T_ result(i);
@@ -43,6 +50,9 @@ namespace paludis
* \ingroup g_iterator
*/
template <typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires std::ForwardIterator<T_>
+#endif
T_ next(const T_ & i, const int & n)
{
T_ result(i);
@@ -56,6 +66,9 @@ namespace paludis
* \ingroup g_iterator
*/
template <typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires std::BidirectionalIterator<T_>
+#endif
T_ previous(const T_ & i)
{
T_ result(i);
diff --git a/paludis/util/join.hh b/paludis/util/join.hh
index 51def6f..bdfec51 100644
--- a/paludis/util/join.hh
+++ b/paludis/util/join.hh
@@ -24,6 +24,10 @@
#include <iterator>
#include <string>
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
+
/** \file
* Declarations for the join function.
*
@@ -36,12 +40,33 @@
namespace paludis
{
+#ifdef PALUDIS_HAVE_CONCEPTS
+ auto concept IsJoinResult<typename T_>
+ {
+ requires std::DefaultConstructible<T_>;
+ requires std::CopyConstructible<T_>;
+ void T_::operator+= (const T_ &);
+ void T_::operator+= (const std::string &);
+ };
+
+ auto concept IsJoinStringifier<typename F_, typename T_, typename I_>
+ {
+ T_ operator() (const F_ &, const I_ &);
+ };
+#endif
+
/**
* Join together the items from i to end using joiner.
*
* \ingroup g_strings
*/
template <typename I_, typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::ForwardIterator<I_>,
+ IsStringifiable<I_::reference>,
+ IsJoinResult<T_>
+#endif
T_ join(I_ i, I_ end, const T_ & joiner)
{
T_ result;
@@ -63,6 +88,12 @@ namespace paludis
* \ingroup g_strings
*/
template <typename I_, typename T_, typename F_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::ForwardIterator<I_>,
+ IsJoinResult<T_>,
+ IsJoinStringifier<F_, T_, I_::reference>
+#endif
T_ join(I_ i, I_ end, const T_ & joiner, const F_ & f)
{
T_ result;
@@ -84,6 +115,11 @@ namespace paludis
* \ingroup g_strings
*/
template <typename I_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::ForwardIterator<I_>,
+ IsStringifiable<I_::reference>
+#endif
std::string join(I_ begin, const I_ end, const char * const t)
{
return join(begin, end, std::string(t));
@@ -96,6 +132,11 @@ namespace paludis
* \ingroup g_strings
*/
template <typename I_, typename F_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::ForwardIterator<I_>,
+ IsJoinStringifier<F_, std::string, I_::reference>
+#endif
std::string join(I_ begin, const I_ end, const char * const t, const F_ & f)
{
return join(begin, end, std::string(t), f);
diff --git a/paludis/util/log.hh b/paludis/util/log.hh
index 77fd9f8..01612c7 100644
--- a/paludis/util/log.hh
+++ b/paludis/util/log.hh
@@ -163,6 +163,9 @@ namespace paludis
* Append some text to our message.
*/
template <typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires IsStringifiable<T_>
+#endif
LogMessageHandler &
operator<< (const T_ & t)
{
diff --git a/paludis/util/random.hh b/paludis/util/random.hh
index 3b9a039..bb5221e 100644
--- a/paludis/util/random.hh
+++ b/paludis/util/random.hh
@@ -24,6 +24,10 @@
#include <inttypes.h>
#include <paludis/util/attributes.hh>
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
+
/** \file
* Declarations for the Random class.
*
@@ -70,6 +74,11 @@ namespace paludis
/// Fetch a random number in (0, max]
template <typename DiffType_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires
+ std::Multiplicable<double, DiffType_>,
+ std::CopyConstructible<DiffType_>
+#endif
DiffType_ operator() (DiffType_ max)
{
local_seed = (_a * local_seed + _b) % _m;
diff --git a/paludis/util/stringify.hh b/paludis/util/stringify.hh
index ad00826..2421c33 100644
--- a/paludis/util/stringify.hh
+++ b/paludis/util/stringify.hh
@@ -21,9 +21,13 @@
#define PALUDIS_GUARD_PALUDIS_STRINGIFY_HH 1
#include <paludis/util/attributes.hh>
+#include <paludis/util/tr1_memory.hh>
#include <sstream>
#include <string>
-#include <paludis/util/tr1_memory.hh>
+
+#ifdef PALUDIS_HAVE_CONCEPTS
+# include <concepts>
+#endif
/** \file
* Stringify functions.
@@ -37,6 +41,20 @@
namespace paludis
{
+#ifdef PALUDIS_HAVE_CONCEPTS
+ auto concept IsStreamStringifiable<typename T_>
+ {
+ requires ! std::Dereferenceable<T_>;
+ std::ostream & operator<< (std::ostream &, const T_ &);
+ };
+
+ auto concept IsStringifiable<typename T_>
+ {
+ std::string stringify(const T_ &);
+ };
+#endif
+
+#ifndef PALUDIS_HAVE_CONCEPTS
/**
* For use by stringify.
*
@@ -91,6 +109,7 @@ namespace paludis
enum { value = 0 } Value;
};
}
+#endif
/**
* Convert item to a string.
@@ -98,12 +117,17 @@ namespace paludis
* \ingroup g_strings
*/
template <typename T_>
+#ifdef PALUDIS_HAVE_CONCEPTS
+ requires IsStreamStringifiable<T_>
+#endif
std::string
stringify(const T_ & item)
{
+#ifndef PALUDIS_HAVE_CONCEPTS
/* check that we're not trying to stringify a pointer or somesuch */
int check_for_stringifying_silly_things
PALUDIS_ATTRIBUTE((unused)) = stringify_internals::CheckType<T_>::value;
+#endif
std::ostringstream s;
s << item;