Don't use std::optional::value() because f u macos

This replaces all use of std::optional's `opt.value()` with `*opt`
because macOS is great and the ghost of Steve Jobs says that actually
supporting std::optional's value() method is not for chumps before macOS
10.14.  So don't use it because Apple is great.

Pretty much all of our use of it actually is done better with operator*
anyway (since operator* doesn't do a check that the optional has a
value).

Also replaced *most* of the `has_value()` calls with direct bool
context, except for one in the config section which looked really
confusing at a glance without a has_value().
pull/1282/head
Jason Rhinelander 4 years ago
parent 7f9160bb6e
commit ebd2142114

@ -6,7 +6,7 @@ set(std_optional_code [[
int main() {
std::optional<int> maybe;
maybe = 1;
return maybe.value() == 1;
return *maybe == 1;
}
]])

@ -192,9 +192,9 @@ namespace llarp
if (arg.empty())
{
const auto maybe = llarp::FindFreeRange();
if (not maybe.has_value())
if (not maybe)
throw std::invalid_argument("cannot determine free ip range");
arg = maybe.value();
arg = *maybe;
}
m_ifaddr = arg;
});
@ -203,9 +203,9 @@ namespace llarp
if (arg.empty())
{
const auto maybe = llarp::FindFreeTun();
if (not maybe.has_value())
if (not maybe)
throw std::invalid_argument("cannot determine free interface name");
arg = maybe.value();
arg = *maybe;
}
m_ifname = arg;
});
@ -228,8 +228,7 @@ namespace llarp
(void)params;
auto parseDNSAddr = [](auto arg) {
IpAddress addr{arg};
const auto maybePort = addr.getPort();
if (not maybePort.has_value())
if (not addr.getPort())
addr.setPort(53);
return addr;
};
@ -399,10 +398,10 @@ namespace llarp
conf.defineOption<std::string>(
"logging", "level", false, DefaultLogLevel, [this](std::string arg) {
std::optional<LogLevel> level = LogLevelFromString(arg);
if (not level.has_value())
if (not level)
throw std::invalid_argument(stringify("invalid log level value: ", arg));
m_logLevel = level.value();
m_logLevel = *level;
});
conf.defineOption<std::string>(
@ -553,13 +552,13 @@ namespace llarp
// open a filestream
auto stream = llarp::util::OpenFileStream<std::ofstream>(confFile.c_str(), std::ios::binary);
if (not stream.has_value() or not stream.value().is_open())
if (not stream or not stream->is_open())
throw std::runtime_error(stringify("Failed to open file ", confFile, " for writing"));
llarp::LogInfo("confStr: ", confStr);
stream.value() << confStr;
stream.value().flush();
*stream << confStr;
stream->flush();
llarp::LogInfo("Generated new config ", confFile);
}

@ -156,8 +156,8 @@ namespace llarp
defaultValueAsString() override
{
std::ostringstream oss;
if (defaultValue.has_value())
oss << defaultValue.value();
if (defaultValue)
oss << *defaultValue;
return oss.str();
}
@ -192,8 +192,8 @@ namespace llarp
std::ostringstream oss;
if (parsedValues.size() > 0)
oss << parsedValues[0];
else if (useDefault and defaultValue.has_value())
oss << defaultValue.value();
else if (useDefault and defaultValue)
oss << *defaultValue;
return oss.str();
}
@ -232,13 +232,13 @@ namespace llarp
else
{
auto maybe = getValue();
if (maybe.has_value())
if (maybe)
{
acceptor(maybe.value());
acceptor(*maybe);
}
else
{
assert(not defaultValue.has_value()); // maybe should have a value if defaultValue does
assert(not defaultValue); // maybe should have a value if defaultValue does
}
}
}

