- Added class base::SafeBool. Resusable implementaion of a save 'operator bool'.
authorMichael Andres <ma@suse.de>
Sat, 18 Feb 2006 23:51:05 +0000 (23:51 +0000)
committerMichael Andres <ma@suse.de>
Sat, 18 Feb 2006 23:51:05 +0000 (23:51 +0000)
  Using CRTP, derived classes must provide 'bool boolTest() const' only. See doc.

zypp/base/Makefile.am
zypp/base/SafeBool.h [new file with mode: 0644]

index 42db4bb..51ca6f0 100644 (file)
@@ -25,6 +25,7 @@ baseinclude_HEADERS = \
        NonCopyable.h   \
        ProvideNumericId.h      \
        ReferenceCounted.h      \
+       SafeBool.h      \
        PtrTypes.h      \
        String.h        \
        Unit.h          \
diff --git a/zypp/base/SafeBool.h b/zypp/base/SafeBool.h
new file mode 100644 (file)
index 0000000..3d6a77b
--- /dev/null
@@ -0,0 +1,79 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/base/SafeBool.h
+ *
+*/
+#ifndef ZYPP_BASE_SAFEBOOL_H
+#define ZYPP_BASE_SAFEBOOL_H
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace base
+  { /////////////////////////////////////////////////////////////////
+
+    namespace safebool_detail
+    {
+      class SafeBoolBase
+      {
+      protected:
+        typedef void (SafeBoolBase::*bool_type)() const;
+        void theTrueBoolType() const {}
+
+        SafeBoolBase() {}
+        SafeBoolBase( const SafeBoolBase & ) {}
+        ~SafeBoolBase() {}
+        SafeBoolBase & operator=( const SafeBoolBase & ) { return *this; }
+      };
+    }
+
+    ///////////////////////////////////////////////////////////////////
+    //
+    // CLASS NAME : SafeBool
+    //
+    /** Validate objects in a boolean context without harmful side effects.
+     * \see http://www.artima.com/cppsource/safebool.html
+     *
+     * Uses CRTP to avoid a virtual function. \c _Derived must provide
+     * <tt>bool boolTest() const</tt> preformong the test.
+     * \code
+     * class Foo : public base::SafeBool<Foo>
+     * {
+     * public:
+     *   bool boolTest() const
+     *   {
+     *     // Perform Boolean logic here
+     *   }
+     * };
+     * \endcode
+     * \todo Investigate why Bit refuses private inheritance
+     * and exposition of operator bool_type.
+    */
+    template<class _Derived>
+      struct SafeBool : public safebool_detail::SafeBoolBase
+      {
+        operator bool_type() const
+        {
+          return( (static_cast<const _Derived *>(this))->boolTest()
+                  ? &safebool_detail::SafeBoolBase::theTrueBoolType
+                  : 0 );
+        }
+      protected:
+        ~SafeBool() {}
+      };
+    ///////////////////////////////////////////////////////////////////
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace base
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_SAFEBOOL_H