Menu

Duplicates.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  *                    2010 Marcus Foerster <MarcusFoerster1@gmx.de>
00005  * All rights reserved.
00006  *
00007  *    Redistribution and use in source and binary forms, with or without
00008  *    modification, are permitted provided that the following conditions
00009  *    are met:
00010  *
00011  *    * Redistributions of source code must retain the above copyright
00012  *      notice, this list of conditions and the following disclaimer.
00013  *
00014  *    * Redistributions in binary form must reproduce the above copyright
00015  *      notice, this list of conditions and the following disclaimer in
00016  *      the documentation and/or other materials provided with the
00017  *      distribution.
00018  *
00019  *    * Neither the name of the copyright holders nor the names of
00020  *      contributors may be used to endorse or promote products derived
00021  *      from this software without specific prior written permission.
00022  *
00023  *
00024  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00025  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  *
00037  * $Id$
00038  *
00039  ******************************************************************************/
00040 
00041 #ifndef _Duplicates_h_
00042 #define _Duplicates_h_
00043 
00044 #include "boost/mpl/begin.hpp"
00045 #include "boost/mpl/end.hpp"
00046 #include "boost/mpl/next.hpp"
00047 #include "boost/mpl/deref.hpp"
00048 
00049 #include "boost/mpl/eval_if.hpp"
00050 #include "boost/mpl/count_if.hpp"
00051 
00052 #include "mw/attributes/type_traits/is_same_base_type.h"
00053 
00054 namespace famouso {
00055     namespace mw {
00056         namespace attributes {
00057             namespace detail {
00058 
00071                 template <typename AttrSeq, typename Iter = typename boost::mpl::begin<AttrSeq>::type>
00072                 struct Duplicates {
00073                     private:
00074                         // The current attribute
00075                         typedef boost::mpl::deref<Iter> curAttr;
00076 
00077                         // A predicate testing every attribute of the sequence against the current
00078                         //  one based on the base type
00079                         typedef famouso::mw::attributes::type_traits::is_same_base_type<
00080                                                                             boost::mpl::_1,
00081                                                                             typename curAttr::type
00082                                                                       > pred;
00083 
00084                         // The number of attributes in the sequence matching the current one
00085                         typedef typename boost::mpl::count_if<AttrSeq, pred> currentCount;
00086 
00087                         // The iterator pointing to the next attribute in the sequence
00088                         typedef boost::mpl::next<Iter> nextIter;
00089 
00090                         // The Duplicates struct using the next iterator
00091                         typedef Duplicates<AttrSeq, typename nextIter::type> nextDupl;
00092 
00093                     public:
00098                         static const bool result = (currentCount::value > 1) ? true : nextDupl::result;
00099 
00103                         typedef typename boost::mpl::eval_if_c<
00104                                                       result,
00105                                                       typename curAttr::type,
00106                                                       typename nextDupl::duplicateAttribute
00107                                                      >::type duplicateAttribute;
00108                 };
00109 
00113                 template <typename AttrSeq>
00114                 struct Duplicates<AttrSeq, typename boost::mpl::end<AttrSeq>::type> {
00115                     private:
00116                         struct Dummy {
00117                             typedef Dummy type;
00118                         };
00119 
00120                     public:
00121                         // \todo: Could anyone please tell me why a static const member is
00122                         //  not "constant enough" so that I must use this ugly enum?!
00123                         // (and why is it sufficient in the general template definition above?!)
00124                         enum {
00125                             result = false
00126                         };
00127 
00128                         typedef Dummy duplicateAttribute;
00129                 };
00130 
00131             }  // end namespace detail
00132         }  // end namespace attributes
00133     }  // end namespace mw
00134 }  // end namespace famouso
00135 
00136 #endif