1/*
2 * File:	DataTarget.h
3 *
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
6 */
7#if !defined(_DataTarget_h_)
8#define _DataTarget_h_
9
10#include "stdafx.h"
11#include "DataSource.h"
12
13namespace elftosb
14{
15
16// Forward declaration
17class DataSource;
18
19/*!
20 * \brief Abstract base class for the target address or range of data.
21 *
22 * Targets at the most basic level have a single address, and potentially
23 * an address range. Unbounded targets have a beginning address but no
24 * specific end address, while bounded targets do have an end address.
25 *
26 * Users of a data target can access the begin and end addresses directly.
27 * However, the most powerful way to use a target is with the
28 * getRangeForSegment() method. This method returns the target address range
29 * for a segment of a data source. The value of the resulting range can be
30 * completely dependent upon the segment's properties, those of its data
31 * source, and the type of data target.
32 *
33 * \see elftosb::DataSource
34 */
35class DataTarget
36{
37public:
38	//! \brief Simple structure that describes an addressed region of memory.
39	//! \todo Decide if the end address is inclusive or not.
40	struct AddressRange
41	{
42		uint32_t m_begin;
43		uint32_t m_end;
44	};
45
46public:
47	//! \brief Default constructor.
48	DataTarget() : m_source(0) {}
49
50	//! \brief Destructor.
51	virtual ~DataTarget() {}
52
53	//! \brief Whether the target is just a single address or has an end to it.
54	virtual bool isBounded() { return false; }
55
56	virtual uint32_t getBeginAddress() { return 0; }
57	virtual uint32_t getEndAddress() { return 0; }
58
59	//! \brief Return the address range for a segment of a data source.
60	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment)=0;
61
62	inline void setSource(DataSource * source) { m_source = source; }
63	inline DataSource * getSource() const { return m_source; }
64
65protected:
66	DataSource * m_source;	//!< Corresponding data source for this target.
67};
68
69/*!
70 * \brief Target with a constant values for the addresses.
71 *
72 * This target type supports can be both bounded and unbounded. It always has
73 * at least one address, the beginning address. The end address is optional,
74 * and if not provided makes the target unbounded.
75 */
76class ConstantDataTarget : public DataTarget
77{
78public:
79	//! \brief Constructor taking only a begin address.
80	ConstantDataTarget(uint32_t start) : DataTarget(), m_begin(start), m_end(0), m_hasEnd(false) {}
81
82	//! \brief Constructor taking both begin and end addresses.
83	ConstantDataTarget(uint32_t start, uint32_t end) : DataTarget(), m_begin(start), m_end(end), m_hasEnd(true) {}
84
85	//! \brief The target is bounded if an end address was specified.
86	virtual bool isBounded() { return m_hasEnd; }
87
88	virtual uint32_t getBeginAddress() { return m_begin; }
89	virtual uint32_t getEndAddress() { return m_end; }
90
91	//! \brief Return the address range for a segment of a data source.
92	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
93
94protected:
95	uint32_t m_begin;	//!< Start address.
96	uint32_t m_end;		//!< End address.
97	bool m_hasEnd;		//!< Was an end address specified?
98};
99
100/*!
101 * \brief Target address that is the "natural" location of whatever the source data is.
102 *
103 * The data source used with the target must have a natural location. If
104 * getRangeForSegment() is called with a segment that does not have a natural
105 * location, a semantic_error will be thrown.
106 */
107class NaturalDataTarget : public DataTarget
108{
109public:
110	//! \brief Default constructor.
111	NaturalDataTarget() : DataTarget() {}
112
113	//! \brief Natural data targets are bounded by their source's segment lengths.
114	virtual bool isBounded() { return true; }
115
116	//! \brief Return the address range for a segment of a data source.
117	virtual DataTarget::AddressRange getRangeForSegment(DataSource & source, DataSource::Segment & segment);
118};
119
120}; // namespace elftosb
121
122#endif // _DataTarget_h_
123