lemon/utility.h
changeset 1435 8e85e6bbefdf
parent 1418 afaa773d0ad0
child 1447 3351c85ffa02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/utility.h	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,152 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/utility.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
     1.8 + * Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
     1.9 + * EGRES).
    1.10 + *
    1.11 + * Permission to use, modify and distribute this software is granted
    1.12 + * provided that this copyright notice appears in all copies. For
    1.13 + * precise terms see the accompanying LICENSE file.
    1.14 + *
    1.15 + * This software is provided "AS IS" with no warranty of any kind,
    1.16 + * express or implied, and with no claim as to its suitability for any
    1.17 + * purpose.
    1.18 + *
    1.19 + * This file contains a modified version of the enable_if library from BOOST.
    1.20 + * See the appropriate copyright notice below.
    1.21 + */
    1.22 +
    1.23 +// Boost enable_if library
    1.24 +
    1.25 +// Copyright 2003 © The Trustees of Indiana University.
    1.26 +
    1.27 +// Use, modification, and distribution is subject to the Boost Software
    1.28 +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    1.29 +// http://www.boost.org/LICENSE_1_0.txt)
    1.30 +
    1.31 +//    Authors: Jaakko Järvi (jajarvi at osl.iu.edu)
    1.32 +//             Jeremiah Willcock (jewillco at osl.iu.edu)
    1.33 +//             Andrew Lumsdaine (lums at osl.iu.edu)
    1.34 +
    1.35 +
    1.36 +#ifndef LEMON_UTILITY_H
    1.37 +#define LEMON_UTILITY_H
    1.38 +
    1.39 +namespace lemon
    1.40 +{
    1.41 +
    1.42 +  /// Basic type for defining "tags". A "YES" condition for enable_if.
    1.43 +
    1.44 +  /// \todo This should go to a separate "basic_types.h" (or something)
    1.45 +  /// file.
    1.46 +  struct True {
    1.47 +    static const bool value = true;
    1.48 +  };
    1.49 +
    1.50 +  /// Basic type for defining "tags". A "NO" condition for enable_if.
    1.51 +  struct False {
    1.52 +    static const bool value = false;
    1.53 +  };
    1.54 +
    1.55 +  template <typename T>
    1.56 +  struct Wrap {
    1.57 +    const T &value;
    1.58 +    Wrap(const T &t) : value(t) {}
    1.59 +  };
    1.60 +
    1.61 +  /**************** dummy class to avoid ambiguity ****************/
    1.62 +
    1.63 +  template<int T> struct dummy { dummy(int) {} };
    1.64 +
    1.65 +  /**************** enable_if from BOOST ****************/
    1.66 + 
    1.67 +  template <bool B, class T = void>
    1.68 +  struct enable_if_c {
    1.69 +    typedef T type;
    1.70 +  };
    1.71 +
    1.72 +  template <class T>
    1.73 +  struct enable_if_c<false, T> {};
    1.74 +
    1.75 +  template <class Cond, class T = void> 
    1.76 +  struct enable_if : public enable_if_c<Cond::value, T> {};
    1.77 +
    1.78 +  template <bool B, class T>
    1.79 +  struct lazy_enable_if_c {
    1.80 +    typedef typename T::type type;
    1.81 +  };
    1.82 +
    1.83 +  template <class T>
    1.84 +  struct lazy_enable_if_c<false, T> {};
    1.85 +
    1.86 +  template <class Cond, class T> 
    1.87 +  struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
    1.88 +
    1.89 +
    1.90 +  template <bool B, class T = void>
    1.91 +  struct disable_if_c {
    1.92 +    typedef T type;
    1.93 +  };
    1.94 +
    1.95 +  template <class T>
    1.96 +  struct disable_if_c<true, T> {};
    1.97 +
    1.98 +  template <class Cond, class T = void> 
    1.99 +  struct disable_if : public disable_if_c<Cond::value, T> {};
   1.100 +
   1.101 +  template <bool B, class T>
   1.102 +  struct lazy_disable_if_c {
   1.103 +    typedef typename T::type type;
   1.104 +  };
   1.105 +
   1.106 +  template <class T>
   1.107 +  struct lazy_disable_if_c<true, T> {};
   1.108 +
   1.109 +  template <class Cond, class T> 
   1.110 +  struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
   1.111 +
   1.112 +  // smart referencing
   1.113 +
   1.114 +  template <typename _Type, typename Enable = void>
   1.115 +  struct SmartReference {
   1.116 +    typedef _Type& Type;
   1.117 +  };
   1.118 +  
   1.119 +  template <typename _Type>
   1.120 +  struct SmartReference<
   1.121 +    _Type, 
   1.122 +    typename enable_if<typename _Type::NeedCopy, void>::type
   1.123 +  > {
   1.124 +    typedef _Type Type;
   1.125 +  };
   1.126 +
   1.127 +  template <typename _Type, typename Enable = void>
   1.128 +  struct SmartConstReference {
   1.129 +    typedef const _Type& Type;
   1.130 +  };
   1.131 +  
   1.132 +  template <typename _Type>
   1.133 +  struct SmartConstReference<
   1.134 +    _Type, 
   1.135 +    typename enable_if<typename _Type::NeedCopy, void>::type
   1.136 +  > {
   1.137 +    typedef const _Type Type;
   1.138 +  };
   1.139 +
   1.140 +  template <typename _Type, typename Enable = void>
   1.141 +  struct SmartParameter {
   1.142 +    typedef _Type& Type;
   1.143 +  };
   1.144 +  
   1.145 +  template <typename _Type>
   1.146 +  struct SmartParameter<
   1.147 +    _Type, 
   1.148 +    typename enable_if<typename _Type::NeedCopy, void>::type
   1.149 +  > {
   1.150 +    typedef const _Type& Type;
   1.151 +  };
   1.152 +
   1.153 +} // namespace lemon
   1.154 +
   1.155 +#endif