Phoenix
Object-oriented orthogonally persistent operating system
numeric.h
Go to the documentation of this file.
00001 /*
00002  * /phoenix/include/triton/numeric.h
00003  *
00004  * This file is a part of Phoenix operating system.
00005  * Copyright (c) 2011-2012, Artyom Lebedev <artyom.lebedev@gmail.com>
00006  * All rights reserved.
00007  * See COPYING file for copyright details.
00008  */
00009 
00014 #ifndef NUMERIC_H_
00015 #define NUMERIC_H_
00016 
00017 namespace triton {
00018 
00019 namespace triton_internal {
00020 
00024 template <typename T>
00025 class Numeric: public Object {
00026 protected:
00027     union Value {
00028         enable_if<is_numeric<T>(), T> value;
00029         hash_t hash;
00030     } _v;
00031 public:
00032 
00033     inline
00034     Numeric(T value)
00035     {
00036         _v.hash = 0;
00037         _v.value = value;
00038     }
00039 
00040     template <typename Targ>
00041     inline
00042     Numeric(const Numeric<Targ> &value)
00043     {
00044         _v.hash = 0;
00045         _v.value = value._v.value;
00046     }
00047 
00048     virtual hash_t
00049     __hash__() const
00050     {
00051         return _v.hash;
00052     }
00053 
00054     inline
00055     operator T()
00056     {
00057         return _v.value;
00058     }
00059 
00060     template <typename Targ>
00061     inline Numeric<T> &
00062     operator =(const Numeric<Targ> &value)
00063     {
00064         _v.hash = 0;
00065         _v.value = value._v.value;
00066     }
00067 };
00068 
00069 template <typename T>
00070 class NumericInt: public Numeric<enable_if<is_integral<T>(), T>> {
00071 public:
00072 
00073     inline NumericInt(T value) : Numeric<T>(value) {}
00074 
00075     template <typename Targ>
00076     inline NumericInt<T> &
00077     operator &=(Targ value)
00078     {
00079         this->_v.value &= value;
00080         return *this;
00081     }
00082 
00083     template <typename Targ>
00084     inline NumericInt<T> &
00085     operator |=(Targ value)
00086     {
00087         this->_v.value |= value;
00088         return *this;
00089     }
00090 
00091     template <typename Targ>
00092     inline NumericInt<T> &
00093     operator ^=(Targ value)
00094     {
00095         this->_v.value ^= value;
00096         return *this;
00097     }
00098 };
00099 
00100 template <typename T>
00101 class NumericFloat: public Numeric<enable_if<is_float<T>(), T>> {
00102 public:
00103 
00104     inline NumericFloat(T value) : Numeric<T>(value) {}
00105 
00106 };
00107 
00108 } /* namespace triton_internal */
00109 
00111 typedef triton_internal::NumericInt<char> Char;
00113 typedef triton_internal::NumericInt<unsigned char> UChar;
00115 typedef triton_internal::NumericInt<int> Int;
00117 typedef triton_internal::NumericInt<unsigned int> UInt;
00119 typedef triton_internal::NumericInt<long> Long;
00121 typedef triton_internal::NumericInt<unsigned long> ULong;
00123 typedef triton_internal::NumericInt<long> LongLong;
00125 typedef triton_internal::NumericInt<unsigned long> ULongLong;
00126 
00128 typedef triton_internal::NumericFloat<float> Float;
00130 typedef triton_internal::NumericFloat<double> Double;
00132 typedef triton_internal::NumericFloat<long double> LongDouble;
00133 
00134 template <typename T>
00135 inline Object::hash_t
00136 hash(T value, enable_if<is_integral<T>(), T> dummy = 0)
00137 {
00138     return triton_internal::NumericInt<T>(value).__hash__();
00139 }
00140 
00141 template <typename T>
00142 inline Object::hash_t
00143 hash(T value, enable_if<is_float<T>(), T> dummy = 0)
00144 {
00145     return triton_internal::NumericFloat<T>(value).__hash__();
00146 }
00147 
00148 } /* namespace triton */
00149 
00150 #endif /* NUMERIC_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines