10b57cec5SDimitry Andric //===-- UserID.h ------------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLDB_UTILITY_USERID_H
100b57cec5SDimitry Andric #define LLDB_UTILITY_USERID_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lldb/lldb-defines.h"
130b57cec5SDimitry Andric #include "lldb/lldb-types.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric namespace lldb_private {
160b57cec5SDimitry Andric class Stream;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric /// \class UserID UserID.h "lldb/Core/UserID.h"
190b57cec5SDimitry Andric /// A mix in class that contains a generic user ID.
200b57cec5SDimitry Andric ///
210b57cec5SDimitry Andric /// UserID is designed as a mix in class that can contain an integer based
220b57cec5SDimitry Andric /// unique identifier for a variety of objects in lldb.
230b57cec5SDimitry Andric ///
240b57cec5SDimitry Andric /// The value for this identifier is chosen by each parser plug-in. A value
250b57cec5SDimitry Andric /// should be chosen that makes sense for each kind of object and should allow
260b57cec5SDimitry Andric /// quick access to further and more in depth parsing.
270b57cec5SDimitry Andric ///
280b57cec5SDimitry Andric /// Symbol table entries can use this to store the original symbol table
290b57cec5SDimitry Andric /// index, functions can use it to store the symbol table index or the
300b57cec5SDimitry Andric /// DWARF offset.
310b57cec5SDimitry Andric struct UserID {
320b57cec5SDimitry Andric   /// Construct with optional user ID.
m_uidUserID330b57cec5SDimitry Andric   UserID(lldb::user_id_t uid = LLDB_INVALID_UID) : m_uid(uid) {}
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   /// Destructor.
360b57cec5SDimitry Andric   ~UserID() = default;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   /// Clears the object state.
390b57cec5SDimitry Andric   ///
400b57cec5SDimitry Andric   /// Clears the object contents back to a default invalid state.
ClearUserID410b57cec5SDimitry Andric   void Clear() { m_uid = LLDB_INVALID_UID; }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   /// Get accessor for the user ID.
440b57cec5SDimitry Andric   ///
450b57cec5SDimitry Andric   /// \return
460b57cec5SDimitry Andric   ///     The user ID.
GetIDUserID470b57cec5SDimitry Andric   lldb::user_id_t GetID() const { return m_uid; }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   /// Set accessor for the user ID.
500b57cec5SDimitry Andric   ///
510b57cec5SDimitry Andric   /// \param[in] uid
520b57cec5SDimitry Andric   ///     The new user ID.
SetIDUserID530b57cec5SDimitry Andric   void SetID(lldb::user_id_t uid) { m_uid = uid; }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   /// Unary predicate function object that can search for a matching user ID.
560b57cec5SDimitry Andric   ///
570b57cec5SDimitry Andric   /// Function object that can be used on any class that inherits from UserID:
580b57cec5SDimitry Andric   /// \code
590b57cec5SDimitry Andric   /// iterator pos;
600b57cec5SDimitry Andric   /// pos = std::find_if (coll.begin(), coll.end(), UserID::IDMatches(blockID));
610b57cec5SDimitry Andric   /// \endcode
620b57cec5SDimitry Andric   class IDMatches {
630b57cec5SDimitry Andric   public:
640b57cec5SDimitry Andric     /// Construct with the user ID to look for.
IDMatchesUserID650b57cec5SDimitry Andric     IDMatches(lldb::user_id_t uid) : m_uid(uid) {}
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric     /// Unary predicate function object callback.
operatorUserID680b57cec5SDimitry Andric     bool operator()(const UserID &rhs) const { return m_uid == rhs.GetID(); }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   private:
710b57cec5SDimitry Andric     // Member variables.
720b57cec5SDimitry Andric     const lldb::user_id_t m_uid; ///< The user ID we are looking for
730b57cec5SDimitry Andric   };
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric protected:
760b57cec5SDimitry Andric   // Member variables.
770b57cec5SDimitry Andric   lldb::user_id_t m_uid; ///< The user ID that uniquely identifies an object.
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric inline bool operator==(const UserID &lhs, const UserID &rhs) {
810b57cec5SDimitry Andric   return lhs.GetID() == rhs.GetID();
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric inline bool operator!=(const UserID &lhs, const UserID &rhs) {
850b57cec5SDimitry Andric   return lhs.GetID() != rhs.GetID();
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric /// Stream the UserID object to a Stream.
890b57cec5SDimitry Andric Stream &operator<<(Stream &strm, const UserID &uid);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric } // namespace lldb_private
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric #endif // LLDB_UTILITY_USERID_H
94