aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-10-12 21:06:56 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-10-12 21:44:18 +0100
commit4a771032711c4cfbf9cb6e96fd6fee553e491d0f (patch)
tree896d2ebdf7a2367e53eb36c97250698a6774cad5
parent903a669622844e80821328e2c40a14c6ad2ae58d (diff)
downloadpaludis-4a771032711c4cfbf9cb6e96fd6fee553e491d0f.tar.gz
paludis-4a771032711c4cfbf9cb6e96fd6fee553e491d0f.tar.xz
Add Tribool class
-rw-r--r--.gitignore1
-rw-r--r--paludis/util/files.m41
-rw-r--r--paludis/util/tribool-fwd.hh33
-rw-r--r--paludis/util/tribool.cc62
-rw-r--r--paludis/util/tribool.hh50
-rw-r--r--paludis/util/tribool_TEST.cc73
6 files changed, 220 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 4a97042..a8afdf2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -368,6 +368,7 @@ paludis-*.*.*.tar.bz2
/paludis/util/thread_TEST
/paludis/util/thread_pool_TEST
/paludis/util/tokeniser_TEST
+/paludis/util/tribool_TEST
/paludis/util/util.hh
/paludis/util/validated_TEST
/paludis/util/virtual_constructor_TEST
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 99af6d6..16e6366 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -64,6 +64,7 @@ add(`system', `hh', `cc', `test', `testscript')
add(`thread', `hh', `cc', `test')
add(`thread_pool', `hh', `cc', `test')
add(`tokeniser', `hh', `cc', `test')
+add(`tribool', `hh', `cc', `fwd', `test')
add(`util', `hh')
add(`validated', `hh', `fwd', `test')
add(`visitor', `hh', `impl', `fwd', `test')
diff --git a/paludis/util/tribool-fwd.hh b/paludis/util/tribool-fwd.hh
new file mode 100644
index 0000000..9ac6b31
--- /dev/null
+++ b/paludis/util/tribool-fwd.hh
@@ -0,0 +1,33 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_TRIBOOL_FWD_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_TRIBOOL_FWD_HH 1
+
+#include <paludis/util/attributes.hh>
+#include <paludis/util/no_type.hh>
+
+namespace paludis
+{
+ struct Tribool;
+
+ NoType<0u> * indeterminate(const NoType<0u> * const) PALUDIS_VISIBLE;
+}
+
+#endif
diff --git a/paludis/util/tribool.cc b/paludis/util/tribool.cc
new file mode 100644
index 0000000..da16dfd
--- /dev/null
+++ b/paludis/util/tribool.cc
@@ -0,0 +1,62 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/util/tribool.hh>
+
+using namespace paludis;
+
+Tribool::Tribool() :
+ _value(v_false)
+{
+}
+
+Tribool::Tribool(const bool b) :
+ _value(b ? v_true : v_false)
+{
+}
+
+Tribool::Tribool(NoType<0u> * (*) (const NoType<0u> * const)) :
+ _value(v_indeterminate)
+{
+}
+
+bool
+Tribool::is_true() const
+{
+ return v_true == _value;
+}
+
+bool
+Tribool::is_false() const
+{
+ return v_false == _value;
+}
+
+bool
+Tribool::is_indeterminate() const
+{
+ return v_indeterminate == _value;
+}
+
+NoType<0u> *
+paludis::indeterminate(const NoType<0u> * const)
+{
+ return 0;
+}
+
diff --git a/paludis/util/tribool.hh b/paludis/util/tribool.hh
new file mode 100644
index 0000000..a878155
--- /dev/null
+++ b/paludis/util/tribool.hh
@@ -0,0 +1,50 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_TRIBOOL_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_TRIBOOL_HH 1
+
+#include <paludis/util/tribool-fwd.hh>
+
+namespace paludis
+{
+ class PALUDIS_VISIBLE Tribool
+ {
+ private:
+ enum Value
+ {
+ v_false,
+ v_true,
+ v_indeterminate
+ };
+
+ Value _value;
+
+ public:
+ Tribool();
+ Tribool(const bool);
+ Tribool(NoType<0u> * (*) (const NoType<0u> * const));
+
+ bool is_true() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ bool is_false() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ bool is_indeterminate() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ };
+}
+
+#endif
diff --git a/paludis/util/tribool_TEST.cc b/paludis/util/tribool_TEST.cc
new file mode 100644
index 0000000..cd418fd
--- /dev/null
+++ b/paludis/util/tribool_TEST.cc
@@ -0,0 +1,73 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/util/tribool.hh>
+#include <test/test_framework.hh>
+#include <test/test_runner.hh>
+
+using namespace paludis;
+using namespace test;
+
+namespace test_cases
+{
+ struct TriboolDefaultCtorTest : TestCase
+ {
+ TriboolDefaultCtorTest() : TestCase("default ctor") { }
+
+ void run()
+ {
+ Tribool b;
+ TEST_CHECK(b.is_false());
+ TEST_CHECK(! b.is_true());
+ TEST_CHECK(! b.is_indeterminate());
+ }
+ } test_default_ctor;
+
+ struct TriboolBoolCtorTest : TestCase
+ {
+ TriboolBoolCtorTest() : TestCase("bool ctor") { }
+
+ void run()
+ {
+ Tribool b(true);
+ TEST_CHECK(! b.is_false());
+ TEST_CHECK(b.is_true());
+ TEST_CHECK(! b.is_indeterminate());
+
+ Tribool f(false);
+ TEST_CHECK(f.is_false());
+ TEST_CHECK(! f.is_true());
+ TEST_CHECK(! f.is_indeterminate());
+ }
+ } test_bool_ctor;
+
+ struct TriboolIndetCtorTest : TestCase
+ {
+ TriboolIndetCtorTest() : TestCase("indet ctor") { }
+
+ void run()
+ {
+ Tribool b(indeterminate);
+ TEST_CHECK(! b.is_false());
+ TEST_CHECK(! b.is_true());
+ TEST_CHECK(b.is_indeterminate());
+ }
+ } test_indet_ctor;
+}
+