$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
object_id.hh
1 // Copyright (C) 2009, 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 MLN_UTIL_OBJECT_ID_HH
28 # define MLN_UTIL_OBJECT_ID_HH
29 
33 
34 
35 # include <mln/core/concept/object.hh>
36 # include <mln/value/concept/integer.hh>
37 # include <mln/metal/abort.hh>
38 
39 namespace mln
40 {
41 
42  namespace util
43  {
44 
48  template <typename Tag, typename V>
49  class object_id : public value::Integer< object_id<Tag, V> >
50  {
51  public:
53  typedef V value_t;
54  typedef unsigned equiv;
55  typedef V enc;
56 
59 
60  object_id();
61 
62  template <typename V2>
63  object_id(const V2& id);
64 
65  template <typename Tag2, typename V2>
66  object_id(const object_id<Tag2,V2>& other);
67 
69 
70  template <typename V2>
71  object_id<Tag,V>& operator=(const V2& e);
72 
73  const V& value() const;
74  V& value();
75 
76  operator unsigned() const;
77 
78  bool is_valid() const;
79  void invalidate();
80 
81  unsigned to_equiv() const;
82 
83  protected:
84  V id_;
85  };
86 
87 
88  template <typename Tag, typename V>
89  bool
90  operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
91 
92  template <typename Tag, typename V, typename V2>
93  bool
94  operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
95 
97  template <typename Tag, typename V>
98  void from_to_(const util::object_id<Tag,V>& from, V& to_);
99 
100  } // end of namespace mln::util
101 
102 # ifndef MLN_INCLUDE_ONLY
103 
104  namespace util
105  {
106 
107  template <typename Tag, typename V>
108  inline
110  : id_(mln_max(V))
111  {
112  }
113 
114  template <typename Tag, typename V>
115  template <typename V2>
116  inline
117  object_id<Tag,V>::object_id(const V2& id)
118  : id_(id)
119  {
120  mlc_converts_to(V2,V)::check();
121  }
122 
123  template <typename Tag, typename V>
124  template <typename Tag2, typename V2>
125  inline
126  object_id<Tag,V>::object_id(const object_id<Tag2,V2>& id)
127  {
128  (void) id;
129  typedef object_id<Tag2,V2> id_t;
130  mlc_abort(id_t)::check();
131  }
132 
133  template <typename Tag, typename V>
134  template <typename V2>
135  inline
136  object_id<Tag,V>&
137  object_id<Tag,V>::operator=(const V2& v)
138  {
139  mlc_converts_to(V2,V)::check();
140 
141  id_ = v;
142  return *this;
143  }
144 
145  template <typename Tag, typename V>
146  inline
147  V&
149  {
150  return id_;
151  }
152 
153  template <typename Tag, typename V>
154  inline
155  const V&
157  {
158  return id_;
159  }
160 
161  template <typename Tag, typename V>
162  inline
163  object_id<Tag,V>::operator unsigned() const
164  {
165  return id_;
166  }
167 
168 
169  template <typename Tag, typename V>
170  inline
171  bool
172  object_id<Tag,V>::is_valid() const
173  {
174  return id_ != mln_max(V);
175  }
176 
177  template <typename Tag, typename V>
178  inline
179  void
180  object_id<Tag,V>::invalidate()
181  {
182  id_ = mln_max(V);
183  }
184 
185  template <typename Tag, typename V>
186  inline
187  unsigned
188  object_id<Tag,V>::to_equiv() const
189  {
190  return id_;
191  }
192 
193 
194 
195  template <typename Tag, typename V, typename V2>
196  inline
197  bool
198  operator==(const object_id<Tag,V>& lhs, const Value<V2>& rhs)
199  {
200  return lhs.value() == static_cast<V>(exact(rhs).to_equiv());
201  }
202 
203  template <typename Tag, typename V>
204  inline
205  bool
206  operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
207  {
208  return lhs.value() == rhs.value();
209  }
210 
211  template <typename Tag, typename V>
212  inline
213  bool
214  operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
215  {
216  return lhs.value() < rhs.value();
217  }
218 
219 
220  // Conversions
221 
222  template <typename Tag, typename V>
223  void from_to_(const util::object_id<Tag,V>& from, V& to_)
224  {
225  to_ = from.value();
226  }
227 
228  } // end of namespace mln::util
229 
230 # endif // ! MLN_INCLUDE_ONLY
231 
232 } // end of namespace mln
233 
234 #endif // ! MLN_UTIL_OBJECT_ID_HH