LLDB  mainline
UserIDResolver.h
Go to the documentation of this file.
1 //===-- UserIDResolver.h ----------------------------------------*- C++ -*-===//
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 #ifndef LLDB_UTILITY_USERIDRESOLVER_H
10 #define LLDB_UTILITY_USERIDRESOLVER_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <mutex>
15 
16 namespace lldb_private {
17 
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
21 /// are safe.
23 public:
24  typedef uint32_t id_t;
25  virtual ~UserIDResolver(); // anchor
26 
27  llvm::Optional<llvm::StringRef> GetUserName(id_t uid) {
29  }
30  llvm::Optional<llvm::StringRef> GetGroupName(id_t gid) {
32  }
33 
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.
37 
38 protected:
39  virtual llvm::Optional<std::string> DoGetUserName(id_t uid) = 0;
40  virtual llvm::Optional<std::string> DoGetGroupName(id_t gid) = 0;
41 
42 private:
43  using Map = llvm::DenseMap<id_t, llvm::Optional<std::string>>;
44 
45  llvm::Optional<llvm::StringRef>
46  Get(id_t id, Map &cache,
47  llvm::Optional<std::string> (UserIDResolver::*do_get)(id_t));
48 
49  std::mutex m_mutex;
52 };
53 
54 } // namespace lldb_private
55 
56 #endif // LLDB_UTILITY_USERIDRESOLVER_H
A class that represents a running process on the host machine.
llvm::Optional< llvm::StringRef > Get(id_t id, Map &cache, llvm::Optional< std::string >(UserIDResolver::*do_get)(id_t))
llvm::Optional< llvm::StringRef > GetUserName(id_t uid)
virtual llvm::Optional< std::string > DoGetUserName(id_t uid)=0
virtual llvm::Optional< std::string > DoGetGroupName(id_t gid)=0
static UserIDResolver & GetNoopResolver()
Returns a resolver which returns a failure value for each query.
llvm::DenseMap< id_t, llvm::Optional< std::string > > Map
llvm::Optional< llvm::StringRef > GetGroupName(id_t gid)
An abstract interface for things that know how to map numeric user/group IDs into names...