xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Utility/UserID.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 //===-- UserID.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_USERID_H
10 #define LLDB_UTILITY_USERID_H
11 
12 #include "lldb/lldb-defines.h"
13 #include "lldb/lldb-types.h"
14 
15 namespace lldb_private {
16 class Stream;
17 
18 /// \class UserID UserID.h "lldb/Core/UserID.h"
19 /// A mix in class that contains a generic user ID.
20 ///
21 /// UserID is designed as a mix in class that can contain an integer based
22 /// unique identifier for a variety of objects in lldb.
23 ///
24 /// The value for this identifier is chosen by each parser plug-in. A value
25 /// should be chosen that makes sense for each kind of object and should allow
26 /// quick access to further and more in depth parsing.
27 ///
28 /// Symbol table entries can use this to store the original symbol table
29 /// index, functions can use it to store the symbol table index or the
30 /// DWARF offset.
31 struct UserID {
32   /// Construct with optional user ID.
33   UserID(lldb::user_id_t uid = LLDB_INVALID_UID) : m_uid(uid) {}
34 
35   /// Destructor.
36   ~UserID() = default;
37 
38   /// Clears the object state.
39   ///
40   /// Clears the object contents back to a default invalid state.
41   void Clear() { m_uid = LLDB_INVALID_UID; }
42 
43   /// Get accessor for the user ID.
44   ///
45   /// \return
46   ///     The user ID.
47   lldb::user_id_t GetID() const { return m_uid; }
48 
49   /// Set accessor for the user ID.
50   ///
51   /// \param[in] uid
52   ///     The new user ID.
53   void SetID(lldb::user_id_t uid) { m_uid = uid; }
54 
55   /// Unary predicate function object that can search for a matching user ID.
56   ///
57   /// Function object that can be used on any class that inherits from UserID:
58   /// \code
59   /// iterator pos;
60   /// pos = std::find_if (coll.begin(), coll.end(), UserID::IDMatches(blockID));
61   /// \endcode
62   class IDMatches {
63   public:
64     /// Construct with the user ID to look for.
65     IDMatches(lldb::user_id_t uid) : m_uid(uid) {}
66 
67     /// Unary predicate function object callback.
68     bool operator()(const UserID &rhs) const { return m_uid == rhs.GetID(); }
69 
70   private:
71     // Member variables.
72     const lldb::user_id_t m_uid; ///< The user ID we are looking for
73   };
74 
75 protected:
76   // Member variables.
77   lldb::user_id_t m_uid; ///< The user ID that uniquely identifies an object.
78 };
79 
80 inline bool operator==(const UserID &lhs, const UserID &rhs) {
81   return lhs.GetID() == rhs.GetID();
82 }
83 
84 inline bool operator!=(const UserID &lhs, const UserID &rhs) {
85   return lhs.GetID() != rhs.GetID();
86 }
87 
88 /// Stream the UserID object to a Stream.
89 Stream &operator<<(Stream &strm, const UserID &uid);
90 
91 } // namespace lldb_private
92 
93 #endif // LLDB_UTILITY_USERID_H
94