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