aboutsummaryrefslogtreecommitdiff
path: root/paludis/util
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-06-09 21:14:17 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-06-09 21:14:17 +0100
commit5dbb7636dd880314d5b03379ebfd960b469ee6e9 (patch)
tree6a9a062efbf6aa03dcbdc92b6abf156108904ea2 /paludis/util
parentc9137efc3ef08b5c52f775dd853c1c675e80cc64 (diff)
downloadpaludis-5dbb7636dd880314d5b03379ebfd960b469ee6e9.tar.gz
paludis-5dbb7636dd880314d5b03379ebfd960b469ee6e9.tar.xz
Replace Validated with WrappedValue
Slightly cleaner code, and will allow us to pass in additional validation options.
Diffstat (limited to 'paludis/util')
-rw-r--r--paludis/util/files.m42
-rw-r--r--paludis/util/hashes.hh12
-rw-r--r--paludis/util/stringify.hh10
-rw-r--r--paludis/util/validated.hh150
-rw-r--r--paludis/util/validated_TEST.cc126
-rw-r--r--paludis/util/wrapped_value-fwd.hh (renamed from paludis/util/validated-fwd.hh)32
-rw-r--r--paludis/util/wrapped_value-impl.hh104
-rw-r--r--paludis/util/wrapped_value.hh66
-rw-r--r--paludis/util/wrapped_value_TEST.cc107
9 files changed, 305 insertions, 304 deletions
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 6f58b2fdf..6ca5dc537 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -93,8 +93,8 @@ add(`tokeniser', `hh', `cc', `test')
add(`tribool', `hh', `cc', `fwd', `test')
add(`type_list', `hh', `cc', `fwd')
add(`util', `hh')
-add(`validated', `hh', `fwd', `test')
add(`wildcard_expander', `hh', `cc', `test', `testscript')
add(`wrapped_forward_iterator', `hh', `fwd', `impl', `test')
add(`wrapped_output_iterator', `hh', `fwd', `impl')
+add(`wrapped_value', `hh', `fwd', `impl', `test')
diff --git a/paludis/util/hashes.hh b/paludis/util/hashes.hh
index 22910225e..0d382618d 100644
--- a/paludis/util/hashes.hh
+++ b/paludis/util/hashes.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2008 Ciaran McCreesh
+ * Copyright (c) 2008, 2010 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
@@ -21,7 +21,7 @@
#define PALUDIS_GUARD_PALUDIS_UTIL_HASHES_HH 1
#include <paludis/util/attributes.hh>
-#include <paludis/util/validated-fwd.hh>
+#include <paludis/util/wrapped_value-fwd.hh>
#include <paludis/util/fs_entry-fwd.hh>
#include <cstddef>
#include <utility>
@@ -83,12 +83,12 @@ namespace paludis
}
};
- template <typename D_, typename V_, bool b_, typename C_>
- struct Hash<Validated<D_, V_, b_, C_> >
+ template <typename Tag_>
+ struct Hash<WrappedValue<Tag_> >
{
- std::size_t operator() (const Validated<D_, V_, b_, C_> & v) const
+ std::size_t operator() (const WrappedValue<Tag_> & v) const
{
- return Hash<D_>()(v.data());
+ return Hash<typename WrappedValueTraits<Tag_>::UnderlyingType>()(v.value());
}
};
diff --git a/paludis/util/stringify.hh b/paludis/util/stringify.hh
index ece8faf96..a3048ca83 100644
--- a/paludis/util/stringify.hh
+++ b/paludis/util/stringify.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007, 2008, 2009 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010 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
@@ -21,7 +21,7 @@
#define PALUDIS_GUARD_PALUDIS_STRINGIFY_HH 1
#include <paludis/util/attributes.hh>
-#include <paludis/util/validated-fwd.hh>
+#include <paludis/util/wrapped_value-fwd.hh>
#include <tr1/memory>
#include <sstream>
#include <string>
@@ -147,11 +147,11 @@ namespace paludis
return std::string(item);
}
- template <typename D_, typename V_, bool c_, typename C_>
+ template <typename Tag_>
inline std::string
- real_stringify(const Validated<D_, V_, c_, C_> & v)
+ real_stringify(const WrappedValue<Tag_> & v)
{
- return stringify(v.data());
+ return stringify(v.value());
}
}
diff --git a/paludis/util/validated.hh b/paludis/util/validated.hh
deleted file mode 100644
index f71051112..000000000
--- a/paludis/util/validated.hh
+++ /dev/null
@@ -1,150 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2005, 2006, 2007, 2008 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_VALIDATED_HH
-#define PALUDIS_GUARD_PALUDIS_VALIDATED_HH 1
-
-#include <iosfwd>
-#include <functional>
-#include <paludis/util/validated-fwd.hh>
-#include <paludis/util/operators.hh>
-#include <paludis/util/select.hh>
-
-/** \file
- * Validated declarations.
- *
- * \ingroup g_data_structures
- *
- * \section Examples
- *
- * - \ref example_name.cc "example_name.cc" shows basic usage of various defined
- * Validated classes.
- */
-
-namespace paludis
-{
- /**
- * Default comparator for Validated, used to avoid having to include
- * huge standard library headers in a -fwd.
- *
- * \ingroup g_data_structures
- */
- template <typename T_>
- struct PALUDIS_VISIBLE DefaultValidatedComparator :
- std::less<T_>
- {
- };
-
- /**
- * A Validated wraps a particular class instance, ensuring that it always
- * meets certain validation criteria.
- *
- * \ingroup g_data_structures
- */
- template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
- class Validated :
- public Select<full_comparison_,
- relational_operators::HasRelationalOperators,
- equality_operators::HasEqualityOperators>::Type
- {
- private:
- ValidatedDataType_ _value;
-
- public:
- ///\name Basic operations
- ///\{
-
- /**
- * Copy constructor (no validation needed).
- */
- Validated(const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & other);
-
- /**
- * Constructor (validation needed).
- */
- explicit Validated(const ValidatedDataType_ & value);
-
- /**
- * Assignment (no validation needed).
- */
- const Validated<ValidatedDataType_, Validator_, full_comparison_> & operator=
- (const Validated<ValidatedDataType_, Validator_, full_comparison_> & other)
- {
- _value = other._value;
- return *this;
- }
-
- ///\}
-
- /**
- * Fetch to our ValidatedDataType_. This should not be a cast
- * operator to avoid problems with ambiguous comparison operators.
- */
- const ValidatedDataType_ & data() const
- {
- return _value;
- }
- };
-
- template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
- Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_>::Validated(
- const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & other) :
- _value(other._value)
- {
- }
-
- template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
- Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_>::Validated(
- const ValidatedDataType_ & value) :
- _value(value)
- {
- Validator_::validate(_value);
- }
-
- template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
- bool operator== (
- const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & a,
- const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & b)
- {
- return a.data() == b.data();
- }
-
- template <typename ValidatedDataType_, typename Validator_, typename Comparator_>
- bool operator< (
- const Validated<ValidatedDataType_, Validator_, true, Comparator_> & a,
- const Validated<ValidatedDataType_, Validator_, true, Comparator_> & b)
- {
- return Comparator_()(a.data(), b.data());
- }
-
- /**
- * Writing a Validated instance to a stream is done by its data.
- *
- * \ingroup g_data_structures
- */
- template <typename D_, typename V_, bool c_, typename C_>
- std::ostream &
- operator<< (std::ostream & s, const Validated<D_, V_, c_, C_> & v)
- {
- s << v.data();
- return s;
- }
-}
-
-#endif
diff --git a/paludis/util/validated_TEST.cc b/paludis/util/validated_TEST.cc
deleted file mode 100644
index 3da0eba41..000000000
--- a/paludis/util/validated_TEST.cc
+++ /dev/null
@@ -1,126 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2005, 2006 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/validated.hh>
-#include <test/test_framework.hh>
-#include <test/test_runner.hh>
-
-using namespace test;
-using namespace paludis;
-
-/** \file
- * Test cases for validated.hh .
- *
- * \ingroup grpvalidated
- */
-
-#ifndef DOXYGEN
-struct PositiveEvenValidator
-{
- struct NotValid
- {
- };
-
- static void validate(int value)
- {
- if ((value < 0) || (value % 2))
- throw NotValid();
- }
-};
-
-typedef Validated<int, PositiveEvenValidator> PositiveEven;
-#endif
-
-namespace test_cases
-{
- /**
- * \test Validated<PositiveEven> tests.
- *
- * \ingroup grpvalidated
- */
- struct ValidatedPositiveEvenTests : TestCase
- {
- ValidatedPositiveEvenTests() : TestCase("Validated<PositiveEven> tests") { }
-
- void run()
- {
- PositiveEven v(2);
- TEST_CHECK_EQUAL(v, PositiveEven(2));
- v = PositiveEven(4);
- TEST_CHECK_EQUAL(v, PositiveEven(4));
- TEST_CHECK_THROWS(((v = PositiveEven(5))), PositiveEvenValidator::NotValid);
- TEST_CHECK_EQUAL(v, PositiveEven(4));
- TEST_CHECK_THROWS(PositiveEven w(5), PositiveEvenValidator::NotValid);
- }
- } test_validated_positive_even;
-
- /**
- * \test Validated<PositiveEven> comparison tests.
- *
- * \ingroup grpvalidated
- */
- struct ValidatedPositiveEvenComparisonTests : TestCase
- {
- ValidatedPositiveEvenComparisonTests() :
- TestCase("Validated<PositiveEven> comparison tests") { }
-
- void run()
- {
- PositiveEven v2(2);
- PositiveEven v4(4);
- PositiveEven v4b(4);
-
- TEST_CHECK(v2 < v4);
- TEST_CHECK(v2 <= v4);
- TEST_CHECK(! (v2 == v4));
- TEST_CHECK(v2 != v4);
- TEST_CHECK(! (v2 >= v4));
- TEST_CHECK(! (v2 > v4));
-
- TEST_CHECK(! (v4 < v2));
- TEST_CHECK(! (v4 <= v2));
- TEST_CHECK(! (v4 == v2));
- TEST_CHECK(v4 != v2);
- TEST_CHECK(v4 >= v2);
- TEST_CHECK(v4 > v2);
-
- TEST_CHECK(! (v2 < v2));
- TEST_CHECK(v2 <= v2);
- TEST_CHECK(v2 == v2);
- TEST_CHECK(! (v2 != v2));
- TEST_CHECK(v2 >= v2);
- TEST_CHECK(! (v2 > v2));
-
- TEST_CHECK(! (v4 < v4));
- TEST_CHECK(v4 <= v4);
- TEST_CHECK(v4 == v4);
- TEST_CHECK(! (v4 != v4));
- TEST_CHECK(v4 >= v4);
- TEST_CHECK(! (v4 > v4));
-
- TEST_CHECK(! (v4 < v4b));
- TEST_CHECK(v4 <= v4b);
- TEST_CHECK(v4 == v4b);
- TEST_CHECK(! (v4 != v4b));
- TEST_CHECK(v4 >= v4b);
- TEST_CHECK(! (v4 > v4b));
- }
- } test_validated_positive_even_comparisons;
-}
-
diff --git a/paludis/util/validated-fwd.hh b/paludis/util/wrapped_value-fwd.hh
index bd71a5342..49a1d8390 100644
--- a/paludis/util/validated-fwd.hh
+++ b/paludis/util/wrapped_value-fwd.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh
+ * Copyright (c) 2010 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
@@ -17,28 +17,28 @@
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#ifndef PALUDIS_GUARD_PALUDIS_UTIL_VALIDATED_FWD_HH
-#define PALUDIS_GUARD_PALUDIS_UTIL_VALIDATED_FWD_HH 1
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_WRAPPED_VALUE_FWD_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_WRAPPED_VALUE_FWD_HH 1
+#include <paludis/util/attributes.hh>
#include <iosfwd>
-/** \file
- * Forward declarations for paludis/util/validated.hh .
- *
- * \ingroup g_data_structures
- */
-
namespace paludis
{
- template <typename T_>
- struct DefaultValidatedComparator;
+ template <typename Tag_>
+ struct WrappedValue;
+
+ template <typename Tag_>
+ struct WrappedValueTraits;
+
+ template <typename Type_>
+ struct WrappedValueDevoid;
- template <typename D_, typename, bool = true, typename = DefaultValidatedComparator<D_> >
- class Validated;
+ template <typename Tag_, typename Extra_>
+ struct WrappedValueValidate;
- template <typename D_, typename V_, bool c_, typename C_>
- std::ostream &
- operator<< (std::ostream & s, const Validated<D_, V_, c_, C_> & v);
+ template <typename Tag_>
+ std::ostream & operator<< (std::ostream &, const WrappedValue<Tag_> &) PALUDIS_VISIBLE;
}
#endif
diff --git a/paludis/util/wrapped_value-impl.hh b/paludis/util/wrapped_value-impl.hh
new file mode 100644
index 000000000..f8ef9be09
--- /dev/null
+++ b/paludis/util/wrapped_value-impl.hh
@@ -0,0 +1,104 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 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_WRAPPED_VALUE_IMPL_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_WRAPPED_VALUE_IMPL_HH 1
+
+#include <paludis/util/wrapped_value.hh>
+#include <ostream>
+
+namespace paludis
+{
+ template <typename Tag_, typename Extra_>
+ struct WrappedValueValidate
+ {
+ typedef WrappedValueTraits<Tag_> Type;
+ };
+
+ template <typename Tag_>
+ struct WrappedValueValidate<Tag_, void>
+ {
+ typedef WrappedValueValidate<Tag_, void> Type;
+
+ static bool validate(const typename WrappedValueTraits<Tag_>::UnderlyingType & t, const NoType<0u> *)
+ {
+ return WrappedValueTraits<Tag_>::validate(t);
+ }
+ };
+
+ template <typename Tag_>
+ WrappedValue<Tag_>::WrappedValue(
+ const typename WrappedValueTraits<Tag_>::UnderlyingType & v,
+ const typename WrappedValueDevoid<typename WrappedValueTraits<Tag_>::ValidationParamsType>::Type & p)
+ {
+ if (WrappedValueValidate<Tag_, typename WrappedValueTraits<Tag_>::ValidationParamsType>::Type::validate(v, p))
+ _value.reset(new typename WrappedValueTraits<Tag_>::UnderlyingType(v));
+ else
+ throw typename WrappedValueTraits<Tag_>::ExceptionType(v);
+ }
+
+ template <typename Tag_>
+ WrappedValue<Tag_> &
+ WrappedValue<Tag_>::WrappedValue::operator= (const WrappedValue & v)
+ {
+ _value = v._value;
+ return *this;
+ }
+
+ template <typename Tag_>
+ bool
+ WrappedValue<Tag_>::WrappedValue::operator< (const WrappedValue & other) const
+ {
+ return value() < other.value();
+ }
+
+ template <typename Tag_>
+ bool
+ WrappedValue<Tag_>::WrappedValue::operator== (const WrappedValue & other) const
+ {
+ return value() == other.value();
+ }
+
+ template <typename Tag_>
+ WrappedValue<Tag_>::WrappedValue(const WrappedValue & other) :
+ _value(other._value)
+ {
+ }
+
+ template <typename Tag_>
+ WrappedValue<Tag_>::~WrappedValue()
+ {
+ }
+
+ template <typename Tag_>
+ const typename WrappedValueTraits<Tag_>::UnderlyingType &
+ WrappedValue<Tag_>::value() const
+ {
+ return *_value;
+ }
+
+ template <typename Tag_>
+ std::ostream & operator<< (std::ostream & s, const WrappedValue<Tag_> & v)
+ {
+ s << v.value();
+ return s;
+ }
+}
+
+#endif
diff --git a/paludis/util/wrapped_value.hh b/paludis/util/wrapped_value.hh
new file mode 100644
index 000000000..d77f3e090
--- /dev/null
+++ b/paludis/util/wrapped_value.hh
@@ -0,0 +1,66 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 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_WRAPPED_VALUE_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_WRAPPED_VALUE_HH 1
+
+#include <paludis/util/wrapped_value-fwd.hh>
+#include <paludis/util/no_type.hh>
+#include <paludis/util/operators.hh>
+#include <tr1/memory>
+
+namespace paludis
+{
+ template <typename T_>
+ struct WrappedValueDevoid
+ {
+ typedef T_ Type;
+ };
+
+ template <>
+ struct WrappedValueDevoid<void>
+ {
+ typedef NoType<0u> * Type;
+ };
+
+ template <typename Tag_>
+ class PALUDIS_VISIBLE WrappedValue :
+ public relational_operators::HasRelationalOperators
+ {
+ private:
+ std::tr1::shared_ptr<const typename WrappedValueTraits<Tag_>::UnderlyingType> _value;
+
+ public:
+ explicit WrappedValue(
+ const typename WrappedValueTraits<Tag_>::UnderlyingType &,
+ const typename WrappedValueDevoid<typename WrappedValueTraits<Tag_>::ValidationParamsType>::Type & = static_cast<NoType<0u> *>(0)
+ );
+
+ WrappedValue & operator= (const WrappedValue &);
+ WrappedValue(const WrappedValue &);
+ ~WrappedValue();
+
+ const typename WrappedValueTraits<Tag_>::UnderlyingType & value() const PALUDIS_ATTRIBUTE((warn_unused_result));
+
+ bool operator< (const WrappedValue &) const PALUDIS_ATTRIBUTE((warn_unused_result));
+ bool operator== (const WrappedValue &) const PALUDIS_ATTRIBUTE((warn_unused_result));
+ };
+}
+
+#endif
diff --git a/paludis/util/wrapped_value_TEST.cc b/paludis/util/wrapped_value_TEST.cc
new file mode 100644
index 000000000..ab8a6a963
--- /dev/null
+++ b/paludis/util/wrapped_value_TEST.cc
@@ -0,0 +1,107 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2010 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/wrapped_value-impl.hh>
+#include <test/test_framework.hh>
+#include <test/test_runner.hh>
+#include <string>
+
+using namespace test;
+using namespace paludis;
+
+namespace
+{
+ typedef WrappedValue<struct DormouseTag> Dormouse;
+
+ struct PALUDIS_VISIBLE NotADormouseError
+ {
+ NotADormouseError(const std::string &)
+ {
+ }
+ };
+
+ typedef WrappedValue<struct CheeseTag> Cheese;
+
+ struct PALUDIS_VISIBLE NoCheeseError
+ {
+ NoCheeseError(const std::string &)
+ {
+ }
+ };
+}
+
+namespace paludis
+{
+ template <>
+ struct WrappedValueTraits<DormouseTag>
+ {
+ typedef std::string UnderlyingType;
+ typedef void ValidationParamsType;
+ typedef NotADormouseError ExceptionType;
+
+ static bool validate(const std::string & s)
+ {
+ return s == "glis glis" || s == "muscardinus avellanarius";
+ }
+ };
+
+ template <>
+ struct WrappedValueTraits<CheeseTag>
+ {
+ typedef std::string UnderlyingType;
+ typedef bool ValidationParamsType;
+ typedef NoCheeseError ExceptionType;
+
+ static bool validate(const std::string & s, const bool tasty)
+ {
+ return s == "stilton" || (s == "camembert" && ! tasty);
+ }
+ };
+};
+
+namespace test_cases
+{
+ struct DormouseTest : TestCase
+ {
+ DormouseTest() : TestCase("dormouse tests") { }
+
+ void run()
+ {
+ Dormouse dormouse("glis glis");
+ TEST_CHECK_EQUAL(dormouse.value(), "glis glis");
+ dormouse = Dormouse("muscardinus avellanarius");
+ TEST_CHECK_EQUAL(dormouse.value(), "muscardinus avellanarius");
+ TEST_CHECK_THROWS(dormouse = Dormouse("mesocricetus auratus"), NotADormouseError);
+ }
+ } test_dormouse;
+
+ struct CheeseTest : TestCase
+ {
+ CheeseTest() : TestCase("cheese tests") { }
+
+ void run()
+ {
+ Cheese cheese("stilton", false);
+ TEST_CHECK_THROWS(cheese = Cheese("camembert", true), NoCheeseError);
+ cheese = Cheese("camembert", false);
+ TEST_CHECK_EQUAL(cheese.value(), "camembert");
+ }
+ } test_cheese;
+}
+