• 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     // Forward declaration
00042   namespace util { template <typename Tag, typename V> class object_id; }
00043 
00044   namespace convert
00045   {
00046 
00047     namespace over_load
00048     {
00049 
00050       // object_id<Tag,V> -> V.
00051       template <typename Tag, typename V>
00052       void from_to_(const util::object_id<Tag,V>& from, V& to_);
00053 
00054     } // end of namespace mln::convert::over_load
00055 
00056   } // end of namespace mln::convert
00057 
00058 
00059   namespace util
00060   {
00061 
00065     template <typename Tag, typename V>
00066     class object_id : public value::Integer< object_id<Tag, V> >
00067     {
00068       public:
00070         typedef V value_t;
00071         typedef unsigned equiv;
00072         typedef V enc;
00073 
00076         object_id();
00077 
00078         template <typename V2>
00079           object_id(const V2& id);
00080 
00081         template <typename Tag2, typename V2>
00082           object_id(const object_id<Tag2,V2>& other);
00084 
00085         template <typename V2>
00086           object_id<Tag,V>& operator=(const V2& e);
00087 
00088         const V& value() const;
00089         V& value();
00090 
00091         operator unsigned() const;
00092 
00093         bool is_valid() const;
00094         void invalidate();
00095 
00096         unsigned to_equiv() const;
00097 
00098       protected:
00099         V id_;
00100     };
00101 
00102 
00103     template <typename Tag, typename V>
00104     bool
00105     operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
00106 
00107     template <typename Tag, typename V, typename V2>
00108     bool
00109     operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs);
00110 
00111   } // end of namespace mln::util
00112 
00113 # ifndef MLN_INCLUDE_ONLY
00114 
00115   namespace util
00116   {
00117 
00118     template <typename Tag, typename V>
00119     inline
00120     object_id<Tag,V>::object_id()
00121     : id_(mln_max(V))
00122     {
00123     }
00124 
00125     template <typename Tag, typename V>
00126     template <typename V2>
00127     inline
00128     object_id<Tag,V>::object_id(const V2& id)
00129       : id_(id)
00130     {
00131       mlc_converts_to(V2,V)::check();
00132     }
00133 
00134     template <typename Tag, typename V>
00135     template <typename Tag2, typename V2>
00136     inline
00137     object_id<Tag,V>::object_id(const object_id<Tag2,V2>& id)
00138     {
00139       typedef object_id<Tag2,V2> id_t;
00140       mlc_abort(id_t)::check();
00141     }
00142 
00143     template <typename Tag, typename V>
00144     template <typename V2>
00145     inline
00146     object_id<Tag,V>&
00147     object_id<Tag,V>::operator=(const V2& v)
00148     {
00149       mlc_converts_to(V2,V)::check();
00150 
00151       id_ = v;
00152       return *this;
00153     }
00154 
00155     template <typename Tag, typename V>
00156     inline
00157     V&
00158     object_id<Tag,V>::value()
00159     {
00160       return id_;
00161     }
00162 
00163     template <typename Tag, typename V>
00164     inline
00165     const V&
00166     object_id<Tag,V>::value() const
00167     {
00168       return id_;
00169     }
00170 
00171     template <typename Tag, typename V>
00172     inline
00173     object_id<Tag,V>::operator unsigned() const
00174     {
00175       return id_;
00176     }
00177 
00178 
00179     template <typename Tag, typename V>
00180     inline
00181     bool
00182     object_id<Tag,V>::is_valid() const
00183     {
00184       return id_ != mln_max(V);
00185     }
00186 
00187     template <typename Tag, typename V>
00188     inline
00189     void
00190     object_id<Tag,V>::invalidate()
00191     {
00192       id_ = mln_max(V);
00193     }
00194 
00195     template <typename Tag, typename V>
00196     inline
00197     unsigned
00198     object_id<Tag,V>::to_equiv() const
00199     {
00200       return id_;
00201     }
00202 
00203 
00204 
00205     template <typename Tag, typename V, typename V2>
00206     inline
00207     bool
00208     operator==(const object_id<Tag,V>& lhs, const Value<V2>& rhs)
00209     {
00210       return lhs.value() == exact(rhs).to_equiv();
00211     }
00212 
00213     template <typename Tag, typename V>
00214     inline
00215     bool
00216     operator==(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
00217     {
00218       return lhs.value() == rhs.value();
00219     }
00220 
00221     template <typename Tag, typename V>
00222     inline
00223     bool
00224     operator<(const object_id<Tag,V>& lhs, const object_id<Tag,V>& rhs)
00225     {
00226       return lhs.value() < rhs.value();
00227     }
00228 
00229   } // end of namespace mln::util
00230 
00231   namespace convert
00232   {
00233 
00234     namespace over_load
00235     {
00236 
00237       // object_id<Tag,V> -> V.
00238       template <typename Tag, typename V>
00239       void from_to_(const util::object_id<Tag,V>& from, V& to_)
00240       {
00241         to_ = from.value();
00242       }
00243 
00244     } // end of namespace mln::convert::over_load
00245 
00246   } // end of namespace mln::convert
00247 
00248 # endif // ! MLN_INCLUDE_ONLY
00249 
00250 } // end of namespace mln
00251 
00252 #endif // ! MLN_UTIL_OBJECT_ID_HH

Generated on Tue Oct 4 2011 15:24:07 for Milena (Olena) by  doxygen 1.7.1