From 204c934dbed2baddd242cdd6efd96362479a3284 Mon Sep 17 00:00:00 2001 From: njlr Date: Thu, 12 Oct 2017 10:54:36 +0100 Subject: [PATCH 1/2] * Tidying up * Added unsafe get --- BUCK | 14 +++++++++++--- license => LICENSE.txt | 0 neither/include/maybe.hpp | 10 +++++++--- neither/tests/maybe.cpp | 5 +++++ 4 files changed, 23 insertions(+), 6 deletions(-) rename license => LICENSE.txt (100%) diff --git a/BUCK b/BUCK index b00181f..f1be0f8 100644 --- a/BUCK +++ b/BUCK @@ -1,16 +1,24 @@ -cxx_library( +prebuilt_cxx_library( name = 'neither', + header_only = True, header_namespace = 'neither', exported_headers = subdir_glob([ ('neither/include', '**/*.hpp'), ]), - visibility = ['PUBLIC'] + licenses = [ + 'LICENSE.txt', + ], + visibility = [ + 'PUBLIC' + ], ) cxx_test( name = 'test', - deps = [':neither'], srcs = glob([ 'neither/tests/**/*.cpp', ]), + deps = [ + ':neither', + ], ) diff --git a/license b/LICENSE.txt similarity index 100% rename from license rename to LICENSE.txt diff --git a/neither/include/maybe.hpp b/neither/include/maybe.hpp index 5be5155..640f1b1 100644 --- a/neither/include/maybe.hpp +++ b/neither/include/maybe.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace neither { @@ -37,9 +38,12 @@ template struct Maybe { } constexpr T get(T defaultValue) { - if (hasValue) - return value; - return defaultValue; + return hasValue ? value : defaultValue; + } + + constexpr T unsafeGet() { + assert(hasValue && "unsafeGet must not be called on an empty Maybe"); + return value; } template diff --git a/neither/tests/maybe.cpp b/neither/tests/maybe.cpp index 12570cb..df15308 100644 --- a/neither/tests/maybe.cpp +++ b/neither/tests/maybe.cpp @@ -27,3 +27,8 @@ TEST(neither, maybe_flatMap_no_value) { auto x = maybe(42); ASSERT_TRUE(x.flatMap([](auto x) { return maybe(); }).get(1) == 1); } + +TEST(neither, maybe_unsafe_get_with_value) { + auto x = maybe(42); + ASSERT_TRUE(x.unsafeGet() == 42); +} From f5208dfb3bd80af802f2e04c577d0481419c9561 Mon Sep 17 00:00:00 2001 From: Gaetano Checinski Date: Thu, 12 Oct 2017 16:35:16 +0100 Subject: [PATCH 2/2] gtest requires shared linkstyle? --- BUCK | 1 + 1 file changed, 1 insertion(+) diff --git a/BUCK b/BUCK index 3883b90..43f2bb3 100644 --- a/BUCK +++ b/BUCK @@ -21,6 +21,7 @@ cxx_test( platform_linker_flags = [ ('^linux.*', [ '-lpthread', ]), ], + link_style = 'shared', deps = [ ':neither', ],