Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
violent_cast_image.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_CORE_IMAGE_VMORPH_VIOLENT_CAST_IMAGE_HH
28 # define MLN_CORE_IMAGE_VMORPH_VIOLENT_CAST_IMAGE_HH
29 
34 
35 # include <mln/core/internal/image_value_morpher.hh>
36 # include <mln/value/set.hh>
37 # include <mln/value/cast.hh>
38 
39 
40 namespace mln
41 {
42 
43  // Fwd decl.
44  template <typename T, typename I> struct violent_cast_image;
45 
46  namespace internal
47  {
49  template <typename T, typename I>
50  struct data< violent_cast_image<T,I> >
51  {
52  data(const I& ima);
53  const I& ima_;
54  };
55 
56  } // end of namespace mln::internal
57 
58 
59 
60  namespace trait
61  {
62  template <typename T, typename I, typename value_io>
63  struct violent_cast_image_trait_selector :
64  default_image_morpher< I, T, violent_cast_image<T,I> >
65  {
66  typedef trait::image::vw_io::none vw_io;
67  typedef trait::image::vw_set::none vw_set;
68  };
69 
70  template <typename T, typename I>
71  struct violent_cast_image_trait_selector<T, I, trait::image::vw_io::read> :
72  default_image_morpher< I, T, violent_cast_image<T,I> >
73  {
74  typedef trait::image::vw_io::read vw_io;
75  };
76 
77  template <typename T, typename I>
78  struct violent_cast_image_trait_selector<T, I, trait::image::vw_io::read_write> :
79  default_image_morpher< I, T, violent_cast_image<T,I> >
80  {
81  typedef trait::image::vw_io::read vw_io;
82  };
83 
84  template <typename T, typename I>
85  struct image_< violent_cast_image<T,I> > :
86  violent_cast_image_trait_selector<T, I, mln_trait_image_vw_io(I)>
87  {
88  private:
89  typedef mlc_equal(mln_trait_value_quant(T),
90  trait::value::quant::high) is_high_quant_;
91  public:
92  typedef mlc_if(is_high_quant_,
93  trait::image::quant::high,
94  trait::image::quant::low) quant;
95 
96  typedef trait::image::value_io::read_only value_io;
97  typedef trait::image::pw_io::read pw_io;
98  typedef trait::image::value_access::indirect value_access;
99  typedef trait::image::category::value_morpher category;
100 
102  typedef trait::image::ext_domain::none ext_domain;
103  };
104  } // end of namespace mln::trait
105 
106 
107 
111  //
112  template <typename T, typename I>
114  public internal::image_value_morpher< I, T, violent_cast_image<T,I> >
115  {
117  typedef T value;
118 
120  typedef T rvalue;
121 
123  typedef T lvalue;
124 
126  typedef violent_cast_image< tag::value_<T>, tag::image_<I> > skeleton;
127 
129  violent_cast_image(const Image<I>& ima);
130 
132  void init_(const Image<I>& ima);
133 
135  T operator()(const mln_psite(I)& p) const;
136 
138  T operator()(const mln_psite(I)& p);
139  };
140 
141 
142  template <typename T, typename I>
144  violent_cast_image_(const Image<I>& ima)
145  {
146  mln_precondition(exact(ima).is_valid());
147  violent_cast_image<T,I> tmp(ima);
148  return tmp;
149  }
150 
151 
152 # ifndef MLN_INCLUDE_ONLY
153 
154 
155  // internal::data< violent_cast_image<T,I> >
156 
157  namespace internal
158  {
159 
160  template <typename T, typename I>
161  inline
163  : ima_(ima)
164  {
165  }
166 
167  } // end of namespace mln::internal
168 
169  // violent_cast_image<T,I>
170 
171  template <typename T, typename I>
172  inline
174  {
175  metal::bool_<sizeof(T) == sizeof(typename I::value)>::check();
176  mln_precondition(exact(ima).is_valid());
177  this->data_ = new internal::data< violent_cast_image<T,I> >(exact(ima));
178  }
179 
180  template <typename T, typename I>
181  inline
182  void
184  {
185  mln_precondition(exact(ima).is_valid());
186  this->data_ = new internal::data<violent_cast_image<T,I> >(exact(ima));
187  }
188 
189  template <typename T, typename I>
190  inline
191  T
192  violent_cast_image<T,I>::operator()(const mln_psite(I)& p) const
193  {
194  mln_precondition(this->data_->ima_.has(p));
195  return *(T*)(void*)&( this->data_->ima_(p) );
196  }
197 
198  template <typename T, typename I>
199  inline
200  T
201  violent_cast_image<T,I>::operator()(const mln_psite(I)& p)
202  {
203  return *(T*)(void*)&( this->data_->ima_(p) );
204  }
205 
206 # endif // ! MLN_INCLUDE_ONLY
207 
208 } // end of namespace mln
209 
210 
211 #endif // ! MLN_CORE_IMAGE_VMORPH_VIOLENT_CAST_IMAGE_HH