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