lemon/utility.h
author deba
Mon, 06 Feb 2006 20:32:29 +0000
changeset 1964 df0b07457083
parent 1875 98698b69a902
child 1989 d276e88aa48a
permissions -rw-r--r--
Bug fix
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 // This file contains a modified version of the enable_if library from BOOST.
    20 // See the appropriate copyright notice below.
    21 
    22 // Boost enable_if library
    23 
    24 // Copyright 2003 © The Trustees of Indiana University.
    25 
    26 // Use, modification, and distribution is subject to the Boost Software
    27 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    28 // http://www.boost.org/LICENSE_1_0.txt)
    29 
    30 //    Authors: Jaakko Järvi (jajarvi at osl.iu.edu)
    31 //             Jeremiah Willcock (jewillco at osl.iu.edu)
    32 //             Andrew Lumsdaine (lums at osl.iu.edu)
    33 
    34 
    35 #ifndef LEMON_UTILITY_H
    36 #define LEMON_UTILITY_H
    37 
    38 ///\file
    39 ///\brief Miscellaneous basic utilities
    40 ///
    41 ///\todo Please rethink the organisation of the basic files like this.
    42 ///E.g. this file might be merged with invalid.h.
    43 
    44 
    45 namespace lemon
    46 {
    47 
    48   /// Basic type for defining "tags". A "YES" condition for \c enable_if.
    49 
    50   /// Basic type for defining "tags". A "YES" condition for \c enable_if.
    51   ///
    52   ///\sa False
    53   ///
    54   /// \todo This should go to a separate "basic_types.h" (or something)
    55   /// file.
    56   struct True {
    57     ///\e
    58     static const bool value = true;
    59   };
    60 
    61   /// Basic type for defining "tags". A "NO" condition for \c enable_if.
    62 
    63   /// Basic type for defining "tags". A "NO" condition for \c enable_if.
    64   ///
    65   ///\sa True
    66   struct False {
    67     ///\e
    68     static const bool value = false;
    69   };
    70 
    71   template <bool left, bool right>
    72   struct _CompileTimeAnd {
    73     static const bool value = false;
    74   };
    75   
    76   template <>
    77   struct _CompileTimeAnd<true, true> {
    78     static const bool value = true;
    79   };
    80 
    81   template <typename Left, typename Right>
    82   struct CompileTimeAnd {
    83     static const bool value = 
    84     _CompileTimeAnd<Left::value, Right::value>::value;
    85   };
    86 
    87   template <typename T>
    88   struct Wrap {
    89     const T &value;
    90     Wrap(const T &t) : value(t) {}
    91   };
    92 
    93   /**************** dummy class to avoid ambiguity ****************/
    94 
    95   template<int T> struct dummy { dummy(int) {} };
    96 
    97   /**************** enable_if from BOOST ****************/
    98  
    99   template <bool B, class T = void>
   100   struct enable_if_c {
   101     typedef T type;
   102   };
   103 
   104   template <class T>
   105   struct enable_if_c<false, T> {};
   106 
   107   template <class Cond, class T = void> 
   108   struct enable_if : public enable_if_c<Cond::value, T> {};
   109 
   110   template <bool B, class T>
   111   struct lazy_enable_if_c {
   112     typedef typename T::type type;
   113   };
   114 
   115   template <class T>
   116   struct lazy_enable_if_c<false, T> {};
   117 
   118   template <class Cond, class T> 
   119   struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
   120 
   121 
   122   template <bool B, class T = void>
   123   struct disable_if_c {
   124     typedef T type;
   125   };
   126 
   127   template <class T>
   128   struct disable_if_c<true, T> {};
   129 
   130   template <class Cond, class T = void> 
   131   struct disable_if : public disable_if_c<Cond::value, T> {};
   132 
   133   template <bool B, class T>
   134   struct lazy_disable_if_c {
   135     typedef typename T::type type;
   136   };
   137 
   138   template <class T>
   139   struct lazy_disable_if_c<true, T> {};
   140 
   141   template <class Cond, class T> 
   142   struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
   143 
   144 } // namespace lemon
   145 
   146 #endif