lemon/utility.h
author ladanyi
Mon, 23 May 2005 04:48:14 +0000
changeset 1435 8e85e6bbefdf
parent 1418 src/lemon/utility.h@afaa773d0ad0
child 1447 3351c85ffa02
permissions -rw-r--r--
trunk/src/* move to trunk/
     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 namespace lemon
    37 {
    38 
    39   /// Basic type for defining "tags". A "YES" condition for enable_if.
    40 
    41   /// \todo This should go to a separate "basic_types.h" (or something)
    42   /// file.
    43   struct True {
    44     static const bool value = true;
    45   };
    46 
    47   /// Basic type for defining "tags". A "NO" condition for enable_if.
    48   struct False {
    49     static const bool value = false;
    50   };
    51 
    52   template <typename T>
    53   struct Wrap {
    54     const T &value;
    55     Wrap(const T &t) : value(t) {}
    56   };
    57 
    58   /**************** dummy class to avoid ambiguity ****************/
    59 
    60   template<int T> struct dummy { dummy(int) {} };
    61 
    62   /**************** enable_if from BOOST ****************/
    63  
    64   template <bool B, class T = void>
    65   struct enable_if_c {
    66     typedef T type;
    67   };
    68 
    69   template <class T>
    70   struct enable_if_c<false, T> {};
    71 
    72   template <class Cond, class T = void> 
    73   struct enable_if : public enable_if_c<Cond::value, T> {};
    74 
    75   template <bool B, class T>
    76   struct lazy_enable_if_c {
    77     typedef typename T::type type;
    78   };
    79 
    80   template <class T>
    81   struct lazy_enable_if_c<false, T> {};
    82 
    83   template <class Cond, class T> 
    84   struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
    85 
    86 
    87   template <bool B, class T = void>
    88   struct disable_if_c {
    89     typedef T type;
    90   };
    91 
    92   template <class T>
    93   struct disable_if_c<true, T> {};
    94 
    95   template <class Cond, class T = void> 
    96   struct disable_if : public disable_if_c<Cond::value, T> {};
    97 
    98   template <bool B, class T>
    99   struct lazy_disable_if_c {
   100     typedef typename T::type type;
   101   };
   102 
   103   template <class T>
   104   struct lazy_disable_if_c<true, T> {};
   105 
   106   template <class Cond, class T> 
   107   struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
   108 
   109   // smart referencing
   110 
   111   template <typename _Type, typename Enable = void>
   112   struct SmartReference {
   113     typedef _Type& Type;
   114   };
   115   
   116   template <typename _Type>
   117   struct SmartReference<
   118     _Type, 
   119     typename enable_if<typename _Type::NeedCopy, void>::type
   120   > {
   121     typedef _Type Type;
   122   };
   123 
   124   template <typename _Type, typename Enable = void>
   125   struct SmartConstReference {
   126     typedef const _Type& Type;
   127   };
   128   
   129   template <typename _Type>
   130   struct SmartConstReference<
   131     _Type, 
   132     typename enable_if<typename _Type::NeedCopy, void>::type
   133   > {
   134     typedef const _Type Type;
   135   };
   136 
   137   template <typename _Type, typename Enable = void>
   138   struct SmartParameter {
   139     typedef _Type& Type;
   140   };
   141   
   142   template <typename _Type>
   143   struct SmartParameter<
   144     _Type, 
   145     typename enable_if<typename _Type::NeedCopy, void>::type
   146   > {
   147     typedef const _Type& Type;
   148   };
   149 
   150 } // namespace lemon
   151 
   152 #endif