1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef TARGET_ADDRESS_RANGE_H
6#define TARGET_ADDRESS_RANGE_H
7
8#include <algorithm>
9
10#include "Types.h"
11
12
13class TargetAddressRange {
14public:
15	TargetAddressRange()
16		:
17		fStart(0),
18		fSize(0)
19	{
20	}
21
22	TargetAddressRange(target_addr_t start, target_size_t size)
23		:
24		fStart(start),
25		fSize(size)
26	{
27	}
28
29	bool operator==(const TargetAddressRange& other) const
30	{
31		return fStart == other.fStart && fSize == other.fSize;
32	}
33
34	bool operator!=(const TargetAddressRange& other) const
35	{
36		return !(*this == other);
37	}
38
39	target_addr_t Start() const
40	{
41		return fStart;
42	}
43
44	target_size_t Size() const
45	{
46		return fSize;
47	}
48
49	target_addr_t End() const
50	{
51		return fStart + fSize;
52	}
53
54	bool Contains(target_addr_t address) const
55	{
56		return address >= Start() && address < End();
57	}
58
59	bool Contains(const TargetAddressRange& other) const
60	{
61		return Start() <= other.Start() && End() >= other.End();
62	}
63
64	bool IntersectsWith(const TargetAddressRange& other) const
65	{
66		return Contains(other.Start()) || other.Contains(Start());
67	}
68
69	TargetAddressRange& operator|=(const TargetAddressRange& other)
70	{
71		if (fSize == 0)
72			return *this = other;
73
74		if (other.fSize > 0) {
75			target_addr_t end = std::max(End(), other.End());
76			fStart = std::min(fStart, other.fStart);
77			fSize = end - fStart;
78		}
79
80		return *this;
81	}
82
83private:
84	target_addr_t	fStart;
85	target_size_t	fSize;
86};
87
88
89#endif	// TARGET_ADDRESS_RANGE_H
90