summaryrefslogtreecommitdiff
path: root/src/shared/fileregister.h
blob: 9294d20ed4bb3461355a73119888adf903415817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef MO_REGISTER_FILESREGISTER_INCLUDED
#define MO_REGISTER_FILESREGISTER_INCLUDED

#include "fileregisterfwd.h"
#include <mutex>
#include <boost/shared_ptr.hpp>

namespace MOShared
{

class FileRegister
{
public:
  FileRegister(boost::shared_ptr<OriginConnection> originConnection);

  // noncopyable
  FileRegister(const FileRegister&) = delete;
  FileRegister& operator=(const FileRegister&) = delete;

  bool indexValid(FileIndex index) const;

  FileEntryPtr createFile(
    std::wstring name, DirectoryEntry *parent, DirectoryStats& stats);

  FileEntryPtr getFile(FileIndex index) const;

  size_t highestCount() const
  {
    std::scoped_lock lock(m_Mutex);
    return m_Files.size();
  }

  void reserve(std::size_t n)
  {
    m_Files.reserve(n);
  }

  bool removeFile(FileIndex index);
  void removeOrigin(FileIndex index, OriginID originID);
  void removeOriginMulti(std::set<FileIndex> indices, OriginID originID);

  void sortOrigins();

private:
  using FileMap = std::vector<FileEntryPtr>;

  mutable std::mutex m_Mutex;
  FileMap m_Files;
  boost::shared_ptr<OriginConnection> m_OriginConnection;
  std::atomic<FileIndex> m_NextIndex;

  void unregisterFile(FileEntryPtr file);
  FileIndex generateIndex();
};

} // namespace

#endif // MO_REGISTER_FILESREGISTER_INCLUDED