proto-1.0 7: Adding meta-if and meta-switch in metalic

Index: ChangeLog from Damien Thivolle <damien@lrde.epita.fr> * ChangeLog: New. * mlc/types.hh: Remove already defined struct. * mlc/makefile.src: Add config/system.hh. * mlc/bool.hh: Add meta-if and meta-switch. bool.hh | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- makefile.src | 1 types.hh | 2 - 3 files changed, 89 insertions(+), 5 deletions(-) Index: ChangeLog Index: mlc/types.hh --- mlc/types.hh (revision 4) +++ mlc/types.hh (working copy) @@ -20,8 +20,6 @@ class unknown_type { private: unknown_type(); }; class no_type { private: no_type(); }; - class true_type { private: true_type(); }; - class false_type { private: false_type(); }; } // end of namespace mlc Index: mlc/makefile.src --- mlc/makefile.src (revision 4) +++ mlc/makefile.src (working copy) @@ -8,6 +8,7 @@ bool.hh \ box.hh \ cmp.hh \ + config/system.hh \ contract.hh \ tracked_ptr.hh \ traits.hh \ Index: mlc/bool.hh --- mlc/bool.hh (revision 4) +++ mlc/bool.hh (working copy) @@ -2,8 +2,8 @@ # define METALIC_BOOL_HH -namespace mlc -{ +namespace mlc +{ struct false_type; @@ -22,6 +22,92 @@ + /*----. + | if_ | + `----*/ + + template <bool Cond, class if_true_type, class if_false_type> + struct if_ { + typedef if_true_type ret; + }; + + template<class if_true_type, class if_false_type> + struct if_<false, if_true_type, if_false_type> + { + typedef if_false_type ret; + typedef false_type ensure_type; + }; + + /*--------. + | switch_ | + `--------*/ + + struct invalid {}; + + template<unsigned Cond, class Ret, class Cases = invalid> + struct case_ {}; + + template<unsigned Cond, class Cases, class Default = invalid> + struct switch_; + + template<unsigned Cond, unsigned Compare, class Ret, class Default> + struct switch_<Cond, case_<Compare, Ret>, Default > + { + typedef typename if_< (Cond == Compare), Ret, Default >::ret ret; + }; + + template<unsigned Cond, + unsigned Compare, + class Ret, + class Cases, + class Default> + struct switch_<Cond, case_<Compare, Ret, Cases>, Default > + { + typedef typename + if_< (Cond == Compare), + Ret, + typename switch_<Cond, Cases, Default>::ret>::ret ret; + }; + + template<bool Cond, class Ret, class Cases = invalid> + struct bool_case_ {}; + + template<class Cases, class Default = invalid> + struct bool_switch_; + + template<bool Cond, class Ret, class Default> + struct bool_switch_<bool_case_<Cond, Ret>, Default > + { + typedef typename if_< Cond, Ret, Default >::ret ret; + }; + + template<bool Cond,class Ret, class Cases, class Default> + struct bool_switch_<bool_case_<Cond, Ret, Cases>, Default > + { + typedef typename + if_< Cond, + Ret, + typename bool_switch_<Cases, Default>::ret >::ret ret; + }; + + /*-----. + | misc | + `-----*/ + + template<bool> struct is_true; + template<> struct is_true<true> + { + static void ensure() {}; + typedef true_type ensure_type; + }; + + template<bool> struct is_false; + template<> struct is_false<false> { static void ensure() {}; }; + + // FIXME: find a good name for this struct. + + // base class for meta-types returning Boolean values + template<bool> struct returns_bool_; template<> @@ -38,7 +124,6 @@ static const bool ret = false; }; - } // end of namespace mlc
participants (1)
-
Damien Thivolle