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	TargetAddressRange(const TargetAddressRange& other)
30		:
31		fStart(other.fStart),
32		fSize(other.fSize)
33	{
34	}
35
36	TargetAddressRange& operator=(const TargetAddressRange& other)
37	{
38		fStart = other.fStart;
39		fSize = other.fSize;
40		return *this;
41	}
42
43	bool operator==(const TargetAddressRange& other) const
44	{
45		return fStart == other.fStart && fSize == other.fSize;
46	}
47
48	bool operator!=(const TargetAddressRange& other) const
49	{
50		return !(*this == other);
51	}
52
53	target_addr_t Start() const
54	{
55		return fStart;
56	}
57
58	target_size_t Size() const
59	{
60		return fSize;
61	}
62
63	target_addr_t End() const
64	{
65		return fStart + fSize;
66	}
67
68	bool Contains(target_addr_t address) const
69	{
70		return address >= Start() && address < End();
71	}
72
73	bool Contains(const TargetAddressRange& other) const
74	{
75		return Start() <= other.Start() && End() >= other.End();
76	}
77
78	bool IntersectsWith(const TargetAddressRange& other) const
79	{
80		return Contains(other.Start()) || other.Contains(Start());
81	}
82
83	TargetAddressRange& operator|=(const TargetAddressRange& other)
84	{
85		if (fSize == 0)
86			return *this = other;
87
88		if (other.fSize > 0) {
89			target_addr_t end = std::max(End(), other.End());
90			fStart = std::min(fStart, other.fStart);
91			fSize = end - fStart;
92		}
93
94		return *this;
95	}
96
97private:
98	target_addr_t	fStart;
99	target_size_t	fSize;
100};
101
102
103#endif	// TARGET_ADDRESS_RANGE_H
104