1 // 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 12 namespace 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 // 17 class Region { 18 public: 19 // Default constructor: and empty region. Region()20 Region() : start_(NULL), length_(0) {} 21 22 // Usual constructor for regions given a |start| address and |length|. Region(const void * start,size_t 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. Region(const std::string & string)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. Region(const Region & other)36 Region(const Region& other) : start_(other.start_), length_(other.length_) {} 37 38 // Assignment 'operator' makes |this| region the same as |other|. assign(const Region & 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. start()46 const uint8* start() const { return start_; } 47 48 // Returns the length of the region. length()49 size_t length() const { return length_; } 50 51 // Returns the address after the last byte of the region. end()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_ 63