lemon/utility.h
author deba
Mon, 12 Sep 2005 09:15:59 +0000
changeset 1679 e825655c24a4
parent 1447 3351c85ffa02
child 1696 4e03a355d2ea
permissions -rw-r--r--
Some bugfixes.
     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 <typename T>
    70   struct Wrap {
    71     const T &value;
    72     Wrap(const T &t) : value(t) {}
    73   };
    74 
    75   /**************** dummy class to avoid ambiguity ****************/
    76 
    77   template<int T> struct dummy { dummy(int) {} };
    78 
    79   /**************** enable_if from BOOST ****************/
    80  
    81   template <bool B, class T = void>
    82   struct enable_if_c {
    83     typedef T type;
    84   };
    85 
    86   template <class T>
    87   struct enable_if_c<false, T> {};
    88 
    89   template <class Cond, class T = void> 
    90   struct enable_if : public enable_if_c<Cond::value, T> {};
    91 
    92   template <bool B, class T>
    93   struct lazy_enable_if_c {
    94     typedef typename T::type type;
    95   };
    96 
    97   template <class T>
    98   struct lazy_enable_if_c<false, T> {};
    99 
   100   template <class Cond, class T> 
   101   struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
   102 
   103 
   104   template <bool B, class T = void>
   105   struct disable_if_c {
   106     typedef T type;
   107   };
   108 
   109   template <class T>
   110   struct disable_if_c<true, T> {};
   111 
   112   template <class Cond, class T = void> 
   113   struct disable_if : public disable_if_c<Cond::value, T> {};
   114 
   115   template <bool B, class T>
   116   struct lazy_disable_if_c {
   117     typedef typename T::type type;
   118   };
   119 
   120   template <class T>
   121   struct lazy_disable_if_c<true, T> {};
   122 
   123   template <class Cond, class T> 
   124   struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
   125 
   126   // smart referencing
   127 
   128   template <typename _Type, typename Enable = void>
   129   struct SmartReference {
   130     typedef _Type& Type;
   131   };
   132   
   133   template <typename _Type>
   134   struct SmartReference<
   135     _Type, 
   136     typename enable_if<typename _Type::NeedCopy, void>::type
   137   > {
   138     typedef _Type Type;
   139   };
   140 
   141   template <typename _Type, typename Enable = void>
   142   struct SmartConstReference {
   143     typedef const _Type& Type;
   144   };
   145   
   146   template <typename _Type>
   147   struct SmartConstReference<
   148     _Type, 
   149     typename enable_if<typename _Type::NeedCopy, void>::type
   150   > {
   151     typedef const _Type Type;
   152   };
   153 
   154   template <typename _Type, typename Enable = void>
   155   struct SmartParameter {
   156     typedef _Type& Type;
   157   };
   158   
   159   template <typename _Type>
   160   struct SmartParameter<
   161     _Type, 
   162     typename enable_if<typename _Type::NeedCopy, void>::type
   163   > {
   164     typedef const _Type& Type;
   165   };
   166 
   167 } // namespace lemon
   168 
   169 #endif