bin.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef NTG_ENUM_BIN_HH
00029 # define NTG_ENUM_BIN_HH
00030 
00031 /*
00032   Binary type. Possible values are 0 and 1 (not true and false).
00033 */
00034 
00035 # include <ntg/basics.hh>
00036 # include <ntg/enum/enum_value.hh>
00037 # include <ntg/core/internal/global_ops.hh>
00038 
00039 # include <mlc/contract.hh>
00040 
00041 # include <string>
00042 
00043 namespace ntg {
00044 
00045   namespace internal {
00046 
00047     /*----------------.
00048     | typetraits<bin> |
00049     `----------------*/
00050 
00051     template <>
00052     struct typetraits<bin> : typetraits<enum_value<bin> >
00053     {
00054       typedef binary                    abstract_type;
00055       typedef bin                       self;
00056       typedef self                      ntg_type;
00057 
00058       ntg_build_value_type(enum_value<E>);
00059 
00060       typedef optraits<self>            optraits_type;
00061 
00062       typedef self                      base_type;
00063       typedef bool                      storage_type;
00064       typedef bin                       signed_type;
00065       typedef bin                       unsigned_type;
00066       typedef bin                       cumul_type;
00067       typedef bin                       largest_type;
00068       typedef bin                       signed_largest_type;
00069       typedef bin                       signed_cumul_type;
00070       typedef bin                       unsigned_largest_type;
00071       typedef bin                       unsigned_cumul_type;
00072       typedef unsigned int              integer_type;
00073 
00074       // Particular properties
00075       enum { size = 1 };
00076     };
00077 
00078   } // end of internal.
00079 
00080   /*----.
00081   | bin |
00082   `----*/
00083 
00084   class bin : public enum_value<bin>
00085   {
00086   public:
00087     bin ()
00088     { this->val_ = 0; }
00089 
00090     // FIXME: create a template constructor and check into it if T
00091     // is a real or whatever ?
00092 
00093     bin (unsigned char val)
00094     {
00095       ntg_assert(val < 2);
00096       this->val_ = val;
00097     }
00098 
00099     bin&
00100     operator=(unsigned char val)
00101     {
00102       ntg_assert(val < 2);
00103       this->val_ = val;
00104       return *this;
00105     }
00106 
00107     template <class T>
00108     bin (const real_value<T>& val)
00109     {
00110       ntg_assert(val < 2);
00111       this->val_ = val.val();
00112     }
00113     template <class T>
00114     bin&
00115     operator=(const real_value<T>& val)
00116     {
00117       ntg_assert(val < 2);
00118       this->val_ = val.val();
00119       return *this;
00120     }
00121 
00122     operator unsigned char() const { return this->val_; }
00123   };
00124 
00125   inline std::ostream&
00126   operator<<(std::ostream& stream, const bin& rhs)
00127   {
00128     stream << (unsigned int) rhs.val();
00129     return stream;
00130   }
00131 
00132   namespace internal {
00133 
00134     /*--------------.
00135     | optraits<bin> |
00136     `--------------*/
00137 
00138     template <>
00139     struct optraits<bin> : public optraits<enum_value<bin> >
00140     {
00141     private:
00142       typedef typetraits<bin>::storage_type storage_type_;
00143 
00144     public:
00145       static storage_type_ zero() { return 0; }
00146       static storage_type_ unit() { return 1; }
00147       static storage_type_ min()  { return 0; }
00148       static storage_type_ max()  { return 1; }
00149       static storage_type_ inf()  { return min(); }
00150       static storage_type_ sup()  { return max(); }
00151       static unsigned max_print_width () { return 1U; }
00152 
00153       // logical assignement operators
00154 
00155       static bin&
00156       logical_or_equal(bin& lhs, const bin& rhs)
00157       {
00158         lhs = lhs.val() | rhs.val();
00159         return lhs;
00160       }
00161 
00162       static bin&
00163       logical_and_equal(bin& lhs, const bin& rhs)
00164       {
00165         lhs = lhs.val() & rhs.val();
00166         return lhs;
00167       }
00168 
00169       static bin&
00170       logical_xor_equal(bin& lhs, const bin& rhs)
00171       {
00172         lhs = lhs.val() ^ rhs.val();
00173         return lhs;
00174       }
00175 
00176       // logical binary ops
00177 
00178       static bin
00179       logical_or(const bin& lhs, const bin& rhs)
00180       {
00181         bin tmp(lhs);
00182         tmp |= rhs;
00183         return tmp;
00184       }
00185 
00186       static bin
00187       logical_and(const bin& lhs, const bin& rhs)
00188       {
00189         bin tmp(lhs);
00190         tmp &= rhs;
00191         return tmp;
00192       }
00193 
00194       static bin
00195       logical_xor(const bin& lhs, const bin& rhs)
00196       {
00197         bin tmp(lhs);
00198         tmp ^= rhs;
00199         return tmp;
00200       }
00201 
00202       // comparisons
00203 
00204       static bool
00205       cmp_lt(const bin& lhs, const bin& rhs)
00206       {
00207         return lhs.val() < rhs.val();
00208       }
00209 
00210       static bool
00211       cmp_eq(const bin& lhs, const bin& rhs)
00212       {
00213         return lhs.val() == rhs.val();
00214       }
00215 
00216       static std::string name() { return "bin"; }
00217     };
00218 
00219     /*-----------------.
00220     | operators traits |
00221     `-----------------*/
00222 
00223     //  Logical operators
00224 
00225     template <class T>
00226     struct operator_traits<operator_logical, bin, T>
00227     {
00228       enum { commutative = true };
00229       typedef bin ret;
00230       typedef bin impl;
00231     };
00232 
00233     //  Comparison operators
00234 
00235     template <>
00236     struct operator_traits<operator_cmp, bin, bin>
00237     {
00238       enum { commutative = true };
00239       typedef bin ret;
00240       typedef bin impl;
00241     };
00242 
00243     //  Max
00244 
00245     template <>
00246     struct operator_traits<operator_max, bin, bin>
00247     {
00248       enum { commutative = true };
00249       typedef bin ret;
00250       typedef bin impl;
00251     };
00252 
00253     //  Min
00254 
00255     template <>
00256     struct operator_traits<operator_min, bin, bin>
00257     {
00258       enum { commutative = true };
00259       typedef bin ret;
00260       typedef bin impl;
00261     };
00262 
00263   } // end of internal.
00264 
00265 } // end of ntg.
00266 
00267 #endif // !NTG_ENUM_BIN_HH

Generated on Thu Apr 15 20:13:06 2004 for Olena by doxygen 1.3.6-20040222