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