blob: 4aafbca8b05ba9c1da50ac0427855586249619e6 [file] [log] [blame]
[email protected]04ca1bc2009-05-08 23:00:291// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COURGETTE_REGION_H_
6#define COURGETTE_REGION_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11
12namespace courgette {
13
14// A Region is a descriptor for a region of memory. It has a start address and
15// a length measured in bytes. The Region object does not own the memory.
16//
17class Region {
18 public:
19 // Default constructor: and empty region.
20 Region() : start_(NULL), length_(0) {}
21
22 // Usual constructor for regions given a |start| address and |length|.
23 Region(const void* start, size_t length)
24 : start_(static_cast<const uint8*>(start)),
25 length_(length) {
26 }
27
28 // String constructor. Region is owned by the string, so the string should
29 // have a lifetime greater than the region.
30 explicit Region(const std::string& string)
31 : start_(reinterpret_cast<const uint8*>(string.c_str())),
32 length_(string.length()) {
33 }
34
35 // Copy constructor.
36 Region(const Region& other) : start_(other.start_), length_(other.length_) {}
37
38 // Assignment 'operator' makes |this| region the same as |other|.
39 Region& assign(const Region& other) {
40 this->start_ = other.start_;
41 this->length_ = other.length_;
42 return *this;
43 }
44
45 // Returns the starting address of the region.
46 const uint8* start() const { return start_; }
47
48 // Returns the length of the region.
49 size_t length() const { return length_; }
50
51 // Returns the address after the last byte of the region.
52 const uint8* end() const { return start_ + length_; }
53
54 private:
55 const uint8* start_;
56 size_t length_;
57
58 void operator=(const Region&); // Disallow assignment operator.
59};
60
61} // namespace
62#endif // COURGETTE_REGION_H_