region.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef COURGETTE_REGION_H_
  5. #define COURGETTE_REGION_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. namespace courgette {
  10. // A Region is a descriptor for a region of memory. It has a start address and
  11. // a length measured in bytes. The Region object does not own the memory.
  12. //
  13. class Region {
  14. public:
  15. // Default constructor: and empty region.
  16. Region() : start_(nullptr), length_(0) {}
  17. // Usual constructor for regions given a |start| address and |length|.
  18. Region(const void* start, size_t length)
  19. : start_(static_cast<const uint8_t*>(start)), length_(length) {}
  20. // String constructor. Region is owned by the string, so the string should
  21. // have a lifetime greater than the region.
  22. explicit Region(const std::string& string)
  23. : start_(reinterpret_cast<const uint8_t*>(string.c_str())),
  24. length_(string.length()) {}
  25. // Copy constructor.
  26. Region(const Region& other) : start_(other.start_), length_(other.length_) {}
  27. // Assignment 'operator' makes |this| region the same as |other|.
  28. Region& assign(const Region& other) {
  29. this->start_ = other.start_;
  30. this->length_ = other.length_;
  31. return *this;
  32. }
  33. // Returns the starting address of the region.
  34. const uint8_t* start() const { return start_; }
  35. // Returns the length of the region.
  36. size_t length() const { return length_; }
  37. // Returns the address after the last byte of the region.
  38. const uint8_t* end() const { return start_ + length_; }
  39. private:
  40. const uint8_t* start_;
  41. size_t length_;
  42. void operator=(const Region&); // Disallow assignment operator.
  43. };
  44. } // namespace
  45. #endif // COURGETTE_REGION_H_