Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_container.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63408b99729b4a1bc246de512e472a0c01cb7379 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include "file_reader.hpp"
#include "file_writer.hpp"

#include "../std/vector.hpp"
#include "../std/string.hpp"

class FilesContainerBase
{
protected:

  typedef string Tag;

  struct Info
  {
    Tag m_tag;
    uint64_t m_offset;
    uint64_t m_size;

    Info() {}
    Info(Tag const & tag, uint64_t offset) : m_tag(tag), m_offset(offset) {}
  };

  struct LessInfo
  {
    bool operator() (Info const & t1, Info const & t2) const
    {
      return (t1.m_tag < t2.m_tag);
    }
    bool operator() (Info const & t1, Tag const & t2) const
    {
      return (t1.m_tag < t2);
    }
    bool operator() (Tag const & t1, Info const & t2) const
    {
      return (t1 < t2.m_tag);
    }
  };

  typedef vector<Info> InfoContainer;
  InfoContainer m_info;

  void ReadInfo(FileReader & reader);
};

class FilesContainerR : public FilesContainerBase
{
public:
  explicit FilesContainerR(string const & fName,
                           uint32_t logPageSize = 10,
                           uint32_t logPageCount = 10);

  FileReader GetReader(Tag const & tag) const;

  template <typename F> void ForEachTag(F f) const
  {
    for (size_t i = 0; i < m_info.size(); ++i)
      f(m_info[i].m_tag);
  }

private:
  FileReader m_source;
};

class FilesContainerW : public FilesContainerBase
{
public:
  FilesContainerW(string const & fName,
                  FileWriter::Op op = FileWriter::OP_WRITE_TRUNCATE);
  ~FilesContainerW();

  FileWriter GetWriter(Tag const & tag);

  /// @todo Subclass from FileWriter to check write bounds (avoid overrun).
  /// Return this object with additional check.
  /// @precondition Container should be constructed with OP_WRITE_EXISTING.
  FileWriter GetExistingWriter(Tag const & tag);

  /// @name Append to existing container.
  /// @precondition Container should be constructed with OP_APPEND.
  //@{
  void Append(string const & fName, Tag const & tag);
  void Append(vector<char> const & buffer, Tag const & tag);
  //@}

  void Finish();

private:
  uint64_t SaveCurrentSize();

  string m_name;
  bool m_bNeedRewrite;
  bool m_bFinished;
};