$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
equiv.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_VALUE_EQUIV_HH
28 # define MLN_VALUE_EQUIV_HH
29 
39 # include <mln/core/concept/value.hh>
40 
41 
42 # define mln_value_equiv(V) typename mln::value::internal::equiv_<V>::ret
43 # define mln_value_equiv_(V) mln::value::internal::equiv_<V>::ret
44 
45 
46 
47 namespace mln
48 {
49 
50  namespace value
51  {
52 
53  // Fwd decl.
54  namespace internal { template <typename T> struct equiv_; }
55 
56 
57 
59  template <typename V>
60  typename internal::equiv_<V>::ret
61  equiv(const mln::Value<V>& v);
62 
63 
64 
65 # ifndef MLN_INCLUDE_ONLY
66 
67  namespace internal
68  {
69 
70  typedef char yes_;
71  struct no_ { char dummy[2]; };
72 
73  template <typename T>
74  struct make_
75  {
76  static T* ptr();
77  };
78 
79 
80  // Fwd decl.
81  template <unsigned id, typename T>
82  struct equiv_ret_;
83 
84  // Fwd decl.
85  template <typename V>
86  typename internal::equiv_<V>::ret
87  run_equiv_(const V& v);
88 
89 
90  template <typename V, typename T>
91  inline
92  typename internal::equiv_<V>::ret
93  run_equiv_(const Value<V>* v, const T*)
94  {
95  return run_equiv_(exact(v)->to_equiv()); // Rec.
96  }
97 
98  template <typename V>
99  inline
100  V
101  run_equiv_(const void*, const V* v)
102  {
103  return *v; // Stop rec.
104  }
105 
106  template <typename V>
107  inline
108  typename internal::equiv_<V>::ret
109  run_equiv_(const V& v)
110  {
111  return run_equiv_(&v, &v);
112  }
113 
114 
115  template <typename T>
116  struct equiv_ret_< 1, T > // Rec.
117  {
118  typedef typename T::equiv V;
119  typedef typename equiv_<V>::ret ret;
120  };
121 
122  template <typename T>
123  struct equiv_ret_< 2, T > // Stop rec.
124  {
125  typedef T ret;
126  };
127 
128  template <typename V>
129  yes_ equiv_selector_(Value<V>*);
130 
131  no_ equiv_selector_(void*);
132 
133  template <typename T>
134  struct equiv_
135  {
136  enum { id = sizeof(equiv_selector_(make_<T>::ptr())) };
137  typedef typename equiv_ret_<id, T>::ret ret;
138 
139  inline
140  static ret run(const T& t)
141  {
142  return ret::run(t);
143  }
144  };
145 
146  } // end of namespace mln::value::internal
147 
148 
149 
150  template <typename V>
151  inline
152  typename internal::equiv_<V>::ret
153  equiv(const mln::Value<V>& v)
154  {
155  return internal::run_equiv_(exact(v));
156  }
157 
158 
159 # endif // ! MLN_INCLUDE_ONLY
160 
161  } // end of namespace mln::value
162 
163 } // end of namespace mln
164 
165 # include <mln/value/cast.hh>
166 
167 #endif // ! MLN_VALUE_EQUIV_HH