本文整理汇总了C++中Optional::Visit方法的典型用法代码示例。如果您正苦于以下问题:C++ Optional::Visit方法的具体用法?C++ Optional::Visit怎么用?C++ Optional::Visit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Optional
的用法示例。
在下文中一共展示了Optional::Visit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: write_png_py
/* function: "write_png(bmp, path[, color_type, text_dict])\n
Writes the bitmap as a png at the given path.\n
Raises OSError on failure.\n
\n
Optional parameters:\n
- color_type: A constant from the png-module (e.g. png.RGB_ALPHA)\n
- text_dict: A dictionary of key-strings to value-strings\n
for png tEXt meta-data."
name: "write_png" */
static void write_png_py(const FilePath& p,
const Bitmap& bmp,
const Optional<int>& rawColorType,
const Optional<png_tEXt_map>& maybeTextChunks)
{
const auto defaultType = static_cast<int>(PngColorType::RGB_ALPHA);
const auto colorType =
to_enum<PngColorType>(rawColorType.Or(defaultType)).Visit(
[](const PngColorType t){
return t;
},
[]() -> PngColorType{
throw ValueError("color_type out of range.");
});
auto r = maybeTextChunks.Visit(
[&](const png_tEXt_map& textChunks){
return write_png(p, bmp, colorType, textChunks);
},
[&](){
return write_png(p, bmp, colorType);
});
if (!r.Successful()){
throw OSError(r.ErrorDescription());
}
}
示例2: merge_settings
Settings merge_settings(const Optional<Settings>& maybeSettings, Settings s){
return maybeSettings.Visit(
[&](const Settings& given){
s.Update(given);
return s;
},
[&](){
return s;
});
}
示例3: test_optional
void test_optional(){
using namespace faint;
// Helper constants for testing the Optional
const IntSize altSize = IntSize(5,5);
const IntSize bmpSize = IntSize(10,10);
const Bitmap alt(altSize);
const Bitmap bmp(bmpSize);
VERIFY(alt.GetSize() != bmp.GetSize());
// Uninitialized optional (not set).
Optional<Bitmap> optional;
VERIFY(optional.NotSet());
VERIFY(!optional.IsSet());
VERIFY(!optional);
static_assert(is_true<decltype(has_or(optional))>(),
"Optional of value type lacks Or-method");
EQUAL(optional.Or(alt).GetSize(), altSize);
optional.IfSet(FAIL_IF_CALLED());
optional.Visit(FAIL_IF_CALLED(), FAIL_UNLESS_CALLED());
optional.Set(bmp);
VERIFY(!optional.NotSet());
VERIFY(optional.IsSet());
VERIFY(optional);
EQUAL(optional.Or(alt).GetSize(), bmpSize);
optional.IfSet(FAIL_UNLESS_CALLED());
optional.Visit(FAIL_UNLESS_CALLED(), FAIL_IF_CALLED());
IntSize sz = optional.VisitSimple(
[](const Bitmap& bmp){
return bmp.GetSize();
},
alt.GetSize());
EQUAL(sz, bmpSize);
EQUAL(optional.Get().GetSize(), bmpSize);
// Take the object (clearing the optional)
Bitmap bmp2 = optional.Take();
EQUAL(bmp2.GetSize(), bmp.GetSize());
VERIFY(optional.NotSet());
// Initializing construction
Optional<Bitmap> optional2(bmp2);
VERIFY(optional2.IsSet());
EQUAL(optional2.Get().GetSize(), bmp.GetSize());
optional2.Clear();
VERIFY(optional2.NotSet());
// Reference
int i = 7;
Optional<int&> oi(i);
oi.Get() = 8;
EQUAL(i, 8);
static_assert(is_false<decltype(has_or(oi))>(),
"Optional of reference type has Or-method.");
// Non-reference
int j = 7;
Optional<int> oj(j);
oj.Get() = 8;
EQUAL(j, 7);
// Ensure that copying a reference-Optional
// does not copy the contained value.
FailIfCopied f(10);
Optional<FailIfCopied&> o(f);
o.Get().value++;
EQUAL(o.Get().value, 11);
Optional<FailIfCopied&> o2(o);
VERIFY(o2.IsSet());
o2.Get().value++;
EQUAL(o.Get().value, 12);
IntHolder h(12);
EQUAL(h.value, 12);
h.Get(true).Get()++;
EQUAL(h.value, 13);
EQUAL(h.Get(true).Get(), 13);
}