~josejuan-sanchez/esajpip/continue

« back to all changes in this revision

Viewing changes to src/data/file_segment.h

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-10-01 10:01:21 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20131001100121-xfobvkenqie7y0te
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _DATA_FILE_SEGMENT_H_
 
2
#define _DATA_FILE_SEGMENT_H_
 
3
 
 
4
 
 
5
#include <iostream>
 
6
#include <stdint.h>
 
7
#include <assert.h>
 
8
 
 
9
 
 
10
namespace data
 
11
{
 
12
  using namespace std;
 
13
 
 
14
 
 
15
  /**
 
16
   * Identifies a data segment of a file. This segment is defined by an offset
 
17
   * and a length (number of bytes), both of them with an unsigned integer of
 
18
   * 64 bits. This class is serializable and can be printed.
 
19
   */
 
20
  class FileSegment
 
21
  {
 
22
  public:
 
23
    uint64_t offset;    ///< Offset of the data segment.
 
24
    uint64_t length;    ///< Length of the data segment.
 
25
 
 
26
 
 
27
    /**
 
28
     * Identifies a null segment, with the offset as well as the length
 
29
     * set to zero.
 
30
     */
 
31
    static const FileSegment Null;
 
32
 
 
33
 
 
34
    /**
 
35
     * Initializes all the member variables with zero, being a
 
36
     * null segment.
 
37
     */
 
38
    FileSegment()
 
39
    {
 
40
      offset = length = 0;
 
41
    }
 
42
 
 
43
    /**
 
44
     * Initializes the segment with the given parameters.
 
45
     * @param offset Offset of the segment.
 
46
     * @param length Length of the segment.
 
47
     */
 
48
    FileSegment(uint64_t offset, uint64_t length)
 
49
    {
 
50
      this->offset = offset;
 
51
      this->length = length;
 
52
    }
 
53
 
 
54
    /**
 
55
     * Copy constructor.
 
56
     */
 
57
    FileSegment(const FileSegment& segment)
 
58
    {
 
59
      *this = segment;
 
60
    }
 
61
 
 
62
    /**
 
63
     * Copy assignment.
 
64
     */
 
65
    FileSegment& operator=(const FileSegment& segment)
 
66
    {
 
67
      offset = segment.offset;
 
68
      length = segment.length;
 
69
 
 
70
      return *this;
 
71
    }
 
72
 
 
73
    /**
 
74
     * Removes the first bytes of the segment. Modifies the segment as
 
75
     * if a number of bytes (specified by the parameter) was removed
 
76
     * from the beginning of the segment.
 
77
     * @param count Number of bytes to remove.
 
78
     * @return <code>*this</code>.
 
79
     */
 
80
    FileSegment& RemoveFirst(int count)
 
81
    {
 
82
      assert((length - count) >= 0);
 
83
 
 
84
      offset += count;
 
85
      length -= count;
 
86
 
 
87
      return *this;
 
88
    }
 
89
 
 
90
    /**
 
91
     * Removes the last bytes of the segment. Modifies the segment as
 
92
     * if a number of bytes (specified by the parameter) was removed
 
93
     * from the end of the segment.
 
94
     * @param count Number of bytes to remove.
 
95
     * @return <code>*this</code>.
 
96
     */
 
97
    FileSegment& RemoveLast(int count)
 
98
    {
 
99
      assert((length - count) >= 0);
 
100
 
 
101
      length -= count;
 
102
 
 
103
      return *this;
 
104
    }
 
105
 
 
106
    /**
 
107
     * Returns <code>true</code> if the segment is contiguous to
 
108
     * another given segment, so the first byte of the given segment
 
109
     * is just the next byte after the last byte of the segment.
 
110
     */
 
111
    bool IsContiguousTo(const FileSegment& segment) const
 
112
    {
 
113
      return ((offset + length) == segment.offset);
 
114
    }
 
115
 
 
116
    bool operator==(const FileSegment& segment) const
 
117
    {
 
118
      return ((offset == segment.offset) && (length == segment.length));
 
119
    }
 
120
 
 
121
    bool operator!=(const FileSegment& segment) const
 
122
    {
 
123
      return ((offset != segment.offset) || (length != segment.length));
 
124
    }
 
125
 
 
126
    template<typename T> T& SerializeWith(T& stream)
 
127
    {
 
128
      return (stream & offset & length);
 
129
    }
 
130
 
 
131
    friend ostream& operator << (ostream &out, const FileSegment &segment)
 
132
    {
 
133
        out << "[" << segment.offset << ":" << segment.length << "]";
 
134
        return out;
 
135
    }
 
136
 
 
137
    virtual ~FileSegment()
 
138
    {
 
139
    }
 
140
  };
 
141
 
 
142
}
 
143
 
 
144
 
 
145
 
 
146
#endif /* _DATA_FILE_SEGMENT_H_ */