1 //===-- UserIDResolver.h ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_USERIDRESOLVER_H
10 #define LLDB_UTILITY_USERIDRESOLVER_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringRef.h"
16 namespace lldb_private {
18 /// An abstract interface for things that know how to map numeric user/group IDs
19 /// into names. It caches the resolved names to avoid repeating expensive
20 /// queries. The cache is internally protected by a mutex, so concurrent queries
22 class UserIDResolver {
24 typedef uint32_t id_t;
25 virtual ~UserIDResolver(); // anchor
27 llvm::Optional<llvm::StringRef> GetUserName(id_t uid) {
28 return Get(uid, m_uid_cache, &UserIDResolver::DoGetUserName);
30 llvm::Optional<llvm::StringRef> GetGroupName(id_t gid) {
31 return Get(gid, m_gid_cache, &UserIDResolver::DoGetGroupName);
34 /// Returns a resolver which returns a failure value for each query. Useful as
35 /// a fallback value for the case when we know all lookups will fail.
36 static UserIDResolver &GetNoopResolver();
39 virtual llvm::Optional<std::string> DoGetUserName(id_t uid) = 0;
40 virtual llvm::Optional<std::string> DoGetGroupName(id_t gid) = 0;
43 using Map = llvm::DenseMap<id_t, llvm::Optional<std::string>>;
45 llvm::Optional<llvm::StringRef>
46 Get(id_t id, Map &cache,
47 llvm::Optional<std::string> (UserIDResolver::*do_get)(id_t));
54 } // namespace lldb_private
56 #endif // #ifndef LLDB_HOST_USERIDRESOLVER_H