Replace absl::optional with optional-lite

Step 1 of removing abseil from lokinet.

For the most part this is a drop-in replacement, but there are also a
few changes here to the JSONRPC layer that were needed to work around
current gcc 10 dev snapshot:

- JSONRPC returns a json now instead of an optional<json>.  It doesn't
  make any sense to have a json rpc call that just closes the connection
  with returning anything.  Invoked functions can return a null (default
  constructed) result now if they don't have anything to return (such a
  null value won't be added as "result").
pull/1110/head
Jason Rhinelander 4 years ago
parent ecdc389e1a
commit ac1486d0be

3
.gitmodules vendored

@ -16,3 +16,6 @@
[submodule "test/Catch2"]
path = test/Catch2
url = https://github.com/catchorg/Catch2
[submodule "external/optional-lite"]
path = external/optional-lite
url = https://github.com/martinmoene/optional-lite.git

@ -254,6 +254,7 @@ if(SUBMODULE_CHECK)
check_submodule(external/googletest)
check_submodule(external/cxxopts)
check_submodule(external/ghc-filesystem)
check_submodule(external/optional-lite)
endif()
endif()
@ -266,6 +267,7 @@ set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(external/nlohmann EXCLUDE_FROM_ALL)
add_subdirectory(external/cxxopts)
add_subdirectory(external/ghc-filesystem)
add_subdirectory(external/optional-lite)
if(ANDROID)
list(APPEND LIBS log)

@ -0,0 +1 @@
Subproject commit 3b79f4ee539d8f4c709a76b95a13564b6d9ae9cb

@ -7,7 +7,9 @@
#include <util/thread/logic.hpp>
#include <util/time.hpp>
#include <absl/types/optional.h>
#include <nlohmann/json.hpp>
#include <nonstd/optional.hpp>
#include <list>
#include <memory>
#include <string>
@ -27,7 +29,7 @@ namespace abyss
IRPCHandler(ConnImpl* impl);
virtual absl::optional< Response >
virtual Response
HandleJSONRPC(Method_t method, const Params& params) = 0;
virtual ~IRPCHandler();

@ -13,12 +13,11 @@ struct DemoHandler : public abyss::httpd::IRPCHandler
{
}
absl::optional< Response >
HandleJSONRPC(Method_t method,
ABSL_ATTRIBUTE_UNUSED const Params& params) override
nonstd::optional< Response >
HandleJSONRPC(Method_t method, const Params& /*params*/) override
{
llarp::LogInfo("method: ", method);
return Response::object();
return nonstd::make_optional(Response::object());
}
};

@ -184,15 +184,15 @@ namespace abyss
{
nlohmann::json response;
response["jsonrpc"] = "2.0";
response["id"] = m_Request["id"];
response["id"] = m_Request["id"].get< std::string >();
auto value = handler->HandleJSONRPC(
m_Request["method"].get< std::string >(),
m_Request["params"]);
if(value)
{
response["result"] = value.value();
return WriteResponseJSON(response);
}
if(!value.is_null())
response["result"] = std::move(value);
return WriteResponseJSON(response);
}
return WriteResponseSimple(500, "internal error", "text/plain",
"nope");

@ -73,11 +73,12 @@ target_link_libraries(${UTIL_LIB} PUBLIC
absl::synchronization absl::flat_hash_map absl::container
nlohmann_json::nlohmann_json
ghc_filesystem
optional-lite
)
# cut back on fluff
if (NOT WIN32)
target_link_libraries(${UTIL_LIB} PUBLIC absl::optional absl::variant absl::strings)
target_link_libraries(${UTIL_LIB} PUBLIC absl::variant absl::strings)
endif(NOT WIN32)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

@ -35,7 +35,7 @@ namespace llarp
return std::atoi(str.c_str());
}
absl::optional< bool >
nonstd::optional< bool >
setOptBool(string_view val)
{
if(IsTrueValue(val))

@ -75,9 +75,9 @@ namespace llarp
}
template <>
inline absl::optional< bool >
fromEnv< absl::optional< bool > >(const absl::optional< bool >& val,
string_view envNameSuffix)
inline nonstd::optional< bool >
fromEnv< nonstd::optional< bool > >(const nonstd::optional< bool >& val,
string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
const char* ptr = std::getenv(envName.c_str());
@ -112,7 +112,7 @@ namespace llarp
// long term identity key
std::string m_identKeyfile = "identity.key";
absl::optional< bool > m_blockBogons;
nonstd::optional< bool > m_blockBogons;
bool m_publicOverride = false;
struct sockaddr_in m_ip4addr;
@ -142,7 +142,7 @@ namespace llarp
int workerThreads() const { return fromEnv(m_workerThreads, "WORKER_THREADS"); }
int numNetThreads() const { return fromEnv(m_numNetThreads, "NUM_NET_THREADS"); }
std::string defaultLinkProto() const { return fromEnv(m_DefaultLinkProto, "LINK_PROTO"); }
absl::optional< bool > blockBogons() const { return fromEnv(m_blockBogons, "BLOCK_BOGONS"); }
nonstd::optional< bool > blockBogons() const { return fromEnv(m_blockBogons, "BLOCK_BOGONS"); }
// clang-format on
void
@ -155,14 +155,14 @@ namespace llarp
using NetConfig = std::unordered_multimap< std::string, std::string >;
private:
absl::optional< bool > m_enableProfiling;
nonstd::optional< bool > m_enableProfiling;
std::string m_routerProfilesFile = "profiles.dat";
std::string m_strictConnect;
NetConfig m_netConfig;
public:
// clang-format off
absl::optional< bool > enableProfiling() const { return fromEnv(m_enableProfiling, "ENABLE_PROFILING"); }
nonstd::optional< bool > enableProfiling() const { return fromEnv(m_enableProfiling, "ENABLE_PROFILING"); }
std::string routerProfilesFile() const { return fromEnv(m_routerProfilesFile, "ROUTER_PROFILES_FILE"); }
std::string strictConnect() const { return fromEnv(m_strictConnect, "STRICT_CONNECT"); }
const NetConfig& netConfig() const { return m_netConfig; }

@ -134,7 +134,7 @@ namespace llarp
llarp_time_t exploreInterval) override;
/// get localally stored introset by service address
absl::optional< llarp::service::EncryptedIntroSet >
nonstd::optional< llarp::service::EncryptedIntroSet >
GetIntroSetByLocation(const Key_t& location) const override;
void
@ -455,7 +455,7 @@ namespace llarp
}
}
absl::optional< llarp::service::EncryptedIntroSet >
nonstd::optional< llarp::service::EncryptedIntroSet >
Context::GetIntroSetByLocation(const Key_t& key) const
{
auto itr = _services->nodes.find(key);

@ -101,7 +101,7 @@ namespace llarp
Init(const Key_t& us, AbstractRouter* router,
llarp_time_t exploreInterval) = 0;
virtual absl::optional< llarp::service::EncryptedIntroSet >
virtual nonstd::optional< llarp::service::EncryptedIntroSet >
GetIntroSetByLocation(const Key_t& location) const = 0;
virtual llarp_time_t

@ -6,7 +6,7 @@
#include <util/copy_or_nullptr.hpp>
#include <vector>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -20,7 +20,7 @@ namespace llarp
/// txid
uint64_t txid = 0;
/// the key of a router closer in keyspace if iterative lookup
absl::optional< Key_t > closer;
nonstd::optional< Key_t > closer;
GotIntroMessage(const Key_t& from) : IMessage(from)
{

@ -15,7 +15,7 @@
#include <util/thread/logic.hpp>
#include <functional>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -187,7 +187,7 @@ namespace llarp
// the actual hop
std::shared_ptr< Hop > hop;
const absl::optional< llarp::Addr > fromAddr;
const nonstd::optional< llarp::Addr > fromAddr;
LRCMFrameDecrypt(Context* ctx, Decrypter_ptr dec,
const LR_CommitMessage* commit)
@ -196,7 +196,7 @@ namespace llarp
, context(ctx)
, hop(std::make_shared< Hop >())
, fromAddr(commit->session->GetRemoteRC().IsPublicRouter()
? absl::optional< llarp::Addr >{}
? nonstd::optional< llarp::Addr >{}
: commit->session->GetRemoteEndpoint())
{
hop->info.downstream = commit->session->GetPubKey();

@ -138,7 +138,7 @@ namespace llarp
uint64_t currentStatus = status;
size_t index = 0;
absl::optional< RouterID > failedAt;
nonstd::optional< RouterID > failedAt;
while(index < hops.size())
{
if(!frames[index].DoDecrypt(hops[index].shared))

@ -165,7 +165,7 @@ namespace llarp
nickname.Zero();
enckey.Zero();
pubkey.Zero();
routerVersion = absl::optional< RouterVersion >{};
routerVersion = nonstd::optional< RouterVersion >{};
last_updated = 0;
}

@ -105,7 +105,7 @@ namespace llarp
uint64_t last_updated = 0;
uint64_t version = LLARP_PROTO_VERSION;
absl::optional< RouterVersion > routerVersion;
nonstd::optional< RouterVersion > routerVersion;
util::StatusObject
ExtractStatus() const;

@ -400,7 +400,7 @@ namespace llarp
return resp;
}
absl::optional< Response >
Response
HandleJSONRPC(Method_t method,
ABSL_ATTRIBUTE_UNUSED const Params& params) override
{

@ -922,7 +922,7 @@ namespace llarp
bool
Endpoint::OnLookup(const Address& addr,
absl::optional< const IntroSet > introset,
nonstd::optional< IntroSet > introset,
const RouterID& endpoint)
{
const auto now = Router()->Now();

@ -411,7 +411,7 @@ namespace llarp
llarp_async_verify_rc* j);
bool
OnLookup(const service::Address& addr, absl::optional< const IntroSet > i,
OnLookup(const service::Address& addr, nonstd::optional< IntroSet > i,
const RouterID& endpoint); /* */
bool

@ -23,7 +23,7 @@ namespace llarp
HiddenServiceAddressLookup::HandleResponse(
const std::set< EncryptedIntroSet >& results)
{
absl::optional< IntroSet > found;
nonstd::optional< IntroSet > found;
const Address remote(rootkey);
LogInfo("found ", results.size(), " for ", remote.ToString());
if(results.size() > 0)

@ -16,7 +16,7 @@ namespace llarp
uint64_t relayOrder;
const dht::Key_t location;
using HandlerFunc = std::function< bool(
const Address&, absl::optional< const IntroSet >, const RouterID&) >;
const Address&, nonstd::optional< IntroSet >, const RouterID&) >;
HandlerFunc handle;
HiddenServiceAddressLookup(Endpoint* p, HandlerFunc h,

@ -151,7 +151,7 @@ namespace llarp
return crypto->derive_subkey_private(derivedSignKey, signkey, 1);
}
absl::optional< EncryptedIntroSet >
nonstd::optional< EncryptedIntroSet >
Identity::EncryptAndSignIntroSet(const IntroSet& other_i,
llarp_time_t now) const
{

@ -47,7 +47,7 @@ namespace llarp
bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
absl::optional< EncryptedIntroSet >
nonstd::optional< EncryptedIntroSet >
EncryptAndSignIntroSet(const IntroSet& i, llarp_time_t now) const;
bool

@ -6,7 +6,7 @@
#include <service/vanity.hpp>
#include <util/bencode.hpp>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -23,7 +23,7 @@ namespace llarp
VanityNonce vanity;
uint64_t version = LLARP_PROTO_VERSION;
using OptNonce = absl::optional< VanityNonce >;
using OptNonce = nonstd::optional< VanityNonce >;
void
RandomizeVanity()

@ -84,7 +84,7 @@ namespace llarp
return out;
}
absl::optional< IntroSet >
nonstd::optional< IntroSet >
EncryptedIntroSet::MaybeDecrypt(const PubKey& root) const
{
SharedSecret k(root);
@ -176,7 +176,7 @@ namespace llarp
if(key == "w")
{
W = absl::make_optional< PoW >();
W.emplace();
return bencode_decode_dict(*W, buf);
}

@ -10,7 +10,7 @@
#include <util/time.hpp>
#include <util/status.hpp>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <algorithm>
#include <functional>
#include <iostream>
@ -31,7 +31,7 @@ namespace llarp
PQPubKey K;
Tag topic;
llarp_time_t T = 0;
absl::optional< PoW > W;
nonstd::optional< PoW > W;
Signature Z;
uint64_t version = LLARP_PROTO_VERSION;
@ -111,7 +111,7 @@ namespace llarp
llarp_time_t signedAt = 0;
Payload_t introsetPayload;
TunnelNonce nounce;
absl::optional< Tag > topic;
nonstd::optional< Tag > topic;
Signature sig;
bool
@ -145,7 +145,7 @@ namespace llarp
util::StatusObject
ExtractStatus() const;
absl::optional< IntroSet >
nonstd::optional< IntroSet >
MaybeDecrypt(const PubKey& rootKey) const;
};

@ -86,9 +86,9 @@ namespace llarp
}
bool
OutboundContext::OnIntroSetUpdate(
const Address&, absl::optional< const IntroSet > foundIntro,
const RouterID& endpoint)
OutboundContext::OnIntroSetUpdate(const Address&,
nonstd::optional< IntroSet > foundIntro,
const RouterID& endpoint)
{
if(markedBad)
return true;

@ -115,7 +115,7 @@ namespace llarp
OnGeneratedIntroFrame(AsyncKeyExchange* k, PathID_t p);
bool
OnIntroSetUpdate(const Address& addr, absl::optional< const IntroSet > i,
OnIntroSetUpdate(const Address& addr, nonstd::optional< IntroSet > i,
const RouterID& endpoint);
const dht::Key_t location;

@ -16,7 +16,7 @@ namespace fs = ghc::filesystem;
#include <dirent.h>
#endif
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -32,7 +32,7 @@ namespace llarp
/// open a stream to a file and ensure it exists before open
/// sets any permissions on creation
template < typename T >
absl::optional< T >
nonstd::optional< T >
OpenFileStream(fs::path pathname, std::ios::openmode mode)
{
if(EnsurePrivateFile(pathname))

@ -45,7 +45,7 @@ namespace llarp
}
}
absl::optional< LogLevel >
nonstd::optional< LogLevel >
LogLevelFromString(std::string level)
{
std::transform(

@ -1,7 +1,7 @@
#ifndef LLARP_UTIL_LOG_LEVEL_HPP
#define LLARP_UTIL_LOG_LEVEL_HPP
#include <string>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -22,7 +22,7 @@ namespace llarp
std::string
LogLevelToName(LogLevel lvl);
absl::optional< LogLevel >
nonstd::optional< LogLevel >
LogLevelFromString(std::string level);
} // namespace llarp

@ -3,7 +3,7 @@
#include <util/thread/threading.hpp>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <vector>
namespace llarp
@ -330,7 +330,7 @@ namespace llarp
return true;
}
absl::optional< Value >
nonstd::optional< Value >
find(int32_t handle)
{
absl::ReaderMutexLock l(&m_mutex);

@ -711,7 +711,7 @@ namespace llarp
m_categories.clear();
}
absl::optional< absl::Duration >
nonstd::optional< absl::Duration >
PublisherScheduler::find(const Category *category) const
{
util::Lock l(&m_mutex);
@ -725,7 +725,7 @@ namespace llarp
return it->second;
}
absl::optional< absl::Duration >
nonstd::optional< absl::Duration >
PublisherScheduler::getDefault() const
{
util::Lock l(&m_mutex);

@ -1193,16 +1193,16 @@ namespace llarp
return m_manager;
}
absl::optional< absl::Duration >
nonstd::optional< absl::Duration >
find(string_view categoryName) const
{
return find(m_manager->registry().get(categoryName));
}
absl::optional< absl::Duration >
nonstd::optional< absl::Duration >
find(const Category *category) const;
absl::optional< absl::Duration >
nonstd::optional< absl::Duration >
getDefault() const;
std::vector< std::pair< const Category *, absl::Duration > >

@ -32,7 +32,7 @@ namespace llarp
{
namespace
{
absl::optional< std::string >
nonstd::optional< std::string >
makeStr(double d)
{
if(std::isnan(d) || std::isinf(d))
@ -43,7 +43,7 @@ namespace llarp
return std::to_string(d);
}
absl::optional< std::string >
nonstd::optional< std::string >
makeStr(int i)
{
if(i == std::numeric_limits< int >::min()
@ -56,7 +56,7 @@ namespace llarp
}
template < typename Value >
absl::optional< std::string >
nonstd::optional< std::string >
formatValue(const Record< Value > &record, double elapsedTime,
Publication::Type publicationType)
{

@ -10,7 +10,7 @@
#include <absl/container/flat_hash_map.h>
#include <absl/container/flat_hash_set.h>
#include <absl/hash/hash.h>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <absl/types/span.h>
#include <absl/types/variant.h>
#include <cstring>
@ -78,7 +78,7 @@ namespace llarp
struct Format
{
using Spec = absl::optional< FormatSpec >;
using Spec = nonstd::optional< FormatSpec >;
std::array< Spec, Publication::MaxSize > m_specs;
@ -92,10 +92,11 @@ namespace llarp
m_specs[static_cast< size_t >(pub)].emplace(spec);
}
constexpr void
void
clear()
{
m_specs = decltype(m_specs)();
for(auto &s : m_specs)
s.reset();
}
constexpr const FormatSpec *

@ -1,7 +1,7 @@
#ifndef LLARP_STOPWATCH_HPP
#define LLARP_STOPWATCH_HPP
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <absl/time/clock.h>
namespace llarp
@ -10,8 +10,8 @@ namespace llarp
{
class Stopwatch
{
absl::optional< absl::Time > m_start;
absl::optional< absl::Time > m_stop;
nonstd::optional< absl::Time > m_start;
nonstd::optional< absl::Time > m_stop;
public:
Stopwatch() = default;

@ -4,7 +4,7 @@
#include <ev/ev.hpp>
#include <util/mem.h>
#include <util/thread/threadpool.h>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
namespace llarp
{
@ -54,7 +54,7 @@ namespace llarp
using ID_t = std::thread::id;
llarp_threadpool* const m_Thread;
llarp_ev_loop* m_Loop = nullptr;
absl::optional< ID_t > m_ID;
nonstd::optional< ID_t > m_ID;
util::ContentionKiller m_Killer;
std::function< void(std::function< void(void) >) > m_Queue;
};

@ -4,7 +4,7 @@
#include <util/thread/queue_manager.hpp>
#include <util/thread/threading.hpp>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <atomic>
#include <tuple>
@ -73,7 +73,7 @@ namespace llarp
Type
popFront();
absl::optional< Type >
nonstd::optional< Type >
tryPopFront();
// Remove all elements from the queue. Note this is not atomic, and if
@ -260,7 +260,7 @@ namespace llarp
}
template < typename Type >
absl::optional< Type >
nonstd::optional< Type >
Queue< Type >::tryPopFront()
{
uint32_t generation;
@ -285,7 +285,7 @@ namespace llarp
// - notify any waiting pushers
QueuePopGuard< Type > popGuard(*this, generation, index);
return absl::optional< Type >(std::move(m_data[index]));
return nonstd::optional< Type >(std::move(m_data[index]));
}
template < typename Type >

@ -3,7 +3,7 @@
#include <absl/synchronization/barrier.h>
#include <absl/synchronization/mutex.h>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <absl/time/time.h>
#include <iostream>
@ -45,7 +45,7 @@ namespace llarp
/// in debug mode, we implement lock() to enforce that any lock is only
/// used from a single thread. the point of this is to identify locks that
/// are actually needed by dying a painful death when used across threads
mutable absl::optional< std::thread::id > m_id;
mutable nonstd::optional< std::thread::id > m_id;
void
lock() const
{

@ -6,7 +6,7 @@
#include <atomic>
#include <absl/time/time.h>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <map>
#include <utility>
@ -273,7 +273,7 @@ namespace llarp
return true;
}
absl::optional< absl::Time >
nonstd::optional< absl::Time >
nextTime() const
{
absl::ReaderMutexLock lock(&m_mutex);

@ -69,7 +69,7 @@ namespace llarp
void(const dht::Key_t&, AbstractRouter*, llarp_time_t));
MOCK_CONST_METHOD1(GetIntroSetByLocation,
absl::optional< llarp::service::EncryptedIntroSet >(
nonstd::optional< llarp::service::EncryptedIntroSet >(
const llarp::dht::Key_t&));
MOCK_CONST_METHOD0(ExtractStatus, util::StatusObject());

@ -107,7 +107,7 @@ struct ServerHandler : public abyss::httpd::IRPCHandler
{
}
absl::optional< Response >
Response
HandleJSONRPC(Method_t method, ABSL_ATTRIBUTE_UNUSED const Params& params)
{
test->AssertMethod(method);

@ -50,7 +50,7 @@ TEST(Catalog, smoke)
ASSERT_EQ(handle1, handle2);
ASSERT_TRUE(catalog.find(handle1));
absl::optional< double > result = catalog.find(handle1);
nonstd::optional< double > result = catalog.find(handle1);
ASSERT_TRUE(result);
ASSERT_EQ(value2, result);
catalog.remove(handle2);
@ -80,7 +80,7 @@ TEST(Catalog, Iterator)
for(size_t j = 0; j < ITERATION_COUNT; ++j)
{
int32_t handle = cat->add(id);
absl::optional< int32_t > res = cat->find(handle);
nonstd::optional< int32_t > res = cat->find(handle);
ASSERT_TRUE(res);
ASSERT_EQ(res.value(), id);
ASSERT_TRUE(cat->replace(MAX - id, handle));

@ -8,7 +8,7 @@ using TestString = std::string;
struct TestParseLog
{
TestString input;
absl::optional< llarp::LogLevel > level;
nonstd::optional< llarp::LogLevel > level;
};
struct LogLevelTest : public ::testing::TestWithParam< TestParseLog >

@ -585,7 +585,7 @@ TEST(TestQueue, moveIt)
ASSERT_EQ(5u, counter);
absl::optional< MoveTester > optPopped = queue.tryPopFront();
nonstd::optional< MoveTester > optPopped = queue.tryPopFront();
ASSERT_TRUE(optPopped.has_value());

@ -1,6 +1,6 @@
#include <util/thread/queue_manager.hpp>
#include <absl/types/optional.h>
#include <nonstd/optional.hpp>
#include <vector>
#include <gtest/gtest.h>
@ -75,7 +75,7 @@ class IntQueue
}
}
absl::optional< int >
nonstd::optional< int >
tryPopFront()
{
uint32_t gen = 0;

Loading…
Cancel
Save