@ -29,7 +29,7 @@ namespace llarp
bool
Context::Configure(bool isRelay, std::optional<fs::path> dataDir)
{
fs::path defaultDataDir = dataDir.has_value() ? dataDir.value() : GetDefaultDataDir();
fs::path defaultDataDir = dataDir ? *dataDir : GetDefaultDataDir();
if (configfile.size())
{

@ -101,7 +101,7 @@ namespace llarp
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
return false;
auto& f = optional_f.value();
auto& f = *optional_f;
if (!f.is_open())
return false;
f.write((char*)buf.base, buf.cur - buf.base);
@ -115,7 +115,7 @@ namespace llarp
auto optional = util::OpenFileStream<std::ifstream>(fpath, std::ios::binary | std::ios::in);
if (!optional)
return false;
auto& f = optional.value();
auto& f = *optional;
f.seekg(0, std::ios::end);
const size_t sz = f.tellg();
f.seekg(0, std::ios::beg);

@ -122,9 +122,9 @@ namespace llarp
{
// we should have this value if introset was propagated properly
const auto maybe = dht.GetIntroSetByLocation(location);
if (maybe.has_value())
if (maybe)
{
replies.emplace_back(new GotIntroMessage({maybe.value()}, txID));
replies.emplace_back(new GotIntroMessage({*maybe}, txID));
}
else
{

@ -86,7 +86,7 @@ namespace llarp
}
if (key == "K")
{
if (closer.has_value()) // duplicate key?
if (closer) // duplicate key?
return false;
dht::Key_t K;
if (not K.BDecode(buf))
@ -111,9 +111,9 @@ namespace llarp
return false;
if (!BEncodeWriteDictList("I", found, buf))
return false;
if (closer.has_value())
if (closer)
{
if (!BEncodeWriteDictEntry("K", closer.value(), buf))
if (!BEncodeWriteDictEntry("K", *closer, buf))
return false;
}
if (!BEncodeWriteDictInt("T", txid, buf))

@ -15,9 +15,9 @@ namespace llarp
llarp::LogWarn("got invalid introset from tag lookup");
return false;
}
if (not introset.topic.has_value())
if (not introset.topic)
return false;
if (introset.topic.value() != target)
if (*introset.topic != target)
{
llarp::LogWarn("got introset with mismatched topic in tag lookup");
return false;

@ -125,10 +125,9 @@ namespace llarp
}
else
{
const auto maybe = GetIFAddr(ifname, af);
if (maybe.has_value())
if (const auto maybe = GetIFAddr(ifname, af))
{
m_ourAddr = maybe.value();
m_ourAddr = *maybe;
}
else
{

@ -261,14 +261,14 @@ namespace llarp
return;
}
if (self->fromAddr.has_value())
if (self->fromAddr)
{
// only do ip limiting from non service nodes
#ifndef LOKINET_HIVE
if (self->context->CheckPathLimitHitByIP(self->fromAddr.value()))
if (self->context->CheckPathLimitHitByIP(*self->fromAddr))
{
// we hit a limit so tell it to slow tf down
llarp::LogError("client path build hit limit ", self->fromAddr.value());
llarp::LogError("client path build hit limit ", *self->fromAddr);
OnForwardLRCMResult(
self->context->Router(),
self->hop->info.rxID,

@ -164,11 +164,7 @@ namespace llarp
const sockaddr_in6* addr6 = addr;
memcpy(ip.s6_addr, addr6->sin6_addr.s6_addr, sizeof(ip.s6_addr));
auto maybePort = address.getPort();
if (maybePort)
port = maybePort.value();
else
port = 0;
port = address.getPort().value_or(0);
}
std::ostream&

@ -94,7 +94,7 @@ namespace llarp
{
SockAddr addr(m_ipAddress);
if (m_port)
addr.setPort(m_port.value());
addr.setPort(*m_port);
return addr;
}

@ -259,7 +259,7 @@ llarp_nodedb::SaveAll()
filepath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
if (!optional_ofs)
continue;
auto& ofs = optional_ofs.value();
auto& ofs = *optional_ofs;
ofs.write((char*)buf.base, buf.sz);
ofs.flush();
ofs.close();

@ -192,12 +192,12 @@ namespace llarp
}
else
{
if (failedAt.has_value())
if (failedAt)
{
r->NotifyRouterEvent<tooling::PathBuildRejectedEvent>(
Endpoint(), RXID(), failedAt.value());
LogWarn(Name(), " build failed at ", failedAt.value());
r->routerProfiling().MarkHopFail(failedAt.value());
Endpoint(), RXID(), *failedAt);
LogWarn(Name(), " build failed at ", *failedAt);
r->routerProfiling().MarkHopFail(*failedAt);
}
else
r->routerProfiling().MarkPathFail(this);

@ -125,9 +125,9 @@ namespace llarp
do
{
auto maybe = self->m_DownstreamGather.tryPopFront();
if (not maybe.has_value())
if (not maybe)
break;
msgs.emplace_back(maybe.value());
msgs.emplace_back(*maybe);
} while (true);
self->HandleAllDownstream(std::move(msgs), r);
};
@ -167,9 +167,9 @@ namespace llarp
do
{
auto maybe = self->m_UpstreamGather.tryPopFront();
if (not maybe.has_value())
if (not maybe)
break;
msgs.emplace_back(maybe.value());
msgs.emplace_back(*maybe);
} while (true);
self->HandleAllUpstream(std::move(msgs), r);
};

@ -230,7 +230,7 @@ namespace llarp
auto optional_f = util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
return false;
auto& f = optional_f.value();
auto& f = *optional_f;
if (f.is_open())
{
f.write((char*)buf.base, buf.sz);

@ -548,13 +548,10 @@ namespace llarp
}
// Network config
if (conf->network.m_enableProfiling.has_value())
if (conf->network.m_enableProfiling.has_value() and not *conf->network.m_enableProfiling)
{
if (not conf->network.m_enableProfiling.value())
{
routerProfiling().Disable();
LogWarn("router profiling explicitly disabled");
}
routerProfiling().Disable();
LogWarn("router profiling explicitly disabled");
}
if (!conf->network.m_routerProfilesFile.empty())

@ -125,9 +125,9 @@ namespace llarp
if (!enckey.BEncode(buf))
return false;
// write router version if present
if (routerVersion.has_value())
if (routerVersion)
{
if (not BEncodeWriteDictEntry("r", routerVersion.value(), buf))
if (not BEncodeWriteDictEntry("r", *routerVersion, buf))
return false;
}
/* write last updated */
@ -180,7 +180,7 @@ namespace llarp
{
obj["nickname"] = Nick();
}
if (routerVersion.has_value())
if (routerVersion)
{
obj["routerVersion"] = routerVersion->ToString();
}
@ -246,7 +246,7 @@ namespace llarp
bool
RouterContact::IsPublicRouter() const
{
if (not routerVersion.has_value())
if (not routerVersion)
return false;
return !addrs.empty();
}
@ -390,17 +390,16 @@ namespace llarp
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
const fs::path fpath = std::string(fname); /* */
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
auto f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!f)
{
return false;
}
auto& f = optional_f.value();
if (!f.is_open())
if (!f->is_open())
{
return false;
}
f.write((char*)buf.base, buf.sz);
f->write((char*)buf.base, buf.sz);
return true;
}

@ -106,12 +106,12 @@ namespace llarp
return;
}
auto maybe = m_Identity.EncryptAndSignIntroSet(introSet(), now);
if (not maybe.has_value())
if (not maybe)
{
LogWarn("failed to generate introset for endpoint ", Name());
return;
}
if (PublishIntroSet(maybe.value(), Router()))
if (PublishIntroSet(*maybe, Router()))
{
LogInfo("(re)publishing introset for endpoint ", Name());
}
@ -798,9 +798,9 @@ namespace llarp
do
{
auto maybe = m_RecvQueue.tryPopFront();
if (not maybe.has_value())
if (not maybe)
return;
auto ev = std::move(maybe.value());
auto ev = std::move(*maybe);
ProtocolMessage::ProcessAsync(ev.fromPath, ev.pathid, ev.msg);
} while (true);
}
@ -928,7 +928,7 @@ namespace llarp
const auto now = Router()->Now();
auto& fails = m_state->m_ServiceLookupFails;
auto& lookups = m_state->m_PendingServiceLookups;
if (not introset.has_value() || introset->IsExpired(now))
if (not introset or introset->IsExpired(now))
{
LogError(Name(), " failed to lookup ", addr.ToString(), " from ", endpoint);
fails[endpoint] = fails[endpoint] + 1;
@ -947,7 +947,7 @@ namespace llarp
if (m_state->m_RemoteSessions.count(addr) > 0)
return true;
PutNewOutboundContext(introset.value());
PutNewOutboundContext(*introset);
return true;
}

@ -38,8 +38,8 @@ namespace llarp
selected = introset;
}
const auto maybe = selected.MaybeDecrypt(rootkey);
if (maybe.has_value())
found = maybe.value();
if (maybe)
found = *maybe;
}
return handle(remote, found, endpoint);
}

@ -118,7 +118,7 @@ namespace llarp
auto optional_f = util::OpenFileStream<std::ofstream>(fname, std::ios::binary);
if (!optional_f)
return false;
auto& f = optional_f.value();
auto& f = *optional_f;
if (!f.is_open())
return false;
f.write((char*)buf.cur, buf.sz);
@ -143,7 +143,7 @@ namespace llarp
if (!bencode_decode_dict(*this, &buf))
return false;
ServiceInfo::OptNonce van;
std::optional<VanityNonce> van;
if (!vanity.IsZero())
van = vanity;
// update pubkeys

@ -20,13 +20,13 @@ namespace llarp
}
bool
ServiceInfo::Update(const byte_t* sign, const byte_t* enc, const OptNonce& nonce)
ServiceInfo::Update(const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce)
{
signkey = sign;
enckey = enc;
if (nonce)
{
vanity = nonce.value();
vanity = *nonce;
}
return UpdateAddr();
}

@ -23,8 +23,6 @@ namespace llarp
VanityNonce vanity;
uint64_t version = LLARP_PROTO_VERSION;
using OptNonce = std::optional<VanityNonce>;
void
RandomizeVanity()
{
@ -45,7 +43,7 @@ namespace llarp
}
bool
Update(const byte_t* sign, const byte_t* enc, const OptNonce& nonce = OptNonce());
Update(const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce = {});
bool
operator==(const ServiceInfo& other) const

@ -279,13 +279,13 @@ namespace llarp
{
if (intro.expiresAt > now && intro.expiresAt - now > path::default_lifetime)
{
if (W && intro.expiresAt - W->extendedLifetime > path::default_lifetime)
if (!W)
{
LogWarn("intro has too high expire time");
return false;
}
if (!W.has_value())
if (intro.expiresAt - W->extendedLifetime > path::default_lifetime)
{
LogWarn("intro has too high expire time");
return false;
}
}
@ -329,7 +329,7 @@ namespace llarp
printer.printAttribute("T", T.count());
if (W)
{
printer.printAttribute("W", W.value());
printer.printAttribute("W", *W);
}
else
{

@ -90,11 +90,11 @@ namespace llarp
if (markedBad)
return true;
updatingIntroSet = false;
if (foundIntro.has_value())
if (foundIntro)
{
if (foundIntro->T == 0s)
{
LogWarn(Name(), " got introset with zero timestamp: ", foundIntro.value());
LogWarn(Name(), " got introset with zero timestamp: ", *foundIntro);
return true;
}
if (currentIntroSet.T > foundIntro->T)
@ -109,7 +109,7 @@ namespace llarp
LogError("got expired introset from lookup from ", endpoint);
return true;
}
currentIntroSet = foundIntro.value();
currentIntroSet = *foundIntro;
ShiftIntroduction(false);
}
else

@ -46,9 +46,9 @@ namespace llarp
do
{
auto maybe = m_SendQueue.tryPopFront();
if (not maybe.has_value())
if (not maybe)
break;
auto& item = maybe.value();
auto& item = *maybe;
if (item.second->SendRoutingMessage(*item.first, r))
{
lastGoodSend = r->Now();

@ -357,13 +357,10 @@ namespace llarp
buf.sz = buf.cur - buf.base;
{
const fs::path path = std::string(fpath);
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(path, std::ios::binary);
if (!optional_f)
auto f = llarp::util::OpenFileStream<std::ofstream>(path, std::ios::binary);
if (not f or not f->is_open())
return false;
auto& f = optional_f.value();
if (!f.is_open())
return false;
f.write((char*)buf.base, buf.sz);
f->write((char*)buf.base, buf.sz);
}
return true;
}

@ -14,10 +14,9 @@ namespace llarp
do
{
auto maybe_line = lines->tryPopFront();
if (not maybe_line.has_value())
if (not maybe_line)
break;
const auto& line = maybe_line.value();
if (fprintf(f, "%s\n", line.c_str()) >= 0)
if (fprintf(f, "%s\n", maybe_line->c_str()) >= 0)
wrote_stuff = true;
} while (true);
@ -43,7 +42,7 @@ namespace llarp
do
{
auto line = m_Lines.tryPopFront();
if (not line.has_value())
if (not line)
break;
} while (true);
fflush(m_File);

@ -13,8 +13,8 @@ namespace llarp
std::promise<ID_t> result;
// queue setting id and try to get the result back
llarp_threadpool_queue_job(m_Thread, [&]() {
m_ID.emplace(std::this_thread::get_id());
result.set_value(m_ID.value());
m_ID = std::this_thread::get_id();
result.set_value(*m_ID);
});
// get the result back
ID_t spawned = result.get_future().get();
@ -129,7 +129,7 @@ namespace llarp
bool
Logic::can_flush() const
{
return m_ID.value() == std::this_thread::get_id();
return *m_ID == std::this_thread::get_id();
}
void

@ -27,9 +27,9 @@ namespace llarp
{
auto functor = m_queue.tryPopFront();
if (functor.has_value())
if (functor)
{
functor.value()();
(*functor)();
}
else
{
@ -57,7 +57,7 @@ namespace llarp
return;
}
functor.value()();
(*functor)();
}
}

@ -51,13 +51,13 @@ namespace llarp
{
if (!m_id)
{
m_id.emplace(std::this_thread::get_id());
m_id = std::this_thread::get_id();
}
else if (m_id.value() != std::this_thread::get_id())
else if (*m_id != std::this_thread::get_id())
{
std::cerr << "NullMutex " << this << " was used across threads: locked by "
<< std::this_thread::get_id() << " and was previously locked by "
<< m_id.value() << "\n";
<< *m_id << "\n";
// if you're encountering this abort() call, you may have discovered a
// case where a NullMutex should be reverted to a "real mutex"
std::abort();

@ -392,14 +392,14 @@ TEST_CASE("ConfigDefinition truthy/falsy bool values", "[config]")
// defaults to false
auto maybe = def.getValue();
CHECK(maybe.has_value());
CHECK(maybe.value() == false);
REQUIRE(maybe);
CHECK(*maybe == false);
// val should result in true
CHECK_NOTHROW(def.parseValue(val));
maybe = def.getValue();
CHECK(maybe.has_value());
CHECK(maybe.value() == true);
REQUIRE(maybe);
CHECK(*maybe);
}
// falsy values
@ -409,14 +409,14 @@ TEST_CASE("ConfigDefinition truthy/falsy bool values", "[config]")
// defaults to true
auto maybe = def.getValue();
CHECK(maybe.has_value());
CHECK(maybe.value() == true);
REQUIRE(maybe);
CHECK(maybe == true);
// val should result in false
CHECK_NOTHROW(def.parseValue(val));
maybe = def.getValue();
CHECK(maybe.has_value());
CHECK(maybe.value() == false);
REQUIRE(maybe);
CHECK(maybe == false);
}
// illegal values

@ -577,7 +577,7 @@ TEST(TestQueue, moveIt)
std::optional< MoveTester > optPopped = queue.tryPopFront();
ASSERT_TRUE(optPopped.has_value());
ASSERT_TRUE(optPopped);
// Moved twice here to construct the optional.
ASSERT_EQ(6u, counter);

@ -258,8 +258,8 @@ TEST(TestQueueManager, SimpleUsage)
auto result = queue.tryPopFront();
ASSERT_TRUE(result.has_value());
ASSERT_EQ(1, result.value());
ASSERT_TRUE(result);
ASSERT_EQ(1, *result);
}
class BasicFunctionality : public ::testing::TestWithParam< uint32_t >

Loading…
Cancel
Save