ivl 679
|
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>. 00002 A C++ template library extending syntax towards mathematical notation. 00003 00004 Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr> 00005 Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr> 00006 00007 ivl is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License 00009 version 3 as published by the Free Software Foundation. 00010 00011 Alternatively, you can redistribute it and/or modify it under the terms 00012 of the GNU General Public License version 2 as published by the Free 00013 Software Foundation. 00014 00015 ivl is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00018 See the GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 and a copy of the GNU Lesser General Public License along 00022 with ivl. If not, see <http://www.gnu.org/licenses/>. */ 00023 00024 #ifndef IVL_CORE_DETAILS_TYPES_LOGIC_HPP 00025 #define IVL_CORE_DETAILS_TYPES_LOGIC_HPP 00026 00027 namespace ivl { 00028 namespace types { 00029 00030 // --------------------------------------------------- 00031 00032 struct t_true 00033 { 00034 typedef t_true type; 00035 enum { value = true }; 00036 operator bool() { return true; } 00037 }; 00038 00039 struct t_false 00040 { 00041 typedef t_false type; 00042 enum { value = false }; 00043 operator bool() { return false; } 00044 }; 00045 00046 template <class T> 00047 struct t_detect { }; 00048 00049 template <> 00050 struct t_detect<t_true> 00051 { 00052 private: 00053 char t_true_size[20]; // used to identify true/false with sizeof 00054 }; 00055 00056 template <> 00057 struct t_detect<t_false> 00058 { 00059 private: 00060 char t_false_size[10]; // used to identify true/false with sizeof 00061 }; 00062 00063 template <bool VAL> 00064 struct t_expr : public t_false { }; 00065 00066 template <> 00067 struct t_expr<true> : public t_true { }; 00068 00069 template <class VAL> 00070 struct t_not : public t_not<typename VAL::type> { }; 00071 00072 template <> 00073 struct t_not<t_true> : public t_false { }; 00074 00075 template <> 00076 struct t_not<t_false> : public t_true { }; 00077 00078 template <class COND, class THEN, class ELSE> 00079 struct t_if : public t_if <typename COND::type, THEN, ELSE> { }; 00080 00081 template <class THEN, class ELSE> 00082 struct t_if <t_true, THEN, ELSE> { typedef THEN type; }; 00083 00084 template <class THEN, class ELSE> 00085 struct t_if <t_false, THEN, ELSE> { typedef ELSE type; }; 00086 00087 template <class A, class B> 00088 struct t_eq : public t_false { }; 00089 00090 template <class A> 00091 struct t_eq <A, A> : public t_true { }; 00092 00093 template <class A, class B> 00094 struct t_neq : public t_not<typename t_eq<A, B>::type> { }; 00095 00096 // --------------------------------------------------- 00097 00098 template <class X, class Y> 00099 struct t_and : public t_and<typename X::type, typename Y::type> { }; 00100 00101 template <> 00102 struct t_and<t_true, t_true> : public t_true { }; 00103 00104 template <> 00105 struct t_and<t_true, t_false> : public t_false { }; 00106 00107 template <> 00108 struct t_and<t_false, t_true> : public t_false { }; 00109 00110 template <> 00111 struct t_and<t_false, t_false> : public t_false { }; 00112 00113 template <class X, class Y> 00114 struct t_or : public t_or<typename X::type, typename Y::type> { }; 00115 00116 template <> 00117 struct t_or<t_true, t_true> : public t_true { }; 00118 00119 template <> 00120 struct t_or<t_true, t_false> : public t_true { }; 00121 00122 template <> 00123 struct t_or<t_false, t_true> : public t_true { }; 00124 00125 template <> 00126 struct t_or<t_false, t_false> : public t_false { }; 00127 00128 template <class X, class Y> 00129 struct t_xor : public t_xor<typename X::type, typename Y::type> { }; 00130 00131 template <> 00132 struct t_xor<t_true, t_true> : public t_false { }; 00133 00134 template <> 00135 struct t_xor<t_true, t_false> : public t_true { }; 00136 00137 template <> 00138 struct t_xor<t_false, t_true> : public t_true { }; 00139 00140 template <> 00141 struct t_xor<t_false, t_false> : public t_false { }; 00142 00143 template <class X, class Y, class Z> 00144 struct t_and_3 : public t_and<t_and<X, Y>, Z> { }; 00145 00146 template <class X, class Y, class Z> 00147 struct t_or_3 : public t_or<t_or<X, Y>, Z> { }; 00148 00149 template <class X, class Y, class Z> 00150 struct t_xor_3 : public t_xor<t_xor<X, Y>, Z> { }; 00151 00152 // --------------------------------------------------- 00153 00154 template <class A> 00155 struct is_const : public t_false { }; 00156 00157 template <class A> 00158 struct is_const <const A> : public t_true { }; 00159 00160 template <class A> 00161 struct is_const <const A&> : public t_true { }; 00162 00163 template <class A> 00164 struct is_const <const A*> : public t_true { }; 00165 00166 } /* namespace types */ 00167 } /* namespace ivl */ 00168 00169 00170 #endif // IVL_CORE_DETAILS_TYPES_LOGIC_HPP