• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- FileSpecList.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_CORE_FILESPECLIST_H
10 #define LLDB_CORE_FILESPECLIST_H
11 #if defined(__cplusplus)
12 
13 #include "lldb/Utility/FileSpec.h"
14 
15 #include <vector>
16 
17 #include <stddef.h>
18 
19 namespace lldb_private {
20 class Stream;
21 
22 /// \class FileSpecList FileSpecList.h "lldb/Core/FileSpecList.h"
23 /// A file collection class.
24 ///
25 /// A class that contains a mutable list of FileSpec objects.
26 class FileSpecList {
27 public:
28   typedef std::vector<FileSpec> collection;
29   typedef collection::const_iterator const_iterator;
30 
31   /// Default constructor.
32   ///
33   /// Initialize this object with an empty file list.
34   FileSpecList();
35 
36   /// Copy constructor.
37   FileSpecList(const FileSpecList &rhs) = default;
38 
39   /// Move constructor
40   FileSpecList(FileSpecList &&rhs) = default;
41 
42   /// Initialize this object from a vector of FileSpecs
FileSpecList(std::vector<FileSpec> && rhs)43   FileSpecList(std::vector<FileSpec> &&rhs) : m_files(std::move(rhs)) {}
44 
45   /// Destructor.
46   ~FileSpecList();
47 
48   /// Assignment operator.
49   ///
50   /// Replace the file list in this object with the file list from \a rhs.
51   ///
52   /// \param[in] rhs
53   ///     A file list object to copy.
54   ///
55   /// \return
56   ///     A const reference to this object.
57   FileSpecList &operator=(const FileSpecList &rhs) = default;
58 
59   /// Move-assignment operator.
60   FileSpecList &operator=(FileSpecList &&rhs) = default;
61 
62   /// Append a FileSpec object to the list.
63   ///
64   /// Appends \a file to the end of the file list.
65   ///
66   /// \param[in] file
67   ///     A new file to append to this file list.
68   void Append(const FileSpec &file);
69 
70   /// Append a FileSpec object if unique.
71   ///
72   /// Appends \a file to the end of the file list if it doesn't already exist
73   /// in the file list.
74   ///
75   /// \param[in] file
76   ///     A new file to append to this file list.
77   ///
78   /// \return
79   ///     \b true if the file was appended, \b false otherwise.
80   bool AppendIfUnique(const FileSpec &file);
81 
82   /// Inserts a new FileSpec into the FileSpecList constructed in-place with
83   /// the given arguments.
84   ///
85   /// \param[in] args
86   ///     Arguments to create the FileSpec
EmplaceBack(Args &&...args)87   template <class... Args> void EmplaceBack(Args &&... args) {
88     m_files.emplace_back(std::forward<Args>(args)...);
89   }
90 
91   /// Clears the file list.
92   void Clear();
93 
94   /// Dumps the file list to the supplied stream pointer "s".
95   ///
96   /// \param[in] s
97   ///     The stream that will be used to dump the object description.
98   void Dump(Stream *s, const char *separator_cstr = "\n") const;
99 
100   /// Find a file index.
101   ///
102   /// Find the index of the file in the file spec list that matches \a file
103   /// starting \a idx entries into the file spec list.
104   ///
105   /// \param[in] idx
106   ///     An index into the file list.
107   ///
108   /// \param[in] file
109   ///     The file specification to search for.
110   ///
111   /// \param[in] full
112   ///     Should FileSpec::Equal be called with "full" true or false.
113   ///
114   /// \return
115   ///     The index of the file that matches \a file if it is found,
116   ///     else UINT32_MAX is returned.
117   size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
118 
119   /// Get file at index.
120   ///
121   /// Gets a file from the file list. If \a idx is not a valid index, an empty
122   /// FileSpec object will be returned. The file objects that are returned can
123   /// be tested using FileSpec::operator void*().
124   ///
125   /// \param[in] idx
126   ///     An index into the file list.
127   ///
128   /// \return
129   ///     A copy of the FileSpec object at index \a idx. If \a idx
130   ///     is out of range, then an empty FileSpec object will be
131   ///     returned.
132   const FileSpec &GetFileSpecAtIndex(size_t idx) const;
133 
134   /// Get file specification pointer at index.
135   ///
136   /// Gets a file from the file list. The file objects that are returned can
137   /// be tested using FileSpec::operator void*().
138   ///
139   /// \param[in] idx
140   ///     An index into the file list.
141   ///
142   /// \return
143   ///     A pointer to a contained FileSpec object at index \a idx.
144   ///     If \a idx is out of range, then an NULL is returned.
145   const FileSpec *GetFileSpecPointerAtIndex(size_t idx) const;
146 
147   /// Get the memory cost of this object.
148   ///
149   /// Return the size in bytes that this object takes in memory. This returns
150   /// the size in bytes of this object, not any shared string values it may
151   /// refer to.
152   ///
153   /// \return
154   ///     The number of bytes that this object occupies in memory.
155   ///
156   /// \see ConstString::StaticMemorySize ()
157   size_t MemorySize() const;
158 
IsEmpty()159   bool IsEmpty() const { return m_files.empty(); }
160 
161   /// Get the number of files in the file list.
162   ///
163   /// \return
164   ///     The number of files in the file spec list.
165   size_t GetSize() const;
166 
Insert(size_t idx,const FileSpec & file)167   bool Insert(size_t idx, const FileSpec &file) {
168     if (idx < m_files.size()) {
169       m_files.insert(m_files.begin() + idx, file);
170       return true;
171     } else if (idx == m_files.size()) {
172       m_files.push_back(file);
173       return true;
174     }
175     return false;
176   }
177 
Replace(size_t idx,const FileSpec & file)178   bool Replace(size_t idx, const FileSpec &file) {
179     if (idx < m_files.size()) {
180       m_files[idx] = file;
181       return true;
182     }
183     return false;
184   }
185 
Remove(size_t idx)186   bool Remove(size_t idx) {
187     if (idx < m_files.size()) {
188       m_files.erase(m_files.begin() + idx);
189       return true;
190     }
191     return false;
192   }
193 
194   static size_t GetFilesMatchingPartialPath(const char *path, bool dir_okay,
195                                             FileSpecList &matches);
196 
begin()197   const_iterator begin() const { return m_files.begin(); }
end()198   const_iterator end() const { return m_files.end(); }
199 
200 protected:
201   collection m_files; ///< A collection of FileSpec objects.
202 };
203 
204 } // namespace lldb_private
205 
206 #endif // #if defined(__cplusplus)
207 #endif // LLDB_CORE_FILESPECLIST_H
208