$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
object_links.hh
1 // Copyright (C) 2009, 2010, 2011, 2013 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 SCRIBO_CORE_OBJECT_LINKS_HH
28 # define SCRIBO_CORE_OBJECT_LINKS_HH
29 
33 
34 
35 # include <mln/util/array.hh>
36 # include <mln/util/tracked_ptr.hh>
37 
38 # include <scribo/core/component_set.hh>
39 
40 # include <scribo/core/concept/serializable.hh>
41 
42 namespace scribo
43 {
44 
45  using namespace mln;
46 
47  // Forward declaration.
48  template <typename L> class object_links;
49 
50 
51  namespace internal
52  {
53 
55  template <typename L>
57  {
59  object_links_data(const component_set<L>& components, unsigned size);
60  object_links_data(const component_set<L>& components,
61  unsigned size, unsigned default_link_id);
62 
63  mln::util::array<unsigned> comp_to_link_;
64  component_set<L> components_;
65  };
66 
67  } // end of namespace scribo::internal
68 
69 
70 
71 
87  template <typename L>
88  class object_links : public Serializable<object_links<L> >
89  {
91 
92  public:
94  object_links();
95 
100 
104  object_links(const component_set<L>& components, unsigned default_link_id);
105 
107  const component_set<L>& components() const;
108 
111  bool is_valid() const;
112 
115  bool has_linking_enabled(unsigned comp_id) const;
116 
119  bool is_linked(unsigned comp_id) const;
120 
123  unsigned nelements() const;
124 
128  void update(unsigned from_id, unsigned to_id);
129 
132  void clear(unsigned id);
133 
136  void disable_linking(unsigned id);
137 
139  const unsigned& operator()(unsigned comp_id) const;
141 
145  const mln::util::array<unsigned>& comp_to_link() const;
146 
149  void init();
150 
152  object_links<L> duplicate() const;
153 
154  private:
156  };
157 
158 
160  template <typename L>
161  std::ostream&
162  operator<<(std::ostream& ostr, const object_links<L>& links);
163 
164 
165 # ifndef MLN_INCLUDE_ONLY
166 
167 
168  namespace internal
169  {
170 
171 
173  template <typename L>
174  object_links_data<L>::object_links_data()
175  {
176  }
177 
178 
179  template <typename L>
180  object_links_data<L>::object_links_data(const component_set<L>& components,
181  unsigned size)
182  : comp_to_link_(size), components_(components)
183  {
184  };
185 
186 
187  template <typename L>
188  object_links_data<L>::object_links_data(const component_set<L>& components,
189  unsigned size, unsigned default_link_id)
190  : comp_to_link_(size, default_link_id), components_(components)
191  {
192  };
193 
194 
195  } // end of namespace scribo::internal
196 
197 
198 
199  template <typename L>
200  object_links<L>::object_links()
201  {
202  data_ = new data_t();
203  }
204 
205 
206  template <typename L>
207  object_links<L>::object_links(const component_set<L>& components)
208  {
209  data_ = new data_t(components, value::next(components.nelements()));
210 
211  // Initialize with no link.
212  init();
213  }
214 
215 
216  template <typename L>
217  object_links<L>::object_links(const component_set<L>& components,
218  unsigned default_link_id)
219  {
220  data_ = new data_t(components, value::next(components.nelements()),
221  default_link_id);
222  }
223 
224 
225  template <typename L>
226  const component_set<L>&
228  {
229  return data_->components_;
230  }
231 
232 
233  template <typename L>
234  bool
235  object_links<L>::is_valid() const
236  {
237  return data_->components_.is_valid()
238  && data_->components_.nelements() == (this->nelements() - 1);
239  }
240 
241 
242  template <typename L>
243  bool
244  object_links<L>::has_linking_enabled(unsigned comp_id) const
245  {
246  mln_precondition(is_valid());
247  mln_precondition(comp_id < data_->comp_to_link_.nelements());
248 
249  return data_->comp_to_link_(comp_id) != 0;
250  }
251 
252  template <typename L>
253  bool
254  object_links<L>::is_linked(unsigned comp_id) const
255  {
256  mln_precondition(is_valid());
257  mln_precondition(comp_id < data_->comp_to_link_.nelements());
258 
259  return has_linking_enabled(comp_id)
260  && data_->comp_to_link_(comp_id) != comp_id;
261  }
262 
263 
264  template <typename L>
265  unsigned
266  object_links<L>::nelements() const
267  {
268  return data_->comp_to_link_.nelements();
269  }
270 
271 
272  template <typename L>
273  const unsigned&
274  object_links<L>::operator()(unsigned comp_id) const
275  {
276  return data_->comp_to_link_(comp_id);
277  }
278 
279  template <typename L>
280  void
281  object_links<L>::update(unsigned from_id, unsigned to_id)
282  {
283  data_->comp_to_link_(from_id) = to_id;
284  }
285 
286  template <typename L>
287  void
288  object_links<L>::clear(unsigned id)
289  {
290  data_->comp_to_link_(id) = id;
291  }
292 
293  template <typename L>
294  void
295  object_links<L>::disable_linking(unsigned id)
296  {
297  data_->comp_to_link_(id) = 0;
298  }
299 
300  template <typename L>
302  object_links<L>::comp_to_link() const
303  {
304  return data_->comp_to_link_;
305  }
306 
307  template <typename L>
308  void
309  object_links<L>::init()
310  {
311  for (unsigned i = 0; i < nelements(); ++i)
312  if (data_->components_(i).tag() == component::Ignored)
313  disable_linking(i);
314  else
315  clear(i);
316  }
317 
318  template <typename L>
319  inline
320  object_links<L>
322  {
323  object_links<L> output;
324  output.data_ = new data_t();
325 
326  *(output.data_.ptr_) = *(data_.ptr_);
327  return output;
328  }
329 
330 
331  template <typename L>
332  std::ostream&
333  operator<<(std::ostream& ostr, const object_links<L>& links)
334  {
335  ostr << "object_links[";
336 
337  for_all_links(l, links)
338  ostr << l << "->" << links.comp_to_link()[l] << ", ";
339 
340  ostr << "]";
341 
342  return ostr;
343  }
344 
345 
346 # endif // ! MLN_INCLUDE_ONLY
347 
348 
349 } // end of namespace scribo
350 
351 
352 #endif // ! SCRIBO_CORE_OBJECT_LINKS_HH