TEST(Optional, MakeOptional) { // const L-value version const std::string s("abc"); auto optStr = make_optional(s); ASSERT_TRUE(optStr.hasValue()); EXPECT_EQ(*optStr, "abc"); *optStr = "cde"; EXPECT_EQ(s, "abc"); EXPECT_EQ(*optStr, "cde"); // L-value version std::string s2("abc"); auto optStr2 = make_optional(s2); ASSERT_TRUE(optStr2.hasValue()); EXPECT_EQ(*optStr2, "abc"); *optStr2 = "cde"; // it's vital to check that s2 wasn't clobbered EXPECT_EQ(s2, "abc"); // L-value reference version std::string& s3(s2); auto optStr3 = make_optional(s3); ASSERT_TRUE(optStr3.hasValue()); EXPECT_EQ(*optStr3, "abc"); *optStr3 = "cde"; EXPECT_EQ(s3, "abc"); // R-value ref version unique_ptr<int> pInt(new int(3)); auto optIntPtr = make_optional(std::move(pInt)); EXPECT_TRUE(pInt.get() == nullptr); ASSERT_TRUE(optIntPtr.hasValue()); EXPECT_EQ(**optIntPtr, 3); }
static aoi_type xl_typedef(definition *def_ptr, typedef_def def) { /* * This should be an indirect to the appropriate type, be it structure, * union, enum, etc. It will also possible be an array/pointer to that * structure (requires deeper indirection old_prefix is necessary for * forward references). */ aoi_type component_type, result_type; int array_maximum; /* First, build the component type. */ component_type = xl_td_buildtype(def_ptr, def.old_type); if (def.rel == REL_ALIAS) return component_type; else if (def.rel == REL_POINTER) return make_optional(component_type); if (!strcmp(def.old_type, "string") || !strcmp(def.old_type, "opaque")) /* * `xl_td_buildtype' builds strings and opaques as variable * length arrays. We need to reset the array bounds below. */ result_type = component_type; else if ((def.rel == REL_VECTOR) || (def.rel == REL_ARRAY)) { /* It's an array. */ result_type = (aoi_type) mustmalloc(sizeof(aoi_type_u)); result_type->kind = AOI_ARRAY; result_type->aoi_type_u_u.array_def.element_type = component_type; result_type->aoi_type_u_u.array_def.flgs = AOI_ARRAY_FLAG_NONE; } else panic("Unknown typedef type %d in `xl_typedef'.", def.rel); /* * At this point we are dealing with a REL_VECTOR or a REL_ARRAY. * We need to determine the array's length. */ array_maximum = xl_eval(def_ptr, def.array_max); switch (def.rel) { case REL_VECTOR: result_type->aoi_type_u_u.array_def.length_type = new_int(array_maximum, 0); break; case REL_ARRAY: result_type->aoi_type_u_u.array_def.length_type = new_int(0, array_maximum); break; default: panic("Unknown array type %d in `xl_typedef'.", def.rel); break; } return result_type; }
constexpr optional<std::tuple<char32_t, utf8_view>> decompose_suffix () const { return make_optional(encoding.length() > 0, [&]() { const auto decoding = decode_reverse(encoding.data(), encoding.length()); const auto remainingLength = encoding.length() - std::get<1>(decoding); const auto remainings = utf8_view(array_view<const unsigned char>(encoding.data(), remainingLength)); return std::make_tuple(std::get<0>(decoding), remainings); }); }
optional<std::string> get_content_of_file(const std::string& path) { std::ifstream stream(path); if (!stream.is_open() || !stream.good()) return nullopt; std::string content(std::istreambuf_iterator<char>(stream), (std::istreambuf_iterator<char>())); return make_optional(std::move(content)); }
constexpr optional<std::tuple<char32_t, utf8_view>> decompose_prefix () const { return make_optional(encoding.length() > 0, [&encoding]() { const auto decoding = decode(encoding.data()); const auto remainingLength = encoding.length() - std::get<1>(decoding); const auto offsetData = encoding.data() + std::get<1>(decoding); const auto remainings = utf8_view(array_view<const unsigned char>(offsetData, remainingLength)); return std::make_tuple(std::ge<0>(decoding), remainings); }); }
__AGENCY_ANNOTATION optional<value_type> back_or_none() const { //return empty() ? nullopt : make_optional(back()); // XXX this requires fewer registers than the equivalent above // but depends on the knowledge that the implementation of back() // returns a reference to an actual memory location even when size() == 0 auto result = make_optional(back()); if(empty()) result = nullopt; return result; }
Value Value::PredicateExpr(Predicate predicate, const Type* const boolType) { if (predicate.isTrue()) { return Value::Constant(Constant::True(), boolType); } else if (predicate.isFalse()) { return Value::Constant(Constant::False(), boolType); } else if (predicate.isVariable()) { return Value::TemplateVarRef(predicate.variableTemplateVar(), boolType); } Value value(PREDICATE, boolType, ExitStates::Normal()); value.impl_->predicate = make_optional(std::move(predicate)); return value; }
Optional<size_t> TemplateBuilder::getUse(const TemplateInst& templateInst) const { assert(!templateInst.arguments().empty()); const auto it = templateUseMap_.find(templateInst); if (it != templateUseMap_.end()) { return make_optional(it->second); } if (templateInst.object() == object_ && templateInst.allArgumentsAreSelfTemplateVars()) { // Don't need to add anything to the path when // passing our own template arguments to ourselves. return Optional<size_t>(-1); } return None; }
void Value::setDebugInfo(Debug::ValueInfo newDebugInfo) { impl_->debugInfo = make_optional(std::move(newDebugInfo)); }
void Alias::setValue(Value argValue) { value_ = make_optional(std::move(argValue)); }
optional<T> move(const optional_xvalue_ref<T>& ref) noexcept( std::is_nothrow_move_constructible<T>::value) { return ref.has_value() ? make_optional(ref.value()) : nullopt; }
optional<typename std::remove_const<T>::type> copy(const optional_ref<T>& ref) { return ref.has_value() ? make_optional(ref.value()) : nullopt; }