Skip to content

Commit

Permalink
Fix conan example
Browse files Browse the repository at this point in the history
  • Loading branch information
foonathan committed May 4, 2018
1 parent 409737e commit 9ed89d6
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions test_package/example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2016 Jonathan Müller <[email protected]>
// Copyright (C) 2016-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

Expand All @@ -20,7 +20,7 @@ ts::optional<char> back(const std::string& str)
}

// some imaginary lookup function
ts::optional<int> lookup(char c)
ts::optional<int> lookup(int c)
{
// simulate lookup
return c == 'T' ? ts::nullopt : ts::make_optional(c + 1);
Expand Down Expand Up @@ -50,11 +50,11 @@ int task_monadic(const std::string& str)
return back(str)
// map takes a functor and applies it to the stored value, if there is any
// the result is another optional with possibly different type
.map([](char c) -> char { return std::toupper(c); })
// lookup is similar to map
// but the result of map(lookup) would be a ts::optional<ts::optional<int>>
// bind() calls unwrap() to flatten it to ts::optional<int>
.bind(lookup)
.map(static_cast<int (*)(int)>(&std::toupper))
// now we map lookup
// as lookup returns already an optional itself,
// it won't wrap the result in another optional
.map(lookup)
// value_or() as usual
.value_or(0);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ int main()
ts::optional_ref<int> ref;

int a = 42;
ref = a; // assignment rebinds
ref = ts::ref(a); // assignment rebinds
std::cout << ref.value() << '\n';

ref.value() = 0;
Expand All @@ -116,15 +116,10 @@ int main()
ts::optional_ref<const int> ref_const(ref);

// create optional_ref from pointer
auto ptr = ts::ref(&a);
auto ptr_const = ts::cref(&a);

// map() takes a functor and wraps the result in an optional itself
// transform() does not wrap it in an optional to allow arbitrary transformation
// but it needs a fallback value if there is no value stored
// here transform() is used to transform an optional_ref to a normal optional
auto ptr_transformed = ptr.transform(ts::optional<int>(), [](int a) { return a; });
// note that the same for this particular situation can be achieved like this
ptr_transformed = ts::copy(ptr); // there is also ts::move() to move the value
auto ptr = ts::opt_ref(&a);
auto ptr_const = ts::opt_cref(&a);

/// transform an optional_ref to an optional by copying
auto ptr_transformed = ts::copy(ptr); // there is also ts::move() to move the value
std::cout << ptr_transformed.value() << '\n';
}

0 comments on commit 9ed89d6

Please sign in to comment.