Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
core/internal/image_base.hh
1 // Copyright (C) 2007, 2008, 2009 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_INTERNAL_IMAGE_BASE_HH
28 # define MLN_CORE_INTERNAL_IMAGE_BASE_HH
29 
35 
36 # include <mln/core/concept/image.hh>
37 # include <mln/core/grids.hh>
38 # include <mln/core/trait/qlf_value.hh>
39 # include <mln/core/internal/check/image_all.hh>
40 # include <mln/core/internal/data.hh>
41 # include <mln/core/internal/morpher_lvalue.hh>
42 # include <mln/util/tracked_ptr.hh>
43 # include <mln/value/set.hh>
44 # include <mln/value/super_value.hh>
45 
46 // image_base
47 // ^
48 // |
49 // ---------------------------
50 // | |
51 // image_primary image_morpher
52 // ^ ^
53 // | |
54 // | -----------------------------------------
55 // | | | |
56 // pw_image_base image_domain_morpher image_value_morpher image_identity
57 
58 
59 namespace mln
60 {
61 
62  namespace internal
63  {
64 
65 
66  template <typename E>
67  struct image_checked_
68  :
69  public check::image_all_<E>,
70  public Image<E>
71  {
72  };
73 
74 
75 
80  //
81  template <typename T, typename S, typename E>
82  struct image_base
83  :
84  public image_checked_<E>
85 
86  {
88  typedef T value;
89 
91  typedef mln::value::set<T> t_eligible_values_set;
92 
93  // Return the set of the image eligigle values
94  const t_eligible_values_set& values_eligible() const;
95 
97  typedef mln::value::set<
98  typename mln::value::super_value<T>::ret > t_values_space;
99 
101  const t_values_space& values_space() const;
102 
103 
105  typedef S domain_t;
106 
108  typedef mln_psite(S) psite;
109 
111  typedef mln_site(S) site;
112 
113 
115  typedef mln_fwd_piter(S) fwd_piter;
116 
118  typedef mln_bkd_piter(S) bkd_piter;
119 
120 
123  typedef fwd_piter piter;
124 
125 
126 
128  bool has(const psite& p) const;
129 
131  std::size_t nsites() const;
132 
134  bool is_valid() const;
135 
136  // FIXME: Add void init_data(..);
137 
138 
140  image_base& operator=(const image_base& rhs);
141 
143  image_base(const image_base& rhs);
144 
148  const void* id_() const;
149 
150 
152  void destroy();
153 
155  const util::tracked_ptr< internal::data<E> >& hook_data_() const;
156 
157  protected:
158 
160  image_base();
161 
162  // Internal data, sharable by several images.
163  util::tracked_ptr< internal::data<E> > data_;
164  };
165 
166 
167 
168 # ifndef MLN_INCLUDE_ONLY
169 
170  template <typename T, typename S, typename E>
171  inline
172  image_base<T, S, E>::image_base()
173  {
174  }
175 
176  template <typename T, typename S, typename E>
177  inline
178  image_base<T, S, E>::image_base(const image_base& rhs)
179  : image_checked_<E>()
180  {
181  mln_precondition(exact(rhs).is_valid()); // FIXME: Is-it too restrictive?
182  this->data_ = rhs.data_;
183  }
184 
185  template <typename T, typename S, typename E>
186  inline
187  image_base<T, S, E>&
188  image_base<T, S, E>::operator=(const image_base<T, S, E>& rhs)
189  {
190  mln_precondition(exact(rhs).is_valid()); // FIXME: Is-it too restrictive?
191  if (& rhs == this) // || ! exact(rhs).is_valid())
192  return *this;
193  this->data_ = rhs.data_;
194  return *this;
195  }
196 
197  template <typename T, typename S, typename E>
198  inline
199  const void*
200  image_base<T, S, E>::id_() const
201  {
202  return data_.ptr_;
203  }
204 
205  template <typename T, typename S, typename E>
206  inline
207  bool
208  image_base<T, S, E>::is_valid() const
209  {
210  return data_ != 0;
211  }
212 
213  template <typename T, typename S, typename E>
214  inline
215  bool
216  image_base<T, S, E>::has(const psite& p) const
217  {
218  mln_precondition(exact(this)->is_valid());
219  return exact(this)->domain().has(p);
220  }
221 
222  template <typename T, typename S, typename E>
223  inline
224  std::size_t
225  image_base<T, S, E>::nsites() const
226  {
227  mlc_equal(mln_trait_site_set_nsites(S),
228  mln::trait::site_set::nsites::known)::check();
229  mln_precondition(exact(this)->is_valid());
230  return exact(this)->domain().nsites();
231  }
232 
233  template <typename T, typename S, typename E>
234  inline
235  const typename image_base<T, S, E>::t_eligible_values_set&
236  image_base<T, S, E>::values_eligible() const
237  {
238  return t_eligible_values_set::the();
239  }
240 
241  template <typename T, typename S, typename E>
242  inline
243  const typename image_base<T, S, E>::t_values_space&
244  image_base<T, S, E>::values_space() const
245  {
246  return t_values_space::the();
247  }
248 
249  template <typename T, typename S, typename E>
250  inline
251  void
252  image_base<T, S, E>::destroy()
253  {
254  data_.clean_();
255  }
256 
257  template <typename T, typename S, typename E>
258  inline
259  const util::tracked_ptr< internal::data<E> >&
260  image_base<T, S, E>::hook_data_() const
261  {
262  return data_;
263  }
264 
265 # endif // ! MLN_INCLUDE_ONLY
266 
267  } // end of namespace mln::internal
268 
269 } // end of namespace mln
270 
271 
272 #endif // ! MLN_CORE_INTERNAL_IMAGE_BASE_HH