ESA JPIP server  0.1
file_segment.h
Go to the documentation of this file.
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 
21  {
22  public:
23  uint64_t offset;
24  uint64_t length;
25 
26 
31  static const FileSegment Null;
32 
33 
39  {
40  offset = length = 0;
41  }
42 
48  FileSegment(uint64_t offset, uint64_t length)
49  {
50  this->offset = offset;
51  this->length = length;
52  }
53 
57  FileSegment(const FileSegment& segment)
58  {
59  *this = segment;
60  }
61 
66  {
67  offset = segment.offset;
68  length = segment.length;
69 
70  return *this;
71  }
72 
81  {
82  assert((length - count) >= 0);
83 
84  offset += count;
85  length -= count;
86 
87  return *this;
88  }
89 
97  FileSegment& RemoveLast(int count)
98  {
99  assert((length - count) >= 0);
100 
101  length -= count;
102 
103  return *this;
104  }
105 
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_ */
static const FileSegment Null
Identifies a null segment, with the offset as well as the length set to zero.
Definition: file_segment.h:31
FileSegment(uint64_t offset, uint64_t length)
Initializes the segment with the given parameters.
Definition: file_segment.h:48
bool operator==(const FileSegment &segment) const
Definition: file_segment.h:116
Identifies a data segment of a file.
Definition: file_segment.h:20
virtual ~FileSegment()
Definition: file_segment.h:137
FileSegment & RemoveFirst(int count)
Removes the first bytes of the segment.
Definition: file_segment.h:80
bool operator!=(const FileSegment &segment) const
Definition: file_segment.h:121
uint64_t offset
Offset of the data segment.
Definition: file_segment.h:23
FileSegment()
Initializes all the member variables with zero, being a null segment.
Definition: file_segment.h:38
FileSegment & RemoveLast(int count)
Removes the last bytes of the segment.
Definition: file_segment.h:97
T & SerializeWith(T &stream)
Definition: file_segment.h:126
bool IsContiguousTo(const FileSegment &segment) const
Returns true if the segment is contiguous to another given segment, so the first byte of the given se...
Definition: file_segment.h:111
FileSegment(const FileSegment &segment)
Copy constructor.
Definition: file_segment.h:57
ostream & operator<<(ostream &out, const Request &request)
Definition: request.cc:65
uint64_t length
Length of the data segment.
Definition: file_segment.h:24
FileSegment & operator=(const FileSegment &segment)
Copy assignment.
Definition: file_segment.h:65