• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

object_id.hh

00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_UTIL_OBJECT_ID_HH
00027 # define MLN_UTIL_OBJECT_ID_HH
00028 
00032 
00033 
00034 # include <mln/core/concept/object.hh>
00035 # include <mln/value/concept/integer.hh>
00036 # include <mln/metal/abort.hh>
00037 
00038 namespace mln
00039 {
00040 
00041   namespace util
00042   {
00043 
00047     template <typename Tag, typename V>
00048     class object_id : public value::Integer< object_id<Tag, V> >
00049     {
00050       public:
00052         typedef V value_t;
00053         typedef unsigned equiv;
00054         typedef V enc;
00055 
00058         object_id();
00059 
00060         template <typename V2>
00061           object_id(const V2& id);
00062 
00063         template <typename Tag2, typename V2>
00064           object_id(const object_id<Tag2,V2>& other);
00066 
00067         template <typename V2>
00068           object_id<Tag,V>& operator=(const V2& e);
00069 
00070         const V& value() const;
00071         V& value();
00072 
00073         operator unsigned() const;
00074 
00075         bool is_valid() const;
00076         void invalidate();
00077 
00078         unsigned to_equiv() const;
00079 
00080       protected:
00081         V id_;
00082     };
00083 
00084 
00085     template <typename Tag, typename V>
00086     bool
00087     operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
00088 
00089     template <typename Tag, typename V, typename V2>
00090     bool
00091     operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
00092 
00093 
00094 # ifndef MLN_INCLUDE_ONLY
00095 
00096 
00097     template <typename Tag, typename V>
00098     inline
00099     object_id<Tag,V>::object_id()
00100     : id_(mln_max(V))
00101     {
00102     }
00103 
00104     template <typename Tag, typename V>
00105     template <typename V2>
00106     inline
00107     object_id<Tag,V>::object_id(const V2& id)
00108       : id_(id)
00109     {
00110       mlc_converts_to(V2,V)::check();
00111     }
00112 
00113     template <typename Tag, typename V>
00114     template <typename Tag2, typename V2>
00115     inline
00116     object_id<Tag,V>::object_id(const object_id<Tag2,V2>& id)
00117     {
00118       typedef object_id<Tag2,V2> id_t;
00119       mlc_abort(id_t)::check();
00120     }
00121 
00122     template <typename Tag, typename V>
00123     template <typename V2>
00124     inline
00125     object_id<Tag,V>&
00126     object_id<Tag,V>::operator=(const V2& v)
00127     {
00128       mlc_converts_to(V2,V)::check();
00129 
00130       id_ = v;
00131       return *this;
00132     }
00133 
00134     template <typename Tag, typename V>
00135     inline
00136     V&
00137     object_id<Tag,V>::value()
00138     {
00139       return id_;
00140     }
00141 
00142     template <typename Tag, typename V>
00143     inline
00144     const V&
00145     object_id<Tag,V>::value() const
00146     {
00147       return id_;
00148     }
00149 
00150     template <typename Tag, typename V>
00151     inline
00152     object_id<Tag,V>::operator unsigned() const
00153     {
00154       return id_;
00155     }
00156 
00157 
00158     template <typename Tag, typename V>
00159     inline
00160     bool
00161     object_id<Tag,V>::is_valid() const
00162     {
00163       return id_ != mln_max(V);
00164     }
00165 
00166     template <typename Tag, typename V>
00167     inline
00168     void
00169     object_id<Tag,V>::invalidate()
00170     {
00171       id_ = mln_max(V);
00172     }
00173 
00174     template <typename Tag, typename V>
00175     inline
00176     unsigned
00177     object_id<Tag,V>::to_equiv() const
00178     {
00179       return id_;
00180     }
00181 
00182 
00183 
00184     template <typename Tag, typename V, typename V2>
00185     inline
00186     bool
00187     operator==(const object_id<Tag,V>& lhs, const Value<V2>& rhs)
00188     {
00189       return lhs.value() == exact(rhs).to_equiv();
00190     }
00191 
00192     template <typename Tag, typename V>
00193     inline
00194     bool
00195     operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
00196     {
00197       return lhs.value() == rhs.value();
00198     }
00199 
00200     template <typename Tag, typename V>
00201     inline
00202     bool
00203     operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
00204     {
00205       return lhs.value() < rhs.value();
00206     }
00207 
00208 
00209 # endif // ! MLN_INCLUDE_ONLY
00210 
00211   } // end of namespace mln::util
00212 
00213 } // end of namespace mln
00214 
00215 #endif // ! MLN_UTIL_OBJECT_ID_HH

Generated on Thu Sep 8 2011 18:32:09 for Milena (Olena) by  doxygen 1.7.1