validated.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_VALIDATED_HH
00021 #define PALUDIS_GUARD_PALUDIS_VALIDATED_HH 1
00022 
00023 #include <iosfwd>
00024 #include <functional>
00025 #include <paludis/util/validated-fwd.hh>
00026 #include <paludis/util/operators.hh>
00027 #include <paludis/util/select.hh>
00028 
00029 /** \file
00030  * Validated declarations.
00031  *
00032  * \ingroup g_data_structures
00033  *
00034  * \section Examples
00035  *
00036  * - \ref example_name.cc "example_name.cc" shows basic usage of various defined
00037  *   Validated classes.
00038  */
00039 
00040 namespace paludis
00041 {
00042     /**
00043      * Default comparator for Validated, used to avoid having to include
00044      * huge standard library headers in a -fwd.
00045      *
00046      * \ingroup g_data_structures
00047      */
00048     template <typename T_>
00049     struct PALUDIS_VISIBLE DefaultValidatedComparator :
00050         std::less<T_>
00051     {
00052     };
00053 
00054     /**
00055      * A Validated wraps a particular class instance, ensuring that it always
00056      * meets certain validation criteria.
00057      *
00058      * \ingroup g_data_structures
00059      */
00060     template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
00061     class Validated :
00062         public Select<full_comparison_,
00063             relational_operators::HasRelationalOperators,
00064             equality_operators::HasEqualityOperators>::Type
00065     {
00066         private:
00067             ValidatedDataType_ _value;
00068 
00069         public:
00070             ///\name Basic operations
00071             ///\{
00072 
00073             /**
00074              * Copy constructor (no validation needed).
00075              */
00076             Validated(const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & other);
00077 
00078             /**
00079              * Constructor (validation needed).
00080              */
00081             explicit Validated(const ValidatedDataType_ & value);
00082 
00083             /**
00084              * Assignment (no validation needed).
00085              */
00086             const Validated<ValidatedDataType_, Validator_, full_comparison_> & operator=
00087                 (const Validated<ValidatedDataType_, Validator_, full_comparison_> & other)
00088             {
00089                 _value = other._value;
00090                 return *this;
00091             }
00092 
00093             ///\}
00094 
00095             /**
00096              * Fetch to our ValidatedDataType_. This should not be a cast
00097              * operator to avoid problems with ambiguous comparison operators.
00098              */
00099             const ValidatedDataType_ & data() const
00100             {
00101                 return _value;
00102             }
00103     };
00104 
00105     template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
00106     Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_>::Validated(
00107             const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & other) :
00108         _value(other._value)
00109     {
00110     }
00111 
00112     template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
00113     Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_>::Validated(
00114             const ValidatedDataType_ & value) :
00115         _value(value)
00116     {
00117         Validator_::validate(_value);
00118     }
00119 
00120     template <typename ValidatedDataType_, typename Validator_, bool full_comparison_, typename Comparator_>
00121     bool operator== (
00122             const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & a,
00123             const Validated<ValidatedDataType_, Validator_, full_comparison_, Comparator_> & b)
00124     {
00125         return a.data() == b.data();
00126     }
00127 
00128     template <typename ValidatedDataType_, typename Validator_, typename Comparator_>
00129     bool operator< (
00130             const Validated<ValidatedDataType_, Validator_, true, Comparator_> & a,
00131             const Validated<ValidatedDataType_, Validator_, true, Comparator_> & b)
00132     {
00133         return Comparator_()(a.data(), b.data());
00134     }
00135 
00136     /**
00137      * Writing a Validated instance to a stream is done by its data.
00138      *
00139      * \ingroup g_data_structures
00140      */
00141     template <typename D_, typename V_, bool c_, typename C_>
00142     std::ostream &
00143     operator<< (std::ostream & s, const Validated<D_, V_, c_, C_> & v)
00144     {
00145         s << v.data();
00146         return s;
00147     }
00148 }
00149 
00150 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4