From c382010ae2e764bf7bbdf467c3d9a115a2113e50 Mon Sep 17 00:00:00 2001
From: Olivier Gayot <og@satcom1.com>
Date: Sun, 19 Nov 2017 16:01:31 +0100
Subject: Added bool operator and ! operator

We can now check if a number evaluates to false or true by doing a
conversion to bool or use the ! operator.

Signed-off-by: Olivier Gayot <og@satcom1.com>
---
 number.cpp | 11 +++++++++++
 number.h   | 11 +++++++++++
 2 files changed, 22 insertions(+)

diff --git a/number.cpp b/number.cpp
index 12448de..94ef1ee 100644
--- a/number.cpp
+++ b/number.cpp
@@ -70,6 +70,11 @@ number::to_uint32() const
     return (*this == 0) ? 0 : _operands.front();
 }
 
+number::operator bool() const
+{
+    return *this != 0;
+}
+
 /* }}} */
 /* Operations {{{ */
 
@@ -226,6 +231,12 @@ number::operator!=(const number &n) const
     return _operands != n._operands;
 }
 
+bool
+number::operator!() const
+{
+    return !static_cast<bool>(*this);
+}
+
 /* }}} */
 /* Assignment operators {{{ */
 
diff --git a/number.h b/number.h
index f68299d..c920ddc 100644
--- a/number.h
+++ b/number.h
@@ -33,6 +33,12 @@ public:
      */
     std::uint32_t to_uint32() const;
 
+    /**
+     * \brief Return true if this number is not equal to 0. Otherwise, return
+     * false.
+     */
+    explicit operator bool() const;
+
     /**
      * \brief Return the result of the addition of a number and this number.
      *
@@ -104,6 +110,11 @@ public:
      */
     bool operator!=(const number &n) const;
 
+    /**
+     * \brief Tells whether this number evaluates to false.
+     */
+    bool operator!() const;
+
     /**
      * \brief Add a number to this number.
      *
-- 
cgit v1.2.3