00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef NTG_ENUM_BIN_HH
00029 # define NTG_ENUM_BIN_HH
00030
00031
00032
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
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
00075 enum { size = 1 };
00076 };
00077
00078 }
00079
00080
00081
00082
00083
00084 class bin : public enum_value<bin>
00085 {
00086 public:
00087 bin ()
00088 { this->val_ = 0; }
00089
00090
00091
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
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
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
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
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
00221
00222
00223
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
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
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
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 }
00264
00265 }
00266
00267 #endif // !NTG_ENUM_BIN_HH