From: g0dil Date: Fri, 10 Nov 2006 23:15:37 +0000 (+0000) Subject: add SafeBool X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=9248350bdd5a59799e603936d98241a1bb4591f9;p=senf.git add SafeBool git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@154 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Utils/SafeBool.cci b/Utils/SafeBool.cci new file mode 100644 index 0000000..4c49b5b --- /dev/null +++ b/Utils/SafeBool.cci @@ -0,0 +1,36 @@ +// $Id$ +// +// Copyright (C) 2006 + +// Definition of inline non-template functions + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +prefix_ void satcom::lib::SafeBoolBase::this_type_does_not_support_comparisons() + const +{} + +prefix_ satcom::lib::SafeBoolBase::SafeBoolBase() +{} + +prefix_ satcom::lib::SafeBoolBase::SafeBoolBase(const SafeBoolBase&) +{} + +prefix_ satcom::lib::SafeBoolBase& satcom::lib::SafeBoolBase::operator=(const SafeBoolBase&) +{ + return *this; +} + +prefix_ satcom::lib::SafeBoolBase::~SafeBoolBase() +{} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/Utils/SafeBool.cti b/Utils/SafeBool.cti new file mode 100644 index 0000000..603ae8a --- /dev/null +++ b/Utils/SafeBool.cti @@ -0,0 +1,51 @@ +// $Id$ +// +// Copyright (C) 2006 + +// Definition of inline template functions + +//#include "SafeBool.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template +prefix_ satcom::lib::SafeBool::operator bool_type() + const +{ + return (static_cast(this))->boolean_test() + ? &SafeBoolBase::this_type_does_not_support_comparisons : 0; +} + +template +prefix_ bool satcom::lib::SafeBool::operator!() + const +{ + return ! (static_cast(this))->boolean_test(); +} + +template +prefix_ satcom::lib::SafeBool::~SafeBool() +{} + +template +prefix_ void satcom::lib::operator==(const SafeBool& lhs, const SafeBool& rhs) +{ + lhs.this_type_does_not_support_comparisons(); +} + +template +prefix_ void satcom::lib::operator!=(const SafeBool& lhs, const SafeBool& rhs) +{ + lhs.this_type_does_not_support_comparisons(); +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/Utils/SafeBool.hh b/Utils/SafeBool.hh new file mode 100644 index 0000000..5bde302 --- /dev/null +++ b/Utils/SafeBool.hh @@ -0,0 +1,82 @@ +// $Id$ +// +// Copyright (C) 2006 + +#ifndef HH_SafeBool_ +#define HH_SafeBool_ 1 + +// Custom includes + +//#include "SafeBool.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + // This is a direct copy of a safe bool solution by Bjorn Karlsson + // from http://www.artima.com/cppsource/safebool.html + // + // Usage: + // class TestableWithVirtual + // : public safe_bool<> + // { + // protected: + // bool boolean_test() const + // { + // // Perform Boolean logic here + // } + // }; + // + // class TestableWithoutVirtual + // : public safe_bool + // { + // public: + // bool boolean_test() const + // { + // // Perform Boolean logic here + // } + // }; + + class SafeBoolBase + { + protected: + typedef void (SafeBoolBase::*bool_type)() const; + void this_type_does_not_support_comparisons() const; + + SafeBoolBase(); + SafeBoolBase(const SafeBoolBase&); + SafeBoolBase& operator=(const SafeBoolBase&); + ~SafeBoolBase(); + }; + + template + class SafeBool + : public SafeBoolBase + { + public: + operator bool_type() const; + bool operator !() const; + + protected: + ~SafeBool(); + }; + + template + void operator==(const SafeBool& lhs,const SafeBool& rhs); + + template + void operator!=(const SafeBool& lhs,const SafeBool& rhs); + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "SafeBool.cci" +//#include "SafeBool.ct" +#include "SafeBool.cti" +//#include "SafeBool.mpp" +#endif + + +// Local Variables: +// mode: c++ +// End: