COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/klao/tag_demo.cc @ 976:04591f9a4173

Last change on this file since 976:04591f9a4173 was 976:04591f9a4173, checked in by Mihaly Barasz, 15 years ago

A demonstration how to use _optional_ boolean tags.

File size: 1.9 KB
Line 
1#include <iostream>
2#include <boost/utility.hpp>
3
4using namespace std;
5using namespace boost;
6
7struct True {
8  static const bool value = true;
9};
10struct False {
11  static const bool value = false;
12};
13
14// Here are some graph structures. Some of them define the type "OneTag"
15// to True or False, some does not. Not defining a tag is (or at least
16// should be) equivalent to defining it to "False".
17struct Graph1 {};
18struct Graph2 {
19  typedef True OneTag;
20};
21struct Graph3 {
22  typedef False OneTag;
23};
24
25
26/**************** The first method to use tags ****************/
27
28template <typename Graph, typename Enable = void>
29struct HasOneTag {
30  typedef False TheTag;
31};
32
33// specialization for those graphs which defined the tag to be true:
34template <typename Graph>
35struct HasOneTag<Graph, typename enable_if<typename Graph::OneTag>::type > {
36  typedef True TheTag;
37};
38
39template <typename Graph>
40int cn1(const Graph &, False) {
41  return 0;
42}
43
44template <typename Graph>
45int cn1(const Graph &, True) {
46  return 1;
47}
48
49template <typename Graph>
50int cn1(const Graph &g) {
51  return cn1(g, typename HasOneTag<Graph>::TheTag());
52}
53
54/**************** The second method ****************/
55
56// An artificial type to provoke a conversion to avoid ambuguity...
57template <typename T>
58struct Wrap {
59  const T &value;
60  Wrap(const T &t) : value(t) {}
61};
62
63template <typename Graph>
64typename enable_if<typename Graph::OneTag, int>::type
65_cn2(const Graph &) {
66  return 1;
67}
68
69template <typename Graph>
70int _cn2(Wrap<Graph>) {
71  return 0;
72}
73
74template <typename Graph>
75int cn2(const Graph& g) {
76  return _cn2<Graph>(g);
77}
78
79
80int main() {
81  Graph1 g1;
82  Graph2 g2;
83  Graph3 g3;
84
85  cout << "The first method:\n";
86  cout << "G1: " << cn1(g1) << endl;
87  cout << "G2: " << cn1(g2) << endl;
88  cout << "G3: " << cn1(g3) << endl;
89
90  cout << "The second method:\n";
91  cout << "G1: " << cn2(g1) << endl;
92  cout << "G2: " << cn2(g2) << endl;
93  cout << "G3: " << cn2(g3) << endl;
94
95}
Note: See TracBrowser for help on using the repository browser.