Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
object_id.hh
1 // Copyright (C) 2009, 2011 EPITA Research and Development Laboratory
2 // (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  // Forward declaration
43  namespace util { template <typename Tag, typename V> class object_id; }
44 
45  namespace convert
46  {
47 
48  namespace over_load
49  {
50 
51  // object_id<Tag,V> -> V.
52  template <typename Tag, typename V>
53  void from_to_(const util::object_id<Tag,V>& from, V& to_);
54 
55  } // end of namespace mln::convert::over_load
56 
57  } // end of namespace mln::convert
58 
59 
60  namespace util
61  {
62 
66  template <typename Tag, typename V>
67  class object_id : public value::Integer< object_id<Tag, V> >
68  {
69  public:
71  typedef V value_t;
72  typedef unsigned equiv;
73  typedef V enc;
74 
77  object_id();
78 
79  template <typename V2>
80  object_id(const V2& id);
81 
82  template <typename Tag2, typename V2>
83  object_id(const object_id<Tag2,V2>& other);
85 
86  template <typename V2>
87  object_id<Tag,V>& operator=(const V2& e);
88 
89  const V& value() const;
90  V& value();
91 
92  operator unsigned() const;
93 
94  bool is_valid() const;
95  void invalidate();
96 
97  unsigned to_equiv() const;
98 
99  protected:
100  V id_;
101  };
102 
103 
104  template <typename Tag, typename V>
105  bool
106  operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
107 
108  template <typename Tag, typename V, typename V2>
109  bool
110  operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
111 
112  } // end of namespace mln::util
113 
114 # ifndef MLN_INCLUDE_ONLY
115 
116  namespace util
117  {
118 
119  template <typename Tag, typename V>
120  inline
122  : id_(mln_max(V))
123  {
124  }
125 
126  template <typename Tag, typename V>
127  template <typename V2>
128  inline
129  object_id<Tag,V>::object_id(const V2& id)
130  : id_(id)
131  {
132  mlc_converts_to(V2,V)::check();
133  }
134 
135  template <typename Tag, typename V>
136  template <typename Tag2, typename V2>
137  inline
138  object_id<Tag,V>::object_id(const object_id<Tag2,V2>& id)
139  {
140  (void) id;
141  typedef object_id<Tag2,V2> id_t;
142  mlc_abort(id_t)::check();
143  }
144 
145  template <typename Tag, typename V>
146  template <typename V2>
147  inline
148  object_id<Tag,V>&
149  object_id<Tag,V>::operator=(const V2& v)
150  {
151  mlc_converts_to(V2,V)::check();
152 
153  id_ = v;
154  return *this;
155  }
156 
157  template <typename Tag, typename V>
158  inline
159  V&
160  object_id<Tag,V>::value()
161  {
162  return id_;
163  }
164 
165  template <typename Tag, typename V>
166  inline
167  const V&
168  object_id<Tag,V>::value() const
169  {
170  return id_;
171  }
172 
173  template <typename Tag, typename V>
174  inline
175  object_id<Tag,V>::operator unsigned() const
176  {
177  return id_;
178  }
179 
180 
181  template <typename Tag, typename V>
182  inline
183  bool
184  object_id<Tag,V>::is_valid() const
185  {
186  return id_ != mln_max(V);
187  }
188 
189  template <typename Tag, typename V>
190  inline
191  void
192  object_id<Tag,V>::invalidate()
193  {
194  id_ = mln_max(V);
195  }
196 
197  template <typename Tag, typename V>
198  inline
199  unsigned
200  object_id<Tag,V>::to_equiv() const
201  {
202  return id_;
203  }
204 
205 
206 
207  template <typename Tag, typename V, typename V2>
208  inline
209  bool
210  operator==(const object_id<Tag,V>& lhs, const Value<V2>& rhs)
211  {
212  return lhs.value() == exact(rhs).to_equiv();
213  }
214 
215  template <typename Tag, typename V>
216  inline
217  bool
218  operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
219  {
220  return lhs.value() == rhs.value();
221  }
222 
223  template <typename Tag, typename V>
224  inline
225  bool
226  operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
227  {
228  return lhs.value() < rhs.value();
229  }
230 
231  } // end of namespace mln::util
232 
233  namespace convert
234  {
235 
236  namespace over_load
237  {
238 
239  // object_id<Tag,V> -> V.
240  template <typename Tag, typename V>
241  void from_to_(const util::object_id<Tag,V>& from, V& to_)
242  {
243  to_ = from.value();
244  }
245 
246  } // end of namespace mln::convert::over_load
247 
248  } // end of namespace mln::convert
249 
250 # endif // ! MLN_INCLUDE_ONLY
251 
252 } // end of namespace mln
253 
254 #endif // ! MLN_UTIL_OBJECT_ID_HH