add SafeBool
g0dil [Fri, 10 Nov 2006 23:15:37 +0000 (23:15 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@154 270642c3-0616-0410-b53a-bc976706d245

Utils/SafeBool.cci [new file with mode: 0644]
Utils/SafeBool.cti [new file with mode: 0644]
Utils/SafeBool.hh [new file with mode: 0644]

diff --git a/Utils/SafeBool.cci b/Utils/SafeBool.cci
new file mode 100644 (file)
index 0000000..4c49b5b
--- /dev/null
@@ -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_
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Utils/SafeBool.cti b/Utils/SafeBool.cti
new file mode 100644 (file)
index 0000000..603ae8a
--- /dev/null
@@ -0,0 +1,51 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Definition of inline template functions
+
+//#include "SafeBool.ih"
+
+// Custom includes
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+template <typename T>
+prefix_ satcom::lib::SafeBool<T>::operator bool_type()
+    const
+{
+    return (static_cast<const T*>(this))->boolean_test()
+       ? &SafeBoolBase::this_type_does_not_support_comparisons : 0;
+}
+
+template <typename T>
+prefix_ bool satcom::lib::SafeBool<T>::operator!()
+    const
+{
+    return ! (static_cast<const T*>(this))->boolean_test();
+}
+
+template <typename T>
+prefix_ satcom::lib::SafeBool<T>::~SafeBool()
+{}
+
+template <typename T, typename U>
+prefix_ void satcom::lib::operator==(const SafeBool<T>& lhs, const SafeBool<U>& rhs)
+{
+    lhs.this_type_does_not_support_comparisons();     
+}
+
+template <typename T, typename U>
+prefix_ void satcom::lib::operator!=(const SafeBool<T>& lhs, const SafeBool<U>& rhs)
+{
+    lhs.this_type_does_not_support_comparisons();
+}
+
+///////////////////////////////cti.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Utils/SafeBool.hh b/Utils/SafeBool.hh
new file mode 100644 (file)
index 0000000..5bde302
--- /dev/null
@@ -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 <TestableWithoutVirtual> 
+    //    {
+    //    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 <typename T=void> 
+    class SafeBool 
+       : public SafeBoolBase 
+    {
+    public:
+       operator bool_type() const;
+       bool operator !() const;
+
+    protected:
+       ~SafeBool();
+    };
+
+    template <typename T, typename U> 
+    void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs);
+
+    template <typename T,typename U> 
+    void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs);
+
+}}
+
+///////////////////////////////hh.e////////////////////////////////////////
+#include "SafeBool.cci"
+//#include "SafeBool.ct"
+#include "SafeBool.cti"
+//#include "SafeBool.mpp"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// End: