1 | //===-- UserIDResolver.cpp ------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Utility/UserIDResolver.h" |
10 | #include "llvm/Support/ManagedStatic.h" |
11 | #include <optional> |
12 | |
13 | using namespace lldb_private; |
14 | |
15 | UserIDResolver::~UserIDResolver() = default; |
16 | |
17 | std::optional<llvm::StringRef> UserIDResolver::Get( |
18 | id_t id, Map &cache, |
19 | std::optional<std::string> (UserIDResolver::*do_get)(id_t)) { |
20 | |
21 | std::lock_guard<std::mutex> guard(m_mutex); |
22 | auto iter_bool = cache.try_emplace(Key: id, Args: std::nullopt); |
23 | if (iter_bool.second) |
24 | iter_bool.first->second = (this->*do_get)(id); |
25 | if (iter_bool.first->second) |
26 | return llvm::StringRef(*iter_bool.first->second); |
27 | return std::nullopt; |
28 | } |
29 | |
30 | namespace { |
31 | class NoopResolver : public UserIDResolver { |
32 | protected: |
33 | std::optional<std::string> DoGetUserName(id_t uid) override { |
34 | return std::nullopt; |
35 | } |
36 | |
37 | std::optional<std::string> DoGetGroupName(id_t gid) override { |
38 | return std::nullopt; |
39 | } |
40 | }; |
41 | } // namespace |
42 | |
43 | static llvm::ManagedStatic<NoopResolver> g_noop_resolver; |
44 | |
45 | UserIDResolver &UserIDResolver::GetNoopResolver() { return *g_noop_resolver; } |
46 |