named_value.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2008, 2009 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_UTIL_NAMED_VALUE_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_NAMED_VALUE_HH 1
00022 
00023 #include <paludis/util/named_value-fwd.hh>
00024 #include <paludis/util/tribool.hh>
00025 
00026 namespace paludis
00027 {
00028     /**
00029      * A NamedValue is used to hold a member of type V_ for a class.
00030      *
00031      * NamedValue is used to simplify 'plain old data' style classes, and to
00032      * provide compiler-time-checked named parameters for functions. Use
00033      * thestruct.themember() and thestruct.themember() = to access the real
00034      * underlying values.
00035      *
00036      * Usually a struct containing NamedValue objects will be constructed using
00037      * the make_named_values() function. For each NamedValue object,
00038      * make_named_values() takes a parameter in the form
00039      * value_for<n::whatever_K_is>(the_value).
00040      *
00041      * In all cases, NamedValue members are listed in name-sorted order, and
00042      * the same name is used for K_ and the member name.
00043      *
00044      * \ingroup g_oo
00045      */
00046     template <typename K_, typename V_>
00047     class NamedValue
00048     {
00049         private:
00050             V_ _value;
00051 
00052         public:
00053             typedef K_ KeyType;
00054             typedef V_ ValueType;
00055 
00056             template <typename T_>
00057             NamedValue(const NamedValue<K_, T_> & v) :
00058                 _value(v())
00059             {
00060             }
00061 
00062             explicit NamedValue(const V_ & v) :
00063                 _value(v)
00064             {
00065             }
00066 
00067             NamedValue(const NamedValue & v) :
00068                 _value(v._value)
00069             {
00070             }
00071 
00072             V_ & operator() ()
00073             {
00074                 return _value;
00075             }
00076 
00077             const V_ & operator() () const
00078             {
00079                 return _value;
00080             }
00081     };
00082 
00083     template <typename K_, typename V_>
00084     NamedValue<K_, V_>
00085     value_for(const V_ & v)
00086     {
00087         return NamedValue<K_, V_>(v);
00088     }
00089 
00090     template <typename K_>
00091     NamedValue<K_, std::string>
00092     value_for(const char * const v)
00093     {
00094         return NamedValue<K_, std::string>(v);
00095     }
00096 
00097     template <typename K_>
00098     NamedValue<K_, Tribool>
00099     value_for(TriboolIndeterminateValueType v)
00100     {
00101         return NamedValue<K_, Tribool>(v);
00102     }
00103 }
00104 
00105 #endif

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