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