www.pudn.com > Loki.rar > MinMax.h


//////////////////////////////////////////////////////////////////////////////// 
// The Loki Library 
// Copyright (c) 2001 by Andrei Alexandrescu 
// This code accompanies the book: 
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design  
//     Patterns Applied". Copyright (c) 2001. Addison-Wesley. 
// Permission to use, copy, modify, distribute and sell this software for any  
//     purpose is hereby granted without fee, provided that the above copyright  
//     notice appear in all copies and that both that copyright notice and this  
//     permission notice appear in supporting documentation. 
// The author or Addison-Welsey Longman make no representations about the  
//     suitability of this software for any purpose. It is provided "as is"  
//     without express or implied warranty. 
//////////////////////////////////////////////////////////////////////////////// 
 
// Last update: May 19, 2002 
 
#ifndef MINMAX_INC_ 
#define MINMAX_INC_ 
 
#include "Typelist.h" 
#include "TypeTraits.h" 
 
namespace Private 
{ 
    typedef TYPELIST_14( 
            const bool, 
            const char, 
            const signed char, 
            const unsigned char, 
            const wchar_t, 
            const short int, 
            const unsigned short int, 
            const int, 
            const unsigned int, 
            const long int, 
            const unsigned long int, 
            const float, 
            const double, 
            const long double) 
        ArithTypes; 
} 
 
template  
class MinMaxTraits 
{ 
    typedef typename Loki::Select::exists,  
            L, R>::Result 
        T1; 
     
    enum { pos1 = Loki::TL::IndexOf::value }; 
    enum { pos2 = Loki::TL::IndexOf::value }; 
    typedef Loki::Select::Result T2; 
 
    enum { rConst = Loki::TypeTraits::isConst >= 
        Loki::TypeTraits::isConst }; 
    enum { l2r = rConst && Loki::Conversion< 
        typename Loki::TypeTraits::NonConstType&,  
        typename Loki::TypeTraits::NonConstType&>::exists }; 
    typedef typename Loki::Select::Result T3; 
 
    enum { lConst = Loki::TypeTraits::isConst >= 
        Loki::TypeTraits::isConst }; 
    enum { r2l = lConst && Loki::Conversion< 
        typename Loki::TypeTraits::NonConstType&,  
        typename Loki::TypeTraits::NonConstType&>::exists }; 
public: 
    typedef typename Loki::Select::Result Result; 
}; 
 
template  
typename MinMaxTraits::Result 
Min(L& lhs, R& rhs) 
{ if (lhs < rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Min(const L& lhs, R& rhs) 
{ if (lhs < rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Min(L& lhs, const R& rhs) 
{ if (lhs < rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Min(const L& lhs, const R& rhs) 
{ if (lhs < rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Max(L& lhs, R& rhs) 
{ if (lhs > rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Max(const L& lhs, R& rhs) 
{ if (lhs > rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Max(L& lhs, const R& rhs) 
{ if (lhs > rhs) return lhs; return rhs; } 
 
template  
typename MinMaxTraits::Result 
Max(const L& lhs, const R& rhs) 
{ if (lhs > rhs) return lhs; return rhs; } 
 
 
#endif // MINMAX_INC